[backend/plugins] Migrate from Weikio.PluginFramework to Iceshrimp.AssemblyUtils

This commit is contained in:
Laura Hausmann 2024-07-15 01:02:41 +02:00
parent 0e9a15e9dd
commit d602d766fa
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
4 changed files with 13 additions and 33 deletions

View file

@ -19,18 +19,4 @@ public static class AssemblyHelpers
return assembly.GetManifestResourceStream(resourceName) ??
throw new Exception($"Failed to get embedded resource {resourceName} from assembly.");
}
public static IEnumerable<Type> GetTypesWithAttribute(Type attribute, Assembly? assembly = null)
{
assembly ??= Assembly.GetExecutingAssembly();
return assembly.GetTypes().Where(type => Attribute.IsDefined(type, attribute));
}
public static IEnumerable<Type> GetImplementationsOfInterface(Type @interface, Assembly? assembly = null)
{
assembly ??= Assembly.GetExecutingAssembly();
return assembly.GetTypes()
.Where(type => type is { IsAbstract: false, IsClass: true } &&
type.GetInterfaces().Contains(@interface));
}
}

View file

@ -1,11 +1,10 @@
using System.Reflection;
using Iceshrimp.AssemblyUtils;
using Iceshrimp.Backend.Core.Extensions;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Catalogs;
namespace Iceshrimp.Backend.Core.Helpers;
using LoadedPlugin = (Plugin descriptor, IPlugin instance);
using LoadedPlugin = (Assembly assembly, IPlugin instance);
public abstract class PluginLoader
{
@ -20,24 +19,18 @@ public abstract class PluginLoader
private static List<LoadedPlugin> _loaded = [];
private static IEnumerable<IPlugin> Plugins => _loaded.Select(p => p.instance);
public static IEnumerable<Assembly> Assemblies => _loaded.Select(p => p.descriptor.Assembly);
public static IEnumerable<Assembly> Assemblies => _loaded.Select(p => p.assembly);
public static async Task LoadPlugins()
{
if (!Directory.Exists(DllPath)) return;
var dlls = Directory.EnumerateFiles(DllPath, "*.dll").ToList();
var catalogs = dlls
.Select(p => new AssemblyPluginCatalog(p, type => type.Implements<IPlugin>()))
.Cast<IPluginCatalog>()
.ToArray();
var combined = new CompositePluginCatalog(catalogs);
await combined.Initialize();
_loaded = combined.GetPlugins()
.Select(p => (descriptor: p, instance: Activator.CreateInstance(p) as IPlugin))
.Where(p => p.instance is not null)
.Cast<LoadedPlugin>()
.ToList();
_loaded = dlls.Select(Path.GetFullPath)
.Select(AssemblyLoader.LoadAssemblyFromPath)
.Select(p => (assembly: p, impls: AssemblyLoader.GetImplementationsOfInterface<IPlugin>(p)))
.SelectMany(p => p.impls.Select(impl => (p.assembly, Activator.CreateInstance(impl) as IPlugin)))
.Cast<LoadedPlugin>()
.ToList();
await Plugins.Select(i => i.Initialize()).AwaitAllNoConcurrencyAsync();
}
@ -62,7 +55,7 @@ public abstract class PluginLoader
}
var plugins = _loaded.Select(plugin => $"{plugin.instance.Name} v{plugin.instance.Version} " +
$"({Path.GetFileName(plugin.descriptor.Assembly.Location)})");
$"({Path.GetFileName(plugin.assembly.Location)})");
logger.LogInformation("Loaded {count} plugins from {dllPath}: \n* {files}", _loaded.Count, DllPath,
string.Join("\n* ", plugins));
}

View file

@ -1,4 +1,5 @@
using System.Reflection;
using Iceshrimp.AssemblyUtils;
using Iceshrimp.Backend.Core.Helpers;
namespace Iceshrimp.Backend.Core.Services;
@ -9,7 +10,7 @@ public class CronService(IServiceScopeFactory serviceScopeFactory) : BackgroundS
{
var tasks = PluginLoader
.Assemblies.Prepend(Assembly.GetExecutingAssembly())
.SelectMany(assembly => AssemblyHelpers.GetImplementationsOfInterface(typeof(ICronTask), assembly))
.SelectMany(AssemblyLoader.GetImplementationsOfInterface<ICronTask>)
.Select(p => Activator.CreateInstance(p) as ICronTask)
.Where(p => p != null)
.Cast<ICronTask>();

View file

@ -43,7 +43,7 @@
<PackageReference Include="System.Text.Json" Version="8.0.4"/>
<PackageReference Include="Ulid" Version="1.3.3"/>
<PackageReference Include="Iceshrimp.WebPush" Version="2.0.0"/>
<PackageReference Include="Weikio.PluginFramework" Version="1.5.1" />
<PackageReference Include="Iceshrimp.AssemblyUtils" Version="1.0.0"/>
</ItemGroup>
<!-- We want Razor runtime compilation support, but only during development -->