| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Injection.SourceGen.PluginFactories; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A plugin factory that uses compile-time generated type information |
| | | 9 | | /// instead of runtime reflection for plugin discovery and instantiation. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// This factory provides better performance and AOT compatibility by using |
| | | 14 | | /// pre-computed plugin information and factory delegates generated at compile time. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// To use this factory, your assembly must have: |
| | | 18 | | /// <list type="bullet"> |
| | | 19 | | /// <item>A reference to <c>NexusLabs.Needlr.Generators</c></item> |
| | | 20 | | /// <item>The <c>[assembly: GenerateTypeRegistry(...)]</c> attribute</item> |
| | | 21 | | /// </list> |
| | | 22 | | /// </para> |
| | | 23 | | /// <para> |
| | | 24 | | /// Plugins are sorted by their <see cref="PluginTypeInfo.Order"/> value (lower first), |
| | | 25 | | /// then alphabetically by fully qualified type name for deterministic execution order. |
| | | 26 | | /// Sorting is performed once during initialization. |
| | | 27 | | /// </para> |
| | | 28 | | /// </remarks> |
| | | 29 | | [DoNotAutoRegister] |
| | | 30 | | public sealed class GeneratedPluginFactory : IPluginFactory |
| | | 31 | | { |
| | | 32 | | private readonly Lazy<IReadOnlyList<PluginTypeInfo>> _lazyPlugins; |
| | | 33 | | private readonly bool _allowAllWhenAssembliesEmpty; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="GeneratedPluginFactory"/> class |
| | | 37 | | /// with a custom plugin provider. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="pluginProvider">A function that returns the plugin types.</param> |
| | | 40 | | /// <param name="allowAllWhenAssembliesEmpty"> |
| | | 41 | | /// When true, passing an empty assemblies collection to the factory methods will not filter plugins. |
| | | 42 | | /// This is useful for NativeAOT scenarios where producing an <see cref="Assembly"/> list without reflection is not |
| | | 43 | | /// </param> |
| | 591 | 44 | | public GeneratedPluginFactory( |
| | 591 | 45 | | Func<IReadOnlyList<PluginTypeInfo>> pluginProvider, |
| | 591 | 46 | | bool allowAllWhenAssembliesEmpty = false) |
| | | 47 | | { |
| | 591 | 48 | | ArgumentNullException.ThrowIfNull(pluginProvider); |
| | | 49 | | // Sort once during initialization - filtering maintains relative order |
| | 1164 | 50 | | _lazyPlugins = new(() => pluginProvider.Invoke() |
| | 46020 | 51 | | .OrderBy(info => info.Order) |
| | 46020 | 52 | | .ThenBy(info => info.PluginType.FullName, StringComparer.Ordinal) |
| | 1164 | 53 | | .ToArray()); |
| | 590 | 54 | | _allowAllWhenAssembliesEmpty = allowAllWhenAssembliesEmpty; |
| | 590 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates instances of plugins of type <typeparamref name="TPlugin"/>. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <typeparam name="TPlugin">The plugin interface or base type to search for.</typeparam> |
| | | 61 | | /// <returns>An enumerable of instantiated plugins implementing <typeparamref name="TPlugin"/>.</returns> |
| | | 62 | | /// <remarks> |
| | | 63 | | /// Plugins are returned in order by their <see cref="PluginTypeInfo.Order"/> value (lower first), |
| | | 64 | | /// then alphabetically by fully qualified type name for deterministic execution order. |
| | | 65 | | /// </remarks> |
| | | 66 | | public IEnumerable<TPlugin> CreatePlugins<TPlugin>() |
| | | 67 | | where TPlugin : class |
| | | 68 | | { |
| | 253 | 69 | | var pluginType = typeof(TPlugin); |
| | 253 | 70 | | return _lazyPlugins.Value |
| | 21001 | 71 | | .Where(info => pluginType.IsAssignableFrom(info.PluginType)) |
| | 257 | 72 | | .Select(info => (TPlugin)info.Factory()); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public IEnumerable<TPlugin> CreatePluginsFromAssemblies<TPlugin>( |
| | | 77 | | IEnumerable<Assembly> assemblies) |
| | | 78 | | where TPlugin : class |
| | | 79 | | { |
| | 315 | 80 | | var assemblyList = assemblies as IReadOnlyCollection<Assembly> ?? assemblies.ToArray(); |
| | 315 | 81 | | if (_allowAllWhenAssembliesEmpty && assemblyList.Count == 0) |
| | | 82 | | { |
| | 4 | 83 | | return CreatePlugins<TPlugin>(); |
| | | 84 | | } |
| | | 85 | | |
| | 311 | 86 | | var assemblySet = assemblyList |
| | 1254 | 87 | | .Select(a => a.GetName().Name) |
| | 1254 | 88 | | .Where(n => n is not null) |
| | 311 | 89 | | .ToHashSet(StringComparer.Ordinal)!; |
| | | 90 | | |
| | 311 | 91 | | var pluginType = typeof(TPlugin); |
| | 311 | 92 | | return _lazyPlugins.Value |
| | 311 | 93 | | .Where(info => |
| | 311 | 94 | | { |
| | 311 | 95 | | // Check if this plugin is from one of the specified assemblies |
| | 24249 | 96 | | var pluginAssemblyName = info.PluginType.Assembly.GetName().Name; |
| | 24249 | 97 | | if (pluginAssemblyName is null || !assemblySet.Contains(pluginAssemblyName)) |
| | 453 | 98 | | return false; |
| | 311 | 99 | | |
| | 311 | 100 | | // Check if plugin implements the requested type |
| | 23796 | 101 | | return pluginType.IsAssignableFrom(info.PluginType); |
| | 311 | 102 | | }) |
| | 2195 | 103 | | .Select(info => (TPlugin)info.Factory()); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Creates instances of plugins that are decorated with the specified attribute. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <typeparam name="TAttribute">The attribute type to search for in the type hierarchy.</typeparam> |
| | | 110 | | /// <returns>An enumerable of instantiated plugins decorated with <typeparamref name="TAttribute"/>.</returns> |
| | | 111 | | /// <remarks> |
| | | 112 | | /// Plugins are returned in order by their <see cref="PluginTypeInfo.Order"/> value (lower first), |
| | | 113 | | /// then alphabetically by fully qualified type name for deterministic execution order. |
| | | 114 | | /// </remarks> |
| | | 115 | | public IEnumerable<object> CreatePluginsWithAttribute<TAttribute>() |
| | | 116 | | where TAttribute : Attribute |
| | | 117 | | { |
| | 3 | 118 | | return _lazyPlugins.Value |
| | 4 | 119 | | .Where(info => info.HasAttribute<TAttribute>()) |
| | 5 | 120 | | .Select(info => info.Factory()); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public IEnumerable<object> CreatePluginsWithAttributeFromAssemblies<TAttribute>( |
| | | 125 | | IEnumerable<Assembly> assemblies) |
| | | 126 | | where TAttribute : Attribute |
| | | 127 | | { |
| | 4 | 128 | | var assemblyList = assemblies as IReadOnlyCollection<Assembly> ?? assemblies.ToArray(); |
| | 4 | 129 | | if (_allowAllWhenAssembliesEmpty && assemblyList.Count == 0) |
| | | 130 | | { |
| | 0 | 131 | | return CreatePluginsWithAttribute<TAttribute>(); |
| | | 132 | | } |
| | | 133 | | |
| | 4 | 134 | | var assemblySet = assemblyList |
| | 4 | 135 | | .Select(a => a.GetName().Name) |
| | 4 | 136 | | .Where(n => n is not null) |
| | 4 | 137 | | .ToHashSet(StringComparer.Ordinal)!; |
| | | 138 | | |
| | 4 | 139 | | return _lazyPlugins.Value |
| | 4 | 140 | | .Where(info => |
| | 4 | 141 | | { |
| | 4 | 142 | | // Check if this plugin is from one of the specified assemblies |
| | 182 | 143 | | var pluginAssemblyName = info.PluginType.Assembly.GetName().Name; |
| | 182 | 144 | | if (pluginAssemblyName is null || !assemblySet.Contains(pluginAssemblyName)) |
| | 16 | 145 | | return false; |
| | 4 | 146 | | |
| | 4 | 147 | | // Use pre-computed attribute info - no reflection needed |
| | 166 | 148 | | return info.HasAttribute<TAttribute>(); |
| | 4 | 149 | | }) |
| | 11 | 150 | | .Select(info => info.Factory()); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Creates instances of plugins of type <typeparamref name="TPlugin"/> that are |
| | | 155 | | /// decorated with the specified attribute. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <typeparam name="TPlugin">The plugin interface or base type to search for.</typeparam> |
| | | 158 | | /// <typeparam name="TAttribute">The attribute type to search for in the type hierarchy.</typeparam> |
| | | 159 | | /// <returns> |
| | | 160 | | /// An enumerable of instantiated plugins implementing <typeparamref name="TPlugin"/> and |
| | | 161 | | /// decorated with <typeparamref name="TAttribute"/>. |
| | | 162 | | /// </returns> |
| | | 163 | | /// <remarks> |
| | | 164 | | /// Plugins are returned in order by their <see cref="PluginTypeInfo.Order"/> value (lower first), |
| | | 165 | | /// then alphabetically by fully qualified type name for deterministic execution order. |
| | | 166 | | /// </remarks> |
| | | 167 | | public IEnumerable<TPlugin> CreatePlugins<TPlugin, TAttribute>() |
| | | 168 | | where TPlugin : class |
| | | 169 | | where TAttribute : Attribute |
| | | 170 | | { |
| | 4 | 171 | | var pluginType = typeof(TPlugin); |
| | 4 | 172 | | return _lazyPlugins.Value |
| | 5 | 173 | | .Where(info => pluginType.IsAssignableFrom(info.PluginType) && info.HasAttribute<TAttribute>()) |
| | 6 | 174 | | .Select(info => (TPlugin)info.Factory()); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <inheritdoc /> |
| | | 178 | | public IEnumerable<TPlugin> CreatePluginsFromAssemblies<TPlugin, TAttribute>( |
| | | 179 | | IEnumerable<Assembly> assemblies) |
| | | 180 | | where TPlugin : class |
| | | 181 | | where TAttribute : Attribute |
| | | 182 | | { |
| | 10 | 183 | | var assemblyList = assemblies as IReadOnlyCollection<Assembly> ?? assemblies.ToArray(); |
| | 10 | 184 | | if (_allowAllWhenAssembliesEmpty && assemblyList.Count == 0) |
| | | 185 | | { |
| | 0 | 186 | | return CreatePlugins<TPlugin, TAttribute>(); |
| | | 187 | | } |
| | | 188 | | |
| | 10 | 189 | | var assemblySet = assemblyList |
| | 10 | 190 | | .Select(a => a.GetName().Name) |
| | 10 | 191 | | .Where(n => n is not null) |
| | 10 | 192 | | .ToHashSet(StringComparer.Ordinal)!; |
| | | 193 | | |
| | 10 | 194 | | var pluginType = typeof(TPlugin); |
| | 10 | 195 | | return _lazyPlugins.Value |
| | 10 | 196 | | .Where(info => |
| | 10 | 197 | | { |
| | 10 | 198 | | // Check if this plugin is from one of the specified assemblies |
| | 811 | 199 | | var pluginAssemblyName = info.PluginType.Assembly.GetName().Name; |
| | 811 | 200 | | if (pluginAssemblyName is null || !assemblySet.Contains(pluginAssemblyName)) |
| | 72 | 201 | | return false; |
| | 10 | 202 | | |
| | 739 | 203 | | if (!pluginType.IsAssignableFrom(info.PluginType)) |
| | 657 | 204 | | return false; |
| | 10 | 205 | | |
| | 10 | 206 | | // Use pre-computed attribute info - no reflection needed |
| | 82 | 207 | | return info.HasAttribute<TAttribute>(); |
| | 10 | 208 | | }) |
| | 33 | 209 | | .Select(info => (TPlugin)info.Factory()); |
| | | 210 | | } |
| | | 211 | | } |