diff --git a/.gitignore b/.gitignore index 6a9072b..2ec8fdb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ *.DotSettings.user .idea/ .experiments/ -nuget.config \ No newline at end of file +nuget.config +/.vs/tone diff --git a/tone/Program.cs b/tone/Program.cs index 4e548db..085675a 100644 --- a/tone/Program.cs +++ b/tone/Program.cs @@ -32,12 +32,14 @@ using tone.Metadata.Taggers.IdTaggers.Audible; using ILogger = Serilog.ILogger; using Log = Serilog.Log; - +using JKToolKit.Spectre.AutoCompletion.Completion; +using JKToolKit.Spectre.AutoCompletion.Integrations; +using tone.SpectreConsoleHelpers; try { var debugMode = args.Contains("--debug"); - + var settingsProvider = new CustomCommandSettingsProvider(); var services = new ServiceCollection(); @@ -46,14 +48,14 @@ var loggerSettings = settingsProvider.Get(); var configBuilder = new ConfigurationBuilder().AddEnvironmentVariables(); var config = new LoggerConfiguration() - .ReadFrom.Configuration(configBuilder.Build()); - + .ReadFrom.Configuration(configBuilder.Build()); + // set loglevel to debug, if --debug is present if (debugMode && loggerSettings != null) { loggerSettings.LogLevel = LogLevel.Debug; - } - + } + if (loggerSettings == null || loggerSettings.LogLevel == LogLevel.None) { return config.CreateLogger(); @@ -68,7 +70,7 @@ LogLevel.Warning => config.MinimumLevel.Warning(), _ => config.MinimumLevel.Error() }; - + return config.CreateLogger(); }); services.AddLogging(loggingBuilder => loggingBuilder.SetMinimumLevel( @@ -144,27 +146,28 @@ }); }); - services.AddSingleton(_ => new AudibleIdTaggerSettings(){ + services.AddSingleton(_ => new AudibleIdTaggerSettings() + { MetadataUrlTemplate = Environment.GetEnvironmentVariable("PILABOR_AUDIBLE_METADATA_URL_TEMPLATE") ?? "", ChaptersUrlTemplate = Environment.GetEnvironmentVariable("PILABOR_AUDIBLE_CHAPTERS_URL_TEMPLATE") ?? "" }); services.AddHttpClient(); - + services.AddSingleton(); - + services.AddSingleton(sp => { var fs = sp.GetRequiredService(); var pathMatcher = sp.GetRequiredService(); var chapterFormat = sp.GetRequiredService(); var ffmetadataFormat = sp.GetRequiredService(); - var idTagger = sp.GetRequiredService(); - + var idTagger = sp.GetRequiredService(); + var taggers = new[] { settingsProvider.Build(s => new ToneJsonTagger(fs, s)), settingsProvider.Build(s => new MetadataTagger(s)), - + settingsProvider.Build(s => { idTagger.Id = s.Id; @@ -208,15 +211,15 @@ return javaScriptApi; }); -// services.AddSingleton(_ => AnsiConsole.Console); -// services.AddLogging(builder => builder.AddSerilog(dispose: true)); -// services.AddSingleton(_ => Log.Logger); - + // services.AddSingleton(_ => AnsiConsole.Console); + // services.AddLogging(builder => builder.AddSerilog(dispose: true)); + // services.AddSingleton(_ => Log.Logger); - var app = new CommandApp(new CustomTypeRegistrar(services)); + var app = new CommandApp(new ToneCustomTypeRegistrar(services)); app.Configure(config => { + config.AddAutoCompletion(x => x.AddPowershell()); config.SetInterceptor(new CustomCommandInterceptor(settingsProvider)); config.UseStrictParsing(); config.CaseSensitivity(CaseSensitivity.None); @@ -241,7 +244,7 @@ /*config.AddCommand("split") .WithDescription("split audio files") - ; + ; */ if (debugMode) diff --git a/tone/SpectreConsoleHelpers/ToneCustomTypeRegistrar.cs b/tone/SpectreConsoleHelpers/ToneCustomTypeRegistrar.cs new file mode 100644 index 0000000..97ddf77 --- /dev/null +++ b/tone/SpectreConsoleHelpers/ToneCustomTypeRegistrar.cs @@ -0,0 +1,107 @@ +using Microsoft.Extensions.DependencyInjection; + +using Sandreas.SpectreConsoleHelpers.DependencyInjection; + +using Spectre.Console.Cli; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tone.SpectreConsoleHelpers; + +public class ToneCustomTypeRegistrar : ITypeRegistrar +{ + private readonly IServiceCollection _builder; + + public ToneCustomTypeRegistrar(IServiceCollection builder) + { + _builder = builder; + } + + public ITypeResolver Build() + { + var resolver = new ToneCustomTypeResolver(); + _builder.AddSingleton(resolver); + var sp = _builder.BuildServiceProvider(); + resolver.Provider = sp; + return resolver; + } + + public void Register(Type service, Type implementation) + { + _builder.AddSingleton(service, implementation); + } + + public void RegisterInstance(Type service, object implementation) + { + _builder.AddSingleton(service, implementation); + } + + public void RegisterLazy(Type service, Func func) + { + Func func2 = func; + if (func2 == null) + { + throw new ArgumentNullException("func"); + } + + _builder.AddSingleton(service, (IServiceProvider _) => func2()); + } +} + +public sealed class ToneCustomTypeResolver : ITypeResolver, IDisposable +{ + private IServiceProvider _provider; + + public IServiceProvider Provider + { + get + { + return _provider; + } + set + { + if (value == null) + { + throw new ArgumentNullException("value"); + } + + if (_provider != null) + { + throw new InvalidOperationException("Provider has already been set."); + } + + _provider = value; + } + } + + public ToneCustomTypeResolver() + { + } + + public object? Resolve(Type? type) + { + if (!(type == null)) + { + if (type == typeof(ITypeResolver)) + { + return this; + } + + return _provider.GetService(type); + } + + return null; + } + + public void Dispose() + { + if (_provider is IDisposable disposable) + { + disposable.Dispose(); + } + } +} \ No newline at end of file diff --git a/tone/tone.csproj b/tone/tone.csproj index 3469e6a..3fc0d2b 100644 --- a/tone/tone.csproj +++ b/tone/tone.csproj @@ -39,6 +39,7 @@ +