From ce99b46b263ab56d5f279ab794758cf0c78d1379 Mon Sep 17 00:00:00 2001 From: dvolper Date: Sat, 22 Jun 2024 13:54:33 +0200 Subject: [PATCH] use ASCII logger in NuGet loader --- .../Commands/GenerateCommand.Outputs.cs | 2 +- src/Doki.CommandLine/NuGet/NuGetLoader.cs | 7 +- src/Doki.CommandLine/NuGet/NuGetLogger.cs | 85 +++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/Doki.CommandLine/NuGet/NuGetLogger.cs diff --git a/src/Doki.CommandLine/Commands/GenerateCommand.Outputs.cs b/src/Doki.CommandLine/Commands/GenerateCommand.Outputs.cs index 50aa0c0..2cfd0ed 100644 --- a/src/Doki.CommandLine/Commands/GenerateCommand.Outputs.cs +++ b/src/Doki.CommandLine/Commands/GenerateCommand.Outputs.cs @@ -34,7 +34,7 @@ internal partial class GenerateCommand var nugetFolder = Path.Combine(workingDirectory.FullName, ".doki", "nuget"); - using var nugetLoader = new NuGetLoader(output.From); + using var nugetLoader = new NuGetLoader(_logger, output.From); var assemblyPath = await nugetLoader.LoadPackageAsync(output.Type, nugetFolder, allowPreview, cancellationToken); diff --git a/src/Doki.CommandLine/NuGet/NuGetLoader.cs b/src/Doki.CommandLine/NuGet/NuGetLoader.cs index 753ed04..ce55529 100644 --- a/src/Doki.CommandLine/NuGet/NuGetLoader.cs +++ b/src/Doki.CommandLine/NuGet/NuGetLoader.cs @@ -17,12 +17,13 @@ internal class NuGetLoader : IDisposable { private readonly SourceCacheContext _cacheContext = new(); - //TODO use console logger (ASCII) - private readonly ILogger _logger = NullLogger.Instance; + private readonly NuGetLogger _logger; private readonly SourceRepositoryProvider _sourceRepositoryProvider; - public NuGetLoader(string? source = null) + public NuGetLoader(Microsoft.Extensions.Logging.ILogger logger, string? source = null) { + _logger = new NuGetLogger(logger); + var sources = new List { new("https://api.nuget.org/v3/index.json") diff --git a/src/Doki.CommandLine/NuGet/NuGetLogger.cs b/src/Doki.CommandLine/NuGet/NuGetLogger.cs new file mode 100644 index 0000000..bed22be --- /dev/null +++ b/src/Doki.CommandLine/NuGet/NuGetLogger.cs @@ -0,0 +1,85 @@ +using Microsoft.Extensions.Logging; +using NuGet.Common; +using ILogger = NuGet.Common.ILogger; +using LogLevel = NuGet.Common.LogLevel; + +namespace Doki.CommandLine.NuGet; + +internal class NuGetLogger(Microsoft.Extensions.Logging.ILogger logger) : ILogger +{ + public void LogDebug(string data) + { + logger.LogDebug(data); + } + + public void LogVerbose(string data) + { + logger.LogTrace(data); + } + + public void LogInformation(string data) + { + logger.LogInformation(data); + } + + public void LogMinimal(string data) + { + logger.LogInformation(data); + } + + public void LogWarning(string data) + { + logger.LogWarning(data); + } + + public void LogError(string data) + { + logger.LogError(data); + } + + public void LogInformationSummary(string data) + { + logger.LogInformation(data); + } + + public void Log(LogLevel level, string data) + { + switch (level) + { + case LogLevel.Debug: + LogDebug(data); + break; + case LogLevel.Verbose: + LogVerbose(data); + break; + case LogLevel.Information: + LogInformation(data); + break; + case LogLevel.Minimal: + LogMinimal(data); + break; + case LogLevel.Warning: + LogWarning(data); + break; + case LogLevel.Error: + LogError(data); + break; + } + } + + public Task LogAsync(LogLevel level, string data) + { + Log(level, data); + return Task.CompletedTask; + } + + public void Log(ILogMessage message) + { + Log(message.Level, message.Message); + } + + public Task LogAsync(ILogMessage message) + { + return LogAsync(message.Level, message.Message); + } +} \ No newline at end of file