| | | 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. Generated bootstraps also provide a |
| | | 14 | | /// registry participant type so assemblies with no injectable types or plugins |
| | | 15 | | /// remain available without runtime assembly scanning. |
| | | 16 | | /// </para> |
| | | 17 | | /// <para> |
| | | 18 | | /// This provider should be used with <see cref="PluginFactories.GeneratedPluginFactory"/> |
| | | 19 | | /// to ensure that all generated registry participants are included in plugin discovery |
| | | 20 | | /// when runtime assembly metadata is available. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> after configuring the Syringe. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | [DoNotAutoRegister] |
| | | 27 | | public sealed class GeneratedAssemblyProvider : IAssemblyProvider |
| | | 28 | | { |
| | | 29 | | private readonly Lazy<IReadOnlyList<Assembly>> _lazyAssemblies; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="GeneratedAssemblyProvider"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="injectableTypesProvider">A function that returns the injectable types.</param> |
| | | 35 | | /// <param name="pluginTypesProvider">A function that returns the plugin types.</param> |
| | | 36 | | /// <remarks> |
| | | 37 | | /// This compatibility overload can derive assemblies only from injectable and plugin metadata. |
| | | 38 | | /// Use the three-argument overload when generated registry participant types are available. |
| | | 39 | | /// </remarks> |
| | | 40 | | public GeneratedAssemblyProvider( |
| | | 41 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypesProvider, |
| | | 42 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypesProvider) |
| | 0 | 43 | | : this( |
| | 0 | 44 | | injectableTypesProvider, |
| | 0 | 45 | | pluginTypesProvider, |
| | 0 | 46 | | Array.Empty<Type>()) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="GeneratedAssemblyProvider"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="injectableTypesProvider">A function that returns the injectable types.</param> |
| | | 54 | | /// <param name="pluginTypesProvider">A function that returns the plugin types.</param> |
| | | 55 | | /// <param name="registryParticipantTypes"> |
| | | 56 | | /// Generated types whose assemblies identify every TypeRegistry participant. |
| | | 57 | | /// </param> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// Assemblies represented by injectable and plugin metadata preserve their existing order. |
| | | 60 | | /// Marker-only participant assemblies are appended in registration order. |
| | | 61 | | /// </remarks> |
| | 332 | 62 | | public GeneratedAssemblyProvider( |
| | 332 | 63 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypesProvider, |
| | 332 | 64 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypesProvider, |
| | 332 | 65 | | IReadOnlyList<Type> registryParticipantTypes) |
| | | 66 | | { |
| | 332 | 67 | | ArgumentNullException.ThrowIfNull(injectableTypesProvider); |
| | 332 | 68 | | ArgumentNullException.ThrowIfNull(pluginTypesProvider); |
| | 332 | 69 | | ArgumentNullException.ThrowIfNull(registryParticipantTypes); |
| | | 70 | | |
| | 331 | 71 | | var registryParticipantTypeSnapshot = registryParticipantTypes.ToArray(); |
| | 331 | 72 | | _lazyAssemblies = new(() => |
| | 331 | 73 | | { |
| | 331 | 74 | | // In NativeAOT with reflection disabled, Type.Assembly can throw. |
| | 331 | 75 | | // Candidate assemblies are only a hint for reflection-based discovery. |
| | 331 | 76 | | // For source generation, the plugin/type registries already define the universe. |
| | 331 | 77 | | try |
| | 331 | 78 | | { |
| | 325 | 79 | | var assemblies = new List<Assembly>(); |
| | 325 | 80 | | var seenAssemblies = new HashSet<Assembly>(); |
| | 331 | 81 | | |
| | 109644 | 82 | | foreach (var info in injectableTypesProvider()) |
| | 331 | 83 | | { |
| | 54497 | 84 | | AddAssembly(info.Type.Assembly); |
| | 331 | 85 | | } |
| | 331 | 86 | | |
| | 72550 | 87 | | foreach (var info in pluginTypesProvider()) |
| | 331 | 88 | | { |
| | 35950 | 89 | | AddAssembly(info.PluginType.Assembly); |
| | 331 | 90 | | } |
| | 331 | 91 | | |
| | 1102 | 92 | | foreach (var registryParticipantType in registryParticipantTypeSnapshot) |
| | 331 | 93 | | { |
| | 226 | 94 | | AddAssembly(registryParticipantType.Assembly); |
| | 331 | 95 | | } |
| | 331 | 96 | | |
| | 325 | 97 | | return assemblies; |
| | 331 | 98 | | |
| | 331 | 99 | | void AddAssembly(Assembly assembly) |
| | 331 | 100 | | { |
| | 90673 | 101 | | if (seenAssemblies.Add(assembly)) |
| | 331 | 102 | | { |
| | 1547 | 103 | | assemblies.Add(assembly); |
| | 331 | 104 | | } |
| | 90673 | 105 | | } |
| | 331 | 106 | | } |
| | 0 | 107 | | catch (NotSupportedException) |
| | 331 | 108 | | { |
| | 0 | 109 | | return Array.Empty<Assembly>(); |
| | 331 | 110 | | } |
| | 656 | 111 | | }); |
| | 331 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() => |
| | 628 | 116 | | _lazyAssemblies.Value; |
| | | 117 | | } |