[backend/plugins] Migrate from Weikio.PluginFramework to Iceshrimp.AssemblyUtils
This commit is contained in:
parent
0e9a15e9dd
commit
d602d766fa
4 changed files with 13 additions and 33 deletions
|
@ -19,18 +19,4 @@ public static class AssemblyHelpers
|
||||||
return assembly.GetManifestResourceStream(resourceName) ??
|
return assembly.GetManifestResourceStream(resourceName) ??
|
||||||
throw new Exception($"Failed to get embedded resource {resourceName} from assembly.");
|
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Iceshrimp.AssemblyUtils;
|
||||||
using Iceshrimp.Backend.Core.Extensions;
|
using Iceshrimp.Backend.Core.Extensions;
|
||||||
using Weikio.PluginFramework.Abstractions;
|
|
||||||
using Weikio.PluginFramework.Catalogs;
|
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Core.Helpers;
|
namespace Iceshrimp.Backend.Core.Helpers;
|
||||||
|
|
||||||
using LoadedPlugin = (Plugin descriptor, IPlugin instance);
|
using LoadedPlugin = (Assembly assembly, IPlugin instance);
|
||||||
|
|
||||||
public abstract class PluginLoader
|
public abstract class PluginLoader
|
||||||
{
|
{
|
||||||
|
@ -20,22 +19,16 @@ public abstract class PluginLoader
|
||||||
|
|
||||||
private static List<LoadedPlugin> _loaded = [];
|
private static List<LoadedPlugin> _loaded = [];
|
||||||
private static IEnumerable<IPlugin> Plugins => _loaded.Select(p => p.instance);
|
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()
|
public static async Task LoadPlugins()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(DllPath)) return;
|
if (!Directory.Exists(DllPath)) return;
|
||||||
var dlls = Directory.EnumerateFiles(DllPath, "*.dll").ToList();
|
var dlls = Directory.EnumerateFiles(DllPath, "*.dll").ToList();
|
||||||
var catalogs = dlls
|
_loaded = dlls.Select(Path.GetFullPath)
|
||||||
.Select(p => new AssemblyPluginCatalog(p, type => type.Implements<IPlugin>()))
|
.Select(AssemblyLoader.LoadAssemblyFromPath)
|
||||||
.Cast<IPluginCatalog>()
|
.Select(p => (assembly: p, impls: AssemblyLoader.GetImplementationsOfInterface<IPlugin>(p)))
|
||||||
.ToArray();
|
.SelectMany(p => p.impls.Select(impl => (p.assembly, Activator.CreateInstance(impl) as IPlugin)))
|
||||||
|
|
||||||
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>()
|
.Cast<LoadedPlugin>()
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
|
@ -62,7 +55,7 @@ public abstract class PluginLoader
|
||||||
}
|
}
|
||||||
|
|
||||||
var plugins = _loaded.Select(plugin => $"{plugin.instance.Name} v{plugin.instance.Version} " +
|
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,
|
logger.LogInformation("Loaded {count} plugins from {dllPath}: \n* {files}", _loaded.Count, DllPath,
|
||||||
string.Join("\n* ", plugins));
|
string.Join("\n* ", plugins));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Iceshrimp.AssemblyUtils;
|
||||||
using Iceshrimp.Backend.Core.Helpers;
|
using Iceshrimp.Backend.Core.Helpers;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Core.Services;
|
namespace Iceshrimp.Backend.Core.Services;
|
||||||
|
@ -9,7 +10,7 @@ public class CronService(IServiceScopeFactory serviceScopeFactory) : BackgroundS
|
||||||
{
|
{
|
||||||
var tasks = PluginLoader
|
var tasks = PluginLoader
|
||||||
.Assemblies.Prepend(Assembly.GetExecutingAssembly())
|
.Assemblies.Prepend(Assembly.GetExecutingAssembly())
|
||||||
.SelectMany(assembly => AssemblyHelpers.GetImplementationsOfInterface(typeof(ICronTask), assembly))
|
.SelectMany(AssemblyLoader.GetImplementationsOfInterface<ICronTask>)
|
||||||
.Select(p => Activator.CreateInstance(p) as ICronTask)
|
.Select(p => Activator.CreateInstance(p) as ICronTask)
|
||||||
.Where(p => p != null)
|
.Where(p => p != null)
|
||||||
.Cast<ICronTask>();
|
.Cast<ICronTask>();
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
<PackageReference Include="System.Text.Json" Version="8.0.4"/>
|
<PackageReference Include="System.Text.Json" Version="8.0.4"/>
|
||||||
<PackageReference Include="Ulid" Version="1.3.3"/>
|
<PackageReference Include="Ulid" Version="1.3.3"/>
|
||||||
<PackageReference Include="Iceshrimp.WebPush" Version="2.0.0"/>
|
<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>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- We want Razor runtime compilation support, but only during development -->
|
<!-- We want Razor runtime compilation support, but only during development -->
|
||||||
|
|
Loading…
Add table
Reference in a new issue