feat: Dynamic command loader, default command with module registry
This commit is contained in:
99
Program.cs
99
Program.cs
@ -1,2 +1,97 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
namespace Automancer;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Automancer.Common;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Automancer.Commands;
|
||||
|
||||
public abstract class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
CommandApp? app = null;
|
||||
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureLogging(logging =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.AddConsole();
|
||||
logging.SetMinimumLevel(LogLevel.Information);
|
||||
})
|
||||
.ConfigureAppConfiguration((hostingContext, config) => {
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
|
||||
config.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
||||
.AddEnvironmentVariables(prefix: "AUTOMANCER_")
|
||||
.AddCommandLine(args);
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
var registry = new CommandRegistry();
|
||||
services.AddSingleton(registry);
|
||||
|
||||
RegisterCommandModules(services);
|
||||
|
||||
var registrar = new TypeRegistrar(services);
|
||||
app = new CommandApp(registrar);
|
||||
app.SetDefaultCommand<RootCommand>();
|
||||
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.SetApplicationName("automancer");
|
||||
|
||||
var modules = provider.GetServices<ICommandModule>();
|
||||
var registry = provider.GetRequiredService<CommandRegistry>();
|
||||
|
||||
foreach (var module in modules)
|
||||
{
|
||||
if (module is ICommandModuleWithRegistry withRegistry)
|
||||
{
|
||||
withRegistry.Configure(config, registry);
|
||||
}
|
||||
else
|
||||
{
|
||||
module.Configure(config);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
return app != null ? await app.RunAsync(args) : 1;
|
||||
}
|
||||
|
||||
private static void RegisterCommandModules(IServiceCollection services)
|
||||
{
|
||||
var moduleType = typeof(ICommandModule);
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
|
||||
var moduleTypes = assemblies
|
||||
.SelectMany(a =>
|
||||
{
|
||||
try { return a.GetTypes(); }
|
||||
catch { return []; }
|
||||
})
|
||||
.Where(t => moduleType.IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)
|
||||
.ToList();
|
||||
|
||||
foreach (var type in moduleTypes)
|
||||
{
|
||||
services.AddSingleton(type);
|
||||
services.AddSingleton(typeof(ICommandModule), type);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user