| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Generators; |
| | | 5 | | using NexusLabs.Needlr.Injection.Reflection.PluginFactories; |
| | | 6 | | using NexusLabs.Needlr.Injection.SourceGen.PluginFactories; |
| | | 7 | | |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Reflection; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.Injection.Bundle; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builds and configures an <see cref="IServiceCollection"/>, scanning assemblies for injectable types and plugins. |
| | | 15 | | /// </summary> |
| | | 16 | | [DoNotAutoRegister] |
| | | 17 | | public sealed class ServiceProviderBuilder : IServiceProviderBuilder |
| | | 18 | | { |
| | | 19 | | private readonly IServiceCollectionPopulator _serviceCollectionPopulator; |
| | | 20 | | private readonly Lazy<IReadOnlyList<Assembly>> _lazyCandidateAssemblies; |
| | | 21 | | |
| | | 22 | | public ServiceProviderBuilder( |
| | | 23 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | | 24 | | IAssemblyProvider assemblyProvider) : |
| | 11 | 25 | | this( |
| | 11 | 26 | | serviceCollectionPopulator, |
| | 11 | 27 | | assemblyProvider, |
| | 11 | 28 | | additionalAssemblies: []) |
| | | 29 | | { |
| | 9 | 30 | | } |
| | | 31 | | |
| | 14 | 32 | | public ServiceProviderBuilder( |
| | 14 | 33 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | 14 | 34 | | IAssemblyProvider assemblyProvider, |
| | 14 | 35 | | IReadOnlyList<Assembly> additionalAssemblies) |
| | | 36 | | { |
| | 14 | 37 | | ArgumentNullException.ThrowIfNull(serviceCollectionPopulator); |
| | 13 | 38 | | ArgumentNullException.ThrowIfNull(assemblyProvider); |
| | 12 | 39 | | ArgumentNullException.ThrowIfNull(additionalAssemblies); |
| | | 40 | | |
| | 11 | 41 | | _serviceCollectionPopulator = serviceCollectionPopulator; |
| | 11 | 42 | | _lazyCandidateAssemblies = new(() => |
| | 11 | 43 | | { |
| | 7 | 44 | | var staticAssemblies = assemblyProvider.GetCandidateAssemblies(); |
| | 11 | 45 | | // Use assembly full name for deduplication instead of Location (Location is empty for single-file/AOT) |
| | 7 | 46 | | HashSet<string> uniqueAssemblyNames = new(StringComparer.OrdinalIgnoreCase); |
| | 7 | 47 | | List<Assembly> allCandidateAssemblies = new(additionalAssemblies.Count + staticAssemblies.Count); |
| | 11 | 48 | | |
| | 11 | 49 | | // load the static referenced assemblies |
| | 20 | 50 | | foreach (var assembly in staticAssemblies) |
| | 11 | 51 | | { |
| | 3 | 52 | | var name = assembly.FullName ?? assembly.GetName().Name ?? string.Empty; |
| | 3 | 53 | | if (uniqueAssemblyNames.Add(name)) |
| | 11 | 54 | | { |
| | 3 | 55 | | allCandidateAssemblies.Add(assembly); |
| | 11 | 56 | | } |
| | 11 | 57 | | } |
| | 11 | 58 | | |
| | 11 | 59 | | // load any additional |
| | 18 | 60 | | foreach (var assembly in additionalAssemblies) |
| | 11 | 61 | | { |
| | 2 | 62 | | var name = assembly.FullName ?? assembly.GetName().Name ?? string.Empty; |
| | 2 | 63 | | if (uniqueAssemblyNames.Add(name)) |
| | 11 | 64 | | { |
| | 1 | 65 | | allCandidateAssemblies.Add(assembly); |
| | 11 | 66 | | } |
| | 11 | 67 | | } |
| | 11 | 68 | | |
| | 7 | 69 | | return allCandidateAssemblies.Distinct().ToArray(); |
| | 11 | 70 | | }); |
| | 11 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | 12 | 74 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() => _lazyCandidateAssemblies.Value; |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public IServiceProvider Build( |
| | | 78 | | IConfiguration config) => |
| | 2 | 79 | | Build( |
| | 2 | 80 | | services: new ServiceCollection(), |
| | 2 | 81 | | config: config); |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public IServiceProvider Build( |
| | | 85 | | IServiceCollection services, |
| | | 86 | | IConfiguration config) => |
| | 6 | 87 | | Build( |
| | 6 | 88 | | services: services, |
| | 6 | 89 | | config: config, |
| | 6 | 90 | | postPluginRegistrationCallbacks: []); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | | 93 | | public IServiceProvider Build( |
| | | 94 | | IServiceCollection services, |
| | | 95 | | IConfiguration config, |
| | | 96 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) => |
| | 7 | 97 | | Build(services, config, [], postPluginRegistrationCallbacks); |
| | | 98 | | |
| | | 99 | | /// <inheritdoc/> |
| | | 100 | | public IServiceProvider Build( |
| | | 101 | | IServiceCollection services, |
| | | 102 | | IConfiguration config, |
| | | 103 | | IReadOnlyList<Action<IServiceCollection>> preRegistrationCallbacks, |
| | | 104 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) |
| | | 105 | | { |
| | 7 | 106 | | ArgumentNullException.ThrowIfNull(services); |
| | 6 | 107 | | ArgumentNullException.ThrowIfNull(config); |
| | 5 | 108 | | ArgumentNullException.ThrowIfNull(preRegistrationCallbacks); |
| | 5 | 109 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallbacks); |
| | | 110 | | |
| | 5 | 111 | | services.AddSingleton(config); |
| | | 112 | | |
| | 10 | 113 | | foreach (var callback in preRegistrationCallbacks) |
| | | 114 | | { |
| | 0 | 115 | | callback.Invoke(services); |
| | | 116 | | } |
| | | 117 | | |
| | 5 | 118 | | _serviceCollectionPopulator.RegisterToServiceCollection( |
| | 5 | 119 | | services, |
| | 5 | 120 | | config, |
| | 5 | 121 | | GetCandidateAssemblies()); |
| | | 122 | | |
| | 12 | 123 | | foreach (var callback in postPluginRegistrationCallbacks) |
| | | 124 | | { |
| | 1 | 125 | | callback.Invoke(services); |
| | | 126 | | } |
| | | 127 | | |
| | 5 | 128 | | var provider = services.BuildServiceProvider(); |
| | | 129 | | |
| | 5 | 130 | | ConfigurePostBuildServiceCollectionPlugins(provider, config); |
| | | 131 | | |
| | 5 | 132 | | return provider; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc /> |
| | | 136 | | [UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", |
| | | 137 | | Justification = "PluginFactory is only used as fallback when source-gen bootstrap is not present. AOT apps use s |
| | | 138 | | public void ConfigurePostBuildServiceCollectionPlugins( |
| | | 139 | | IServiceProvider provider, |
| | | 140 | | IConfiguration config) |
| | | 141 | | { |
| | 7 | 142 | | ArgumentNullException.ThrowIfNull(provider); |
| | 6 | 143 | | ArgumentNullException.ThrowIfNull(config); |
| | | 144 | | |
| | 5 | 145 | | var candidateAssemblies = GetCandidateAssemblies(); |
| | | 146 | | |
| | 5 | 147 | | IPluginFactory pluginFactory = NeedlrSourceGenBootstrap.TryGetProviders(out _, out var pluginTypeProvider) |
| | 5 | 148 | | ? new GeneratedPluginFactory(pluginTypeProvider, allowAllWhenAssembliesEmpty: true) |
| | 5 | 149 | | : new ReflectionPluginFactory(); |
| | | 150 | | |
| | 5 | 151 | | PostBuildServiceCollectionPluginOptions options = new( |
| | 5 | 152 | | provider, |
| | 5 | 153 | | config, |
| | 5 | 154 | | candidateAssemblies, |
| | 5 | 155 | | pluginFactory); |
| | | 156 | | |
| | 5 | 157 | | var executedPluginTypes = new HashSet<Type>(); |
| | 10 | 158 | | foreach (var plugin in pluginFactory.CreatePluginsFromAssemblies<IPostBuildServiceCollectionPlugin>(candidateAss |
| | | 159 | | { |
| | 0 | 160 | | var pluginType = plugin.GetType(); |
| | 0 | 161 | | if (!executedPluginTypes.Add(pluginType)) |
| | | 162 | | { |
| | | 163 | | // Skip duplicate plugin instances of the same type |
| | | 164 | | continue; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | plugin.Configure(options); |
| | | 168 | | } |
| | 5 | 169 | | } |
| | | 170 | | } |