| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Generators; |
| | | 5 | | using NexusLabs.Needlr.Injection.SourceGen.PluginFactories; |
| | | 6 | | |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Injection.SourceGen; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builds and configures an <see cref="IServiceCollection"/> using source-generated plugin discovery. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// This builder uses <see cref="GeneratedPluginFactory"/> for plugin discovery and is AOT compatible. |
| | | 16 | | /// </remarks> |
| | | 17 | | [DoNotAutoRegister] |
| | | 18 | | public sealed class GeneratedServiceProviderBuilder : IServiceProviderBuilder |
| | | 19 | | { |
| | | 20 | | private readonly IServiceCollectionPopulator _serviceCollectionPopulator; |
| | | 21 | | private readonly IAssemblyProvider _assemblyProvider; |
| | | 22 | | private readonly GeneratedPluginFactory _pluginFactory; |
| | | 23 | | |
| | 245 | 24 | | public GeneratedServiceProviderBuilder( |
| | 245 | 25 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | 245 | 26 | | IAssemblyProvider assemblyProvider, |
| | 245 | 27 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 28 | | { |
| | 245 | 29 | | ArgumentNullException.ThrowIfNull(serviceCollectionPopulator); |
| | 245 | 30 | | ArgumentNullException.ThrowIfNull(assemblyProvider); |
| | 245 | 31 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | | 32 | | |
| | 245 | 33 | | _serviceCollectionPopulator = serviceCollectionPopulator; |
| | 245 | 34 | | _assemblyProvider = assemblyProvider; |
| | 245 | 35 | | _pluginFactory = new GeneratedPluginFactory(pluginTypeProvider, allowAllWhenAssembliesEmpty: true); |
| | 245 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() => |
| | 490 | 40 | | _assemblyProvider.GetCandidateAssemblies(); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public IServiceProvider Build( |
| | | 44 | | IConfiguration config) => |
| | 0 | 45 | | Build( |
| | 0 | 46 | | services: new ServiceCollection(), |
| | 0 | 47 | | config: config); |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public IServiceProvider Build( |
| | | 51 | | IServiceCollection services, |
| | | 52 | | IConfiguration config) => |
| | 0 | 53 | | Build( |
| | 0 | 54 | | services: services, |
| | 0 | 55 | | config: config, |
| | 0 | 56 | | postPluginRegistrationCallbacks: []); |
| | | 57 | | |
| | | 58 | | /// <inheritdoc/> |
| | | 59 | | public IServiceProvider Build( |
| | | 60 | | IServiceCollection services, |
| | | 61 | | IConfiguration config, |
| | | 62 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) => |
| | 0 | 63 | | Build(services, config, [], postPluginRegistrationCallbacks); |
| | | 64 | | |
| | | 65 | | /// <inheritdoc/> |
| | | 66 | | public IServiceProvider Build( |
| | | 67 | | IServiceCollection services, |
| | | 68 | | IConfiguration config, |
| | | 69 | | IReadOnlyList<Action<IServiceCollection>> preRegistrationCallbacks, |
| | | 70 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) |
| | | 71 | | { |
| | 245 | 72 | | ArgumentNullException.ThrowIfNull(services); |
| | 245 | 73 | | ArgumentNullException.ThrowIfNull(config); |
| | 245 | 74 | | ArgumentNullException.ThrowIfNull(preRegistrationCallbacks); |
| | 245 | 75 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallbacks); |
| | | 76 | | |
| | 245 | 77 | | services.AddSingleton(config); |
| | | 78 | | |
| | 508 | 79 | | foreach (var callback in preRegistrationCallbacks) |
| | | 80 | | { |
| | 9 | 81 | | callback.Invoke(services); |
| | | 82 | | } |
| | | 83 | | |
| | 245 | 84 | | _serviceCollectionPopulator.RegisterToServiceCollection( |
| | 245 | 85 | | services, |
| | 245 | 86 | | config, |
| | 245 | 87 | | GetCandidateAssemblies()); |
| | | 88 | | |
| | 1526 | 89 | | foreach (var callback in postPluginRegistrationCallbacks) |
| | | 90 | | { |
| | 518 | 91 | | callback.Invoke(services); |
| | | 92 | | } |
| | | 93 | | |
| | 245 | 94 | | var provider = services.BuildServiceProvider(); |
| | 245 | 95 | | ConfigurePostBuildServiceCollectionPlugins(provider, config); |
| | 245 | 96 | | return provider; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public void ConfigurePostBuildServiceCollectionPlugins( |
| | | 101 | | IServiceProvider provider, |
| | | 102 | | IConfiguration config) |
| | | 103 | | { |
| | 245 | 104 | | ArgumentNullException.ThrowIfNull(provider); |
| | 245 | 105 | | ArgumentNullException.ThrowIfNull(config); |
| | | 106 | | |
| | 245 | 107 | | var candidateAssemblies = GetCandidateAssemblies(); |
| | 245 | 108 | | PostBuildServiceCollectionPluginOptions options = new( |
| | 245 | 109 | | provider, |
| | 245 | 110 | | config, |
| | 245 | 111 | | candidateAssemblies, |
| | 245 | 112 | | _pluginFactory); |
| | | 113 | | |
| | 490 | 114 | | foreach (var plugin in _pluginFactory.CreatePlugins<IPostBuildServiceCollectionPlugin>()) |
| | | 115 | | { |
| | 0 | 116 | | plugin.Configure(options); |
| | | 117 | | } |
| | 245 | 118 | | } |
| | | 119 | | } |