| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Injection.SourceGen.Loaders; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// An assembly provider that derives assemblies from the generated TypeRegistry. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <para> |
| | | 12 | | /// When using source generation, the TypeRegistry contains all injectable types |
| | | 13 | | /// and plugins discovered at compile time. This provider extracts the unique |
| | | 14 | | /// assemblies from those types, enabling cross-assembly plugin discovery without |
| | | 15 | | /// runtime assembly scanning. |
| | | 16 | | /// </para> |
| | | 17 | | /// <para> |
| | | 18 | | /// This provider should be used with <see cref="PluginFactories.GeneratedPluginFactory"/> |
| | | 19 | | /// to ensure that all assemblies containing generated types are included in plugin discovery. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> after configuring the Syringe. |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | [DoNotAutoRegister] |
| | | 26 | | public sealed class GeneratedAssemblyProvider : IAssemblyProvider |
| | | 27 | | { |
| | | 28 | | private readonly Lazy<IReadOnlyList<Assembly>> _lazyAssemblies; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="GeneratedAssemblyProvider"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="injectableTypesProvider">A function that returns the injectable types.</param> |
| | | 34 | | /// <param name="pluginTypesProvider">A function that returns the plugin types.</param> |
| | 261 | 35 | | public GeneratedAssemblyProvider( |
| | 261 | 36 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypesProvider, |
| | 261 | 37 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypesProvider) |
| | | 38 | | { |
| | 261 | 39 | | ArgumentNullException.ThrowIfNull(injectableTypesProvider); |
| | 261 | 40 | | ArgumentNullException.ThrowIfNull(pluginTypesProvider); |
| | | 41 | | |
| | 261 | 42 | | _lazyAssemblies = new(() => |
| | 261 | 43 | | { |
| | 261 | 44 | | // In NativeAOT with reflection disabled, Type.Assembly can throw. |
| | 261 | 45 | | // Candidate assemblies are only a hint for reflection-based discovery. |
| | 261 | 46 | | // For source generation, the plugin/type registries already define the universe. |
| | 261 | 47 | | try |
| | 261 | 48 | | { |
| | 255 | 49 | | var assemblies = new HashSet<Assembly>(); |
| | 261 | 50 | | |
| | 261 | 51 | | // Extract assemblies from injectable types |
| | 73828 | 52 | | foreach (var info in injectableTypesProvider()) |
| | 261 | 53 | | { |
| | 36659 | 54 | | assemblies.Add(info.Type.Assembly); |
| | 261 | 55 | | } |
| | 261 | 56 | | |
| | 261 | 57 | | // Extract assemblies from plugin types |
| | 44302 | 58 | | foreach (var info in pluginTypesProvider()) |
| | 261 | 59 | | { |
| | 21896 | 60 | | assemblies.Add(info.PluginType.Assembly); |
| | 261 | 61 | | } |
| | 261 | 62 | | |
| | 255 | 63 | | return assemblies.ToList(); |
| | 261 | 64 | | } |
| | 0 | 65 | | catch (NotSupportedException) |
| | 261 | 66 | | { |
| | 0 | 67 | | return Array.Empty<Assembly>(); |
| | 261 | 68 | | } |
| | 516 | 69 | | }); |
| | 261 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() => |
| | 494 | 74 | | _lazyAssemblies.Value; |
| | | 75 | | } |