Skip to content

Commit 6eef0dd

Browse files
authored
Merge pull request #67 from tgiachi/fix/plugin-directory-autocreate
fix(plugin): create missing plugin directories instead of failing
2 parents 3717bee + eabd45c commit 6eef0dd

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/SquidStd.Plugin/Extensions/SquidStdBootstrapPluginExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public static class SquidStdBootstrapPluginExtensions
1818
/// across the whole set, and invokes <see cref="ISquidStdPlugin.Configure" /> for each plugin
1919
/// in order against the bootstrap container. Must be called before the bootstrap starts, so
2020
/// plugins can register configuration sections before the configuration is loaded. Relative
21-
/// directories are resolved against the bootstrap root directory. Any failure aborts startup:
21+
/// directories are resolved against the bootstrap root directory and created when missing
22+
/// (an empty directory yields no plugins). Any failure aborts startup:
2223
/// loader problems raise <see cref="Exceptions.PluginLoadException" /> and plugin exceptions
2324
/// propagate unchanged. Plugin assemblies load into the default AssemblyLoadContext and are
2425
/// fully trusted: there is no unloading and no version isolation. Note that plugin load
@@ -56,6 +57,10 @@ Action<PluginCollectionBuilder> configure
5657
? directory
5758
: Path.Combine(bootstrap.Options.RootDirectory, directory);
5859

60+
// Managed like the bootstrap's DirectoriesConfig entries: a missing plugin
61+
// directory is created (and simply yields no plugins), not treated as an error.
62+
Directory.CreateDirectory(resolved);
63+
5964
plugins.AddRange(PluginAssemblyScanner.Scan(resolved));
6065
}
6166

src/SquidStd.Plugin/PluginCollectionBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public PluginCollectionBuilder Add(ISquidStdPlugin plugin)
4444

4545
/// <summary>
4646
/// Adds a directory to scan for external plugin assemblies (*.dll, non-recursive).
47-
/// Relative paths are resolved against the bootstrap root directory.
47+
/// Relative paths are resolved against the bootstrap root directory; a missing
48+
/// directory is created and yields no plugins.
4849
/// </summary>
4950
/// <param name="path">Absolute or root-relative directory path.</param>
5051
/// <returns>The same builder for chaining.</returns>

src/SquidStd.Plugin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ await bootstrap.RunAsync();
4040
can depend on an internal one and vice versa.
4141
- `Configure` runs before the configuration load, so plugins can register their own configuration
4242
sections and services ahead of time.
43+
- Plugin directories are managed like the other bootstrap directories: a missing directory is
44+
created on the spot and simply yields no plugins.
4345
- Each plugin receives a `PluginContext` populated with the standard keys: `PluginContextKeys.RootDirectory`
4446
(the bootstrap root directory) and `PluginContextKeys.AppName` (the bootstrap app name).
4547

tests/SquidStd.Tests/Plugin/SquidStdBootstrapPluginExtensionsTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ public async Task UsePlugins_LoadsExternalPluginsFromRelativeDirectory()
9393
Assert.Equal("tests.external", bootstrap.Container.Resolve<string>(serviceKey: "plugin-marker"));
9494
}
9595

96+
[Fact]
97+
public async Task UsePlugins_MissingDirectory_IsCreatedAndYieldsNoPlugins()
98+
{
99+
using var root = new TempDirectory();
100+
101+
await using var bootstrap = SquidStdBootstrap.Create(
102+
new SquidStdOptions { ConfigName = "plugintest", RootDirectory = root.Path }
103+
);
104+
105+
bootstrap.UsePlugins(plugins => plugins.FromDirectory("plugins"));
106+
107+
Assert.True(Directory.Exists(root.Combine("plugins")));
108+
}
109+
96110
[Fact]
97111
public async Task UsePlugins_AfterStart_Throws()
98112
{

0 commit comments

Comments
 (0)