feat: plugins
This commit is contained in:
42
Program.cs
42
Program.cs
@ -9,10 +9,10 @@ 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;
|
||||
using System.Reflection;
|
||||
|
||||
public abstract class Program
|
||||
{
|
||||
@ -41,6 +41,7 @@ public abstract class Program
|
||||
var registry = new CommandRegistry();
|
||||
services.AddSingleton(registry);
|
||||
|
||||
LoadPluginAssemblies("plugins");
|
||||
RegisterCommandModules(services);
|
||||
|
||||
var registrar = new TypeRegistrar(services);
|
||||
@ -58,15 +59,9 @@ public abstract class Program
|
||||
|
||||
foreach (var module in modules)
|
||||
{
|
||||
if (module is ICommandModuleWithRegistry withRegistry)
|
||||
{
|
||||
withRegistry.Configure(config, registry);
|
||||
}
|
||||
else
|
||||
{
|
||||
module.Configure(config);
|
||||
}
|
||||
module.Configure(config, registry);
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
@ -76,14 +71,17 @@ public abstract class Program
|
||||
|
||||
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 []; }
|
||||
catch {
|
||||
return [];
|
||||
}
|
||||
})
|
||||
.Where(t => moduleType.IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)
|
||||
.ToList();
|
||||
@ -94,4 +92,24 @@ public abstract class Program
|
||||
services.AddSingleton(typeof(ICommandModule), type);
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadPluginAssemblies(string pluginDirectory)
|
||||
{
|
||||
if (!Directory.Exists(pluginDirectory))
|
||||
return;
|
||||
|
||||
var dlls = Directory.GetFiles(pluginDirectory, "*.dll", SearchOption.AllDirectories);
|
||||
|
||||
foreach (var dll in dlls)
|
||||
{
|
||||
try
|
||||
{
|
||||
var asm = Assembly.LoadFrom(dll);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[WARN] Failed to load plugin '{dll}': {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user