Skip to content

Commit

Permalink
Merge pull request #117 from Atlas-Rhythm/feat/extra-plugin-folders
Browse files Browse the repository at this point in the history
Support for multiple plugin folders
  • Loading branch information
sc2ad authored Sep 7, 2023
2 parents 29df75e + 6d1b264 commit 3f74eca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit 3f74eca

Please sign in to comment.