feat: wip...migrating to proper app
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
namespace StreamShorts.Console.Hosting;
|
||||
|
||||
internal static class HostBuilderExtensions
|
||||
{
|
||||
public static CommandApp<DefaultCommand> BuildApp(this IHostBuilder builder)
|
||||
{
|
||||
var registrar = new TypeRegistrar(builder);
|
||||
var app = new CommandApp<DefaultCommand>(registrar);
|
||||
|
||||
app.Configure(static c =>
|
||||
c.SetExceptionHandler(static (ex, resolver) =>
|
||||
{
|
||||
var console = resolver?.Resolve(typeof(IAnsiConsole)) as IAnsiConsole;
|
||||
console?.WriteLine($"[red]An error occurred while executing the command:[/]");
|
||||
console?.WriteException(ex, ExceptionFormats.ShortenEverything);
|
||||
})
|
||||
);
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace StreamShorts.Console.Hosting;
|
||||
|
||||
internal class TypeRegistrar(IHostBuilder builder) : ITypeRegistrar
|
||||
{
|
||||
private readonly IHostBuilder _builder = builder;
|
||||
|
||||
public ITypeResolver Build()
|
||||
{
|
||||
return new TypeResolver(_builder.Build());
|
||||
}
|
||||
|
||||
public void Register(Type service, Type implementation)
|
||||
{
|
||||
_builder.ConfigureServices((_, services) => services.AddSingleton(service, implementation));
|
||||
}
|
||||
|
||||
public void RegisterInstance(Type service, object implementation)
|
||||
{
|
||||
_builder.ConfigureServices((_, services) => services.AddSingleton(service, implementation));
|
||||
}
|
||||
|
||||
public void RegisterLazy(Type service, Func<object> func)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(func);
|
||||
|
||||
_builder.ConfigureServices((_, services) => services.AddSingleton(service, _ => func()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace StreamShorts.Console.Hosting;
|
||||
|
||||
internal class TypeResolver(IHost provider) : ITypeResolver, IDisposable
|
||||
{
|
||||
private readonly IHost _host = provider ?? throw new ArgumentNullException(nameof(provider));
|
||||
|
||||
public object? Resolve(Type? type)
|
||||
{
|
||||
return type is not null ? _host.Services.GetService(type) : null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_host.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user