Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple plugin folders #117

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/Hive.Plugins/Using.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ In the app configuration, the configuration key `Plugins` contains the configura
// A map of plugin name to plugin configuration objects. Defaults
// to an empty map. This is only used when UsePluginSpecificConfig is
// false.
"PluginConfigurations": Map<string, object>
"PluginConfigurations": Map<string, object>,
// More directory paths to load plugins from, if necessary.
// Defaults to an empty array.
"ExtraPluginPaths": string[]
}
```

Expand Down
16 changes: 12 additions & 4 deletions src/Hive.Plugins/Loading/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ private class LoaderConfig
/// </summary>
public Dictionary<string, ConfigurationSection> PluginConfigurations { get; set; } = new();
*/
/// <summary>
/// Alternative folders to also load plugins from.
/// </summary>
public string[] ExtraPluginPaths { get; set; } = Array.Empty<string>();
}

private readonly LoaderConfig config;
Expand All @@ -54,8 +58,12 @@ public PluginLoader(IConfigurationSection config, PluginLoaderOptionsBuilder opt
public void LoadPlugins(IServiceCollection services, IHostEnvironment hostEnv)
{
var pluginDir = new DirectoryInfo(config.PluginPath);
var extraPluginDirs = config.ExtraPluginPaths.Select(path => new DirectoryInfo(path));

List<DirectoryInfo> pluginDirs = new() { pluginDir };
pluginDirs.AddRange(extraPluginDirs);

var pluginsToLoad = FindPluginsToLoad(pluginDir);
var pluginsToLoad = FindPluginsToLoad(pluginDirs);

var exceptions = new List<(Exception Exception, string PluginName)>();

Expand Down Expand Up @@ -133,9 +141,9 @@ public void LoadPlugins(IServiceCollection services, IHostEnvironment hostEnv)
}
}

private IEnumerable<DirectoryInfo> FindPluginsToLoad(DirectoryInfo pluginDir)
=> FindPotentialPluginsToLoad(pluginDir)
.Where(d => !config.ExcludePlugins.Contains(d.Name));
private IEnumerable<DirectoryInfo> FindPluginsToLoad(IEnumerable<DirectoryInfo> pluginDirs)
=> pluginDirs.SelectMany(dir => FindPotentialPluginsToLoad(dir)
.Where(d => !config.ExcludePlugins.Contains(d.Name)));

private IEnumerable<DirectoryInfo> FindPotentialPluginsToLoad(DirectoryInfo pluginDir)
{
Expand Down
Loading