2
0
mirror of synced 2025-01-09 16:15:51 +00:00

79 lines
2.3 KiB
C#
Raw Normal View History

namespace Core
2023-09-11 16:57:57 +02:00
{
2023-09-14 15:40:15 +02:00
internal class PluginManager
2023-09-11 16:57:57 +02:00
{
2023-09-21 10:33:09 +02:00
private readonly List<PluginToolsPair> pairs = new List<PluginToolsPair>();
2023-09-11 16:57:57 +02:00
internal void InstantiatePlugins(Type[] pluginTypes, IToolsFactory provider)
2023-09-11 16:57:57 +02:00
{
2023-09-21 10:33:09 +02:00
pairs.Clear();
2023-09-12 10:31:55 +02:00
foreach (var pluginType in pluginTypes)
{
var tools = provider.CreateTools();
var plugin = InstantiatePlugins(pluginType, tools);
ApplyLogPrefix(plugin, tools);
2023-09-12 10:31:55 +02:00
}
2023-09-11 16:57:57 +02:00
}
2023-09-14 15:40:15 +02:00
internal void AnnouncePlugins()
2023-09-11 16:57:57 +02:00
{
2023-09-21 10:33:09 +02:00
foreach (var pair in pairs) pair.Plugin.Announce();
2023-09-11 16:57:57 +02:00
}
2023-09-14 15:40:15 +02:00
internal PluginMetadata GatherPluginMetadata()
2023-09-13 16:06:05 +02:00
{
var metadata = new PluginMetadata();
2023-09-21 10:33:09 +02:00
foreach (var pair in pairs)
2023-09-13 16:06:05 +02:00
{
2023-09-21 10:33:09 +02:00
if (pair.Plugin is IHasMetadata m)
2023-09-13 16:06:05 +02:00
{
m.AddMetadata(metadata);
}
}
return metadata;
}
internal void DecommissionPlugins(bool deleteKubernetesResources, bool deleteTrackedFiles, bool waitTillDone)
2023-09-11 16:57:57 +02:00
{
2023-09-21 10:33:09 +02:00
foreach (var pair in pairs)
{
pair.Plugin.Decommission();
pair.Tools.Decommission(deleteKubernetesResources, deleteTrackedFiles, waitTillDone);
2023-09-21 10:33:09 +02:00
}
2023-09-11 16:57:57 +02:00
}
2023-09-12 11:25:04 +02:00
2023-09-14 15:40:15 +02:00
internal T GetPlugin<T>() where T : IProjectPlugin
2023-09-12 11:25:04 +02:00
{
2023-09-21 10:33:09 +02:00
return (T)pairs.Single(p => p.Plugin.GetType() == typeof(T)).Plugin;
2023-09-12 11:25:04 +02:00
}
private IProjectPlugin InstantiatePlugins(Type pluginType, PluginTools tools)
{
var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!;
2023-09-21 10:33:09 +02: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 10:33:09 +02: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 10:31:55 +02:00
}
2023-09-11 16:57:57 +02:00
}