feat(core): add PluginDiscovery directory scan
This commit is contained in:
parent
99d2702c13
commit
91618682e2
2 changed files with 129 additions and 0 deletions
38
src/AcDream.Core/Plugins/PluginDiscovery.cs
Normal file
38
src/AcDream.Core/Plugins/PluginDiscovery.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
namespace AcDream.Core.Plugins;
|
||||
|
||||
public sealed record PluginDiscoveryResult(
|
||||
string PluginDirectory,
|
||||
PluginManifest? Manifest,
|
||||
string? Error)
|
||||
{
|
||||
public bool Success => Manifest is not null && Error is null;
|
||||
}
|
||||
|
||||
public static class PluginDiscovery
|
||||
{
|
||||
public static IReadOnlyList<PluginDiscoveryResult> Scan(string pluginsRootDirectory)
|
||||
{
|
||||
if (!Directory.Exists(pluginsRootDirectory))
|
||||
return Array.Empty<PluginDiscoveryResult>();
|
||||
|
||||
var results = new List<PluginDiscoveryResult>();
|
||||
foreach (var subdir in Directory.EnumerateDirectories(pluginsRootDirectory))
|
||||
{
|
||||
var manifestPath = Path.Combine(subdir, "plugin.json");
|
||||
if (!File.Exists(manifestPath))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(manifestPath);
|
||||
var manifest = PluginManifest.Parse(json);
|
||||
results.Add(new PluginDiscoveryResult(subdir, manifest, null));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new PluginDiscoveryResult(subdir, null, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue