feat: Dynamic command loader, default command with module registry
This commit is contained in:
13
Common/CommandRegistry.cs
Normal file
13
Common/CommandRegistry.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace Automancer.Common;
|
||||
|
||||
public record CommandInfo(string Path, string? Description);
|
||||
|
||||
public class CommandRegistry
|
||||
{
|
||||
public List<CommandInfo> Commands { get; } = new();
|
||||
|
||||
public void Add(string path, string? description)
|
||||
{
|
||||
Commands.Add(new CommandInfo(path, description));
|
||||
}
|
||||
}
|
15
Common/ICommandModule.cs
Normal file
15
Common/ICommandModule.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace Automancer.Common;
|
||||
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
public interface ICommandModule
|
||||
{
|
||||
|
||||
void Configure(IConfigurator config);
|
||||
|
||||
}
|
||||
|
||||
public interface ICommandModuleWithRegistry : ICommandModule
|
||||
{
|
||||
void Configure(IConfigurator config, CommandRegistry registry);
|
||||
}
|
34
Common/TypeRegistrar.cs
Normal file
34
Common/TypeRegistrar.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Automancer.Common;
|
||||
|
||||
public sealed class TypeRegistrar : ITypeRegistrar
|
||||
{
|
||||
private readonly IServiceCollection _builder;
|
||||
|
||||
public TypeRegistrar(IServiceCollection builder)
|
||||
{
|
||||
_builder = builder;
|
||||
}
|
||||
|
||||
public ITypeResolver Build()
|
||||
{
|
||||
return new TypeResolver(_builder.BuildServiceProvider());
|
||||
}
|
||||
|
||||
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<object> factory)
|
||||
{
|
||||
_builder.AddSingleton(service, _ => factory());
|
||||
}
|
||||
}
|
24
Common/TypeResolver.cs
Normal file
24
Common/TypeResolver.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Automancer.Common;
|
||||
|
||||
public sealed class TypeResolver : ITypeResolver, IDisposable
|
||||
{
|
||||
private readonly ServiceProvider _provider;
|
||||
|
||||
public TypeResolver(ServiceProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public object? Resolve(Type? type)
|
||||
{
|
||||
return type is null ? null : _provider.GetService(type);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_provider.Dispose();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user