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

79 lines
2.3 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
{
2023-09-21 08:33:09 +00:00
private readonly List<PluginToolsPair> pairs = new List<PluginToolsPair>();
2023-09-11 14:57:57 +00:00
internal void InstantiatePlugins(Type[] pluginTypes, IToolsFactory provider)
2023-09-11 14:57:57 +00:00
{
2023-09-21 08:33:09 +00:00
pairs.Clear();
2023-09-12 08:31:55 +00:00
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-21 08:33:09 +00:00
foreach (var pair in pairs) pair.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();
2023-09-21 08:33:09 +00:00
foreach (var pair in pairs)
2023-09-13 14:06:05 +00:00
{
2023-09-21 08:33:09 +00:00
if (pair.Plugin is IHasMetadata m)
2023-09-13 14:06:05 +00:00
{
m.AddMetadata(metadata);
}
}
return metadata;
}
internal void DecommissionPlugins(bool deleteKubernetesResources, bool deleteTrackedFiles, bool waitTillDone)
2023-09-11 14:57:57 +00:00
{
2023-09-21 08:33:09 +00:00
foreach (var pair in pairs)
{
pair.Plugin.Decommission();
pair.Tools.Decommission(deleteKubernetesResources, deleteTrackedFiles, waitTillDone);
2023-09-21 08:33:09 +00:00
}
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
{
2023-09-21 08:33:09 +00:00
return (T)pairs.Single(p => p.Plugin.GetType() == typeof(T)).Plugin;
2023-09-12 09:25:04 +00:00
}
private IProjectPlugin InstantiatePlugins(Type pluginType, PluginTools tools)
{
var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!;
2023-09-21 08:33:09 +00:00
pairs.Add(new PluginToolsPair(plugin, tools));
return plugin;
}
private void ApplyLogPrefix(IProjectPlugin plugin, PluginTools tools)
{
if (plugin is IHasLogPrefix hasLogPrefix)
{
tools.ApplyLogPrefix(hasLogPrefix.LogPrefix);
}
}
2023-09-21 08:33:09 +00:00
private class PluginToolsPair
{
public PluginToolsPair(IProjectPlugin plugin, IPluginTools tools)
{
Plugin = plugin;
Tools = tools;
}
public IProjectPlugin Plugin { get; }
public IPluginTools Tools { get; }
}
2023-09-12 08:31:55 +00:00
}
2023-09-11 14:57:57 +00:00
}