cs-codex-dist-tests/Framework/Core/PluginManager.cs

63 lines
1.8 KiB
C#
Raw Normal View History

namespace Core
2023-09-11 14:57:57 +00:00
{
2023-09-14 13:40:15 +00:00
internal class PluginManager
2023-09-11 14:57:57 +00:00
{
private readonly List<IProjectPlugin> projectPlugins = new List<IProjectPlugin>();
internal void InstantiatePlugins(Type[] pluginTypes, IToolsFactory provider)
2023-09-11 14:57:57 +00:00
{
2023-09-12 08:31:55 +00:00
projectPlugins.Clear();
foreach (var pluginType in pluginTypes)
{
var tools = provider.CreateTools();
var plugin = InstantiatePlugins(pluginType, tools);
ApplyLogPrefix(plugin, tools);
2023-09-12 08:31:55 +00:00
}
2023-09-11 14:57:57 +00:00
}
2023-09-14 13:40:15 +00:00
internal void AnnouncePlugins()
2023-09-11 14:57:57 +00:00
{
2023-09-13 08:03:11 +00:00
foreach (var plugin in projectPlugins) plugin.Announce();
2023-09-11 14:57:57 +00:00
}
2023-09-14 13:40:15 +00:00
internal PluginMetadata GatherPluginMetadata()
2023-09-13 14:06:05 +00:00
{
var metadata = new PluginMetadata();
foreach (var plugin in projectPlugins)
{
if (plugin is IHasMetadata m)
{
m.AddMetadata(metadata);
}
}
return metadata;
}
2023-09-14 13:40:15 +00:00
internal void DecommissionPlugins()
2023-09-11 14:57:57 +00:00
{
2023-09-13 08:03:11 +00:00
foreach (var plugin in projectPlugins) plugin.Decommission();
2023-09-11 14:57:57 +00:00
}
2023-09-12 09:25:04 +00:00
2023-09-14 13:40:15 +00:00
internal T GetPlugin<T>() where T : IProjectPlugin
2023-09-12 09:25:04 +00:00
{
return (T)projectPlugins.Single(p => p.GetType() == typeof(T));
}
private IProjectPlugin InstantiatePlugins(Type pluginType, PluginTools tools)
{
var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!;
projectPlugins.Add(plugin);
return plugin;
}
private void ApplyLogPrefix(IProjectPlugin plugin, PluginTools tools)
{
if (plugin is IHasLogPrefix hasLogPrefix)
{
tools.ApplyLogPrefix(hasLogPrefix.LogPrefix);
}
}
2023-09-12 08:31:55 +00:00
}
2023-09-11 14:57:57 +00:00
}