| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.Generators; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Runtime bootstrap registry for source-generated Needlr components. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The source generator emits a module initializer in the host assembly that calls |
| | | 13 | | /// one of the Register overloads with the generated TypeRegistry providers. |
| | | 14 | | /// Needlr runtime can then discover generated registries without any runtime reflection. |
| | | 15 | | /// </remarks> |
| | | 16 | | public static class NeedlrSourceGenBootstrap |
| | | 17 | | { |
| | | 18 | | private sealed class Registration |
| | | 19 | | { |
| | 36 | 20 | | public Registration( |
| | 36 | 21 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | 36 | 22 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | 36 | 23 | | Action<object>? decoratorApplier = null, |
| | 36 | 24 | | Action<object, object>? optionsRegistrar = null) |
| | | 25 | | { |
| | 36 | 26 | | InjectableTypeProvider = injectableTypeProvider; |
| | 36 | 27 | | PluginTypeProvider = pluginTypeProvider; |
| | 36 | 28 | | DecoratorApplier = decoratorApplier; |
| | 36 | 29 | | OptionsRegistrar = optionsRegistrar; |
| | 36 | 30 | | } |
| | | 31 | | |
| | 189 | 32 | | public Func<IReadOnlyList<InjectableTypeInfo>> InjectableTypeProvider { get; } |
| | 189 | 33 | | public Func<IReadOnlyList<PluginTypeInfo>> PluginTypeProvider { get; } |
| | 266 | 34 | | public Action<object>? DecoratorApplier { get; } |
| | 9 | 35 | | public Action<object, object>? OptionsRegistrar { get; } |
| | | 36 | | } |
| | | 37 | | |
| | 17 | 38 | | private static readonly object _gate = new object(); |
| | 17 | 39 | | private static readonly List<Registration> _registrations = new List<Registration>(); |
| | 17 | 40 | | private static readonly List<Action<object, object>> _extensionRegistrars = new List<Action<object, object>>(); |
| | | 41 | | |
| | 17 | 42 | | private static readonly AsyncLocal<Registration?> _asyncLocalOverride = new AsyncLocal<Registration?>(); |
| | | 43 | | |
| | | 44 | | private static Registration? _cachedCombined; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Registers the generated type and plugin providers for this application. |
| | | 48 | | /// </summary> |
| | | 49 | | public static void Register( |
| | | 50 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 51 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 52 | | { |
| | 0 | 53 | | Register(injectableTypeProvider, pluginTypeProvider, (Action<object>?)null); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Registers the generated type, plugin, and decorator providers for this application. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="injectableTypeProvider">Provider for injectable types.</param> |
| | | 60 | | /// <param name="pluginTypeProvider">Provider for plugin types.</param> |
| | | 61 | | /// <param name="decoratorApplier"> |
| | | 62 | | /// Action that applies decorators to the service collection. |
| | | 63 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 64 | | /// </param> |
| | | 65 | | public static void Register( |
| | | 66 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 67 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 68 | | Action<object>? decoratorApplier) |
| | | 69 | | { |
| | 0 | 70 | | Register(injectableTypeProvider, pluginTypeProvider, decoratorApplier, null); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Registers the generated type, plugin, decorator, and options providers for this application. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="injectableTypeProvider">Provider for injectable types.</param> |
| | | 77 | | /// <param name="pluginTypeProvider">Provider for plugin types.</param> |
| | | 78 | | /// <param name="decoratorApplier"> |
| | | 79 | | /// Action that applies decorators to the service collection. |
| | | 80 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 81 | | /// </param> |
| | | 82 | | /// <param name="optionsRegistrar"> |
| | | 83 | | /// Action that registers options with the service collection and configuration. |
| | | 84 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 85 | | /// </param> |
| | | 86 | | public static void Register( |
| | | 87 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 88 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 89 | | Action<object>? decoratorApplier, |
| | | 90 | | Action<object, object>? optionsRegistrar) |
| | | 91 | | { |
| | 21 | 92 | | if (injectableTypeProvider is null) throw new ArgumentNullException(nameof(injectableTypeProvider)); |
| | 21 | 93 | | if (pluginTypeProvider is null) throw new ArgumentNullException(nameof(pluginTypeProvider)); |
| | | 94 | | |
| | 21 | 95 | | lock (_gate) |
| | | 96 | | { |
| | 21 | 97 | | _registrations.Add(new Registration(injectableTypeProvider, pluginTypeProvider, decoratorApplier, optionsReg |
| | 21 | 98 | | _cachedCombined = null; |
| | 21 | 99 | | } |
| | 21 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Registers an extension that provides additional service registrations. |
| | | 104 | | /// Extensions are invoked after the main options registrar during BuildServiceProvider. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="extensionRegistrar"> |
| | | 107 | | /// Action that registers extension services with the service collection and configuration. |
| | | 108 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 109 | | /// </param> |
| | | 110 | | /// <remarks> |
| | | 111 | | /// Use this method from extension package module initializers to register additional services. |
| | | 112 | | /// For example, FluentValidation can register its validators without modifying core Needlr. |
| | | 113 | | /// </remarks> |
| | | 114 | | public static void RegisterExtension(Action<object, object> extensionRegistrar) |
| | | 115 | | { |
| | 0 | 116 | | if (extensionRegistrar is null) throw new ArgumentNullException(nameof(extensionRegistrar)); |
| | | 117 | | |
| | 0 | 118 | | lock (_gate) |
| | | 119 | | { |
| | 0 | 120 | | _extensionRegistrars.Add(extensionRegistrar); |
| | 0 | 121 | | _cachedCombined = null; |
| | 0 | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets the registered providers (if any). |
| | | 127 | | /// </summary> |
| | | 128 | | public static bool TryGetProviders( |
| | | 129 | | out Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 130 | | out Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 131 | | { |
| | 186 | 132 | | var local = _asyncLocalOverride.Value; |
| | 186 | 133 | | if (local is not null) |
| | | 134 | | { |
| | 9 | 135 | | injectableTypeProvider = local.InjectableTypeProvider; |
| | 9 | 136 | | pluginTypeProvider = local.PluginTypeProvider; |
| | 9 | 137 | | return true; |
| | | 138 | | } |
| | | 139 | | |
| | 177 | 140 | | lock (_gate) |
| | | 141 | | { |
| | 177 | 142 | | if (_registrations.Count == 0) |
| | | 143 | | { |
| | 4 | 144 | | injectableTypeProvider = null!; |
| | 4 | 145 | | pluginTypeProvider = null!; |
| | 4 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 173 | 149 | | if (_cachedCombined is null) |
| | | 150 | | { |
| | 5 | 151 | | _cachedCombined = Combine(_registrations); |
| | | 152 | | } |
| | | 153 | | |
| | 173 | 154 | | injectableTypeProvider = _cachedCombined.InjectableTypeProvider; |
| | 173 | 155 | | pluginTypeProvider = _cachedCombined.PluginTypeProvider; |
| | 173 | 156 | | return true; |
| | | 157 | | } |
| | 177 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the decorator applier (if any). |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="decoratorApplier"> |
| | | 164 | | /// Action that applies decorators to the service collection. |
| | | 165 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 166 | | /// </param> |
| | | 167 | | /// <returns>True if a decorator applier is registered.</returns> |
| | | 168 | | public static bool TryGetDecoratorApplier(out Action<object>? decoratorApplier) |
| | | 169 | | { |
| | 258 | 170 | | var local = _asyncLocalOverride.Value; |
| | 258 | 171 | | if (local is not null) |
| | | 172 | | { |
| | 0 | 173 | | decoratorApplier = local.DecoratorApplier; |
| | 0 | 174 | | return decoratorApplier is not null; |
| | | 175 | | } |
| | | 176 | | |
| | 258 | 177 | | lock (_gate) |
| | | 178 | | { |
| | 258 | 179 | | if (_registrations.Count == 0) |
| | | 180 | | { |
| | 6 | 181 | | decoratorApplier = null; |
| | 6 | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | 252 | 185 | | if (_cachedCombined is null) |
| | | 186 | | { |
| | 0 | 187 | | _cachedCombined = Combine(_registrations); |
| | | 188 | | } |
| | | 189 | | |
| | 252 | 190 | | decoratorApplier = _cachedCombined.DecoratorApplier; |
| | 252 | 191 | | return decoratorApplier is not null; |
| | | 192 | | } |
| | 258 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Gets the options registrar (if any). |
| | | 197 | | /// </summary> |
| | | 198 | | /// <param name="optionsRegistrar"> |
| | | 199 | | /// Action that registers options with the service collection and configuration. |
| | | 200 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 201 | | /// </param> |
| | | 202 | | /// <returns>True if an options registrar is registered.</returns> |
| | | 203 | | public static bool TryGetOptionsRegistrar(out Action<object, object>? optionsRegistrar) |
| | | 204 | | { |
| | 0 | 205 | | var local = _asyncLocalOverride.Value; |
| | 0 | 206 | | if (local is not null) |
| | | 207 | | { |
| | 0 | 208 | | optionsRegistrar = local.OptionsRegistrar; |
| | 0 | 209 | | return optionsRegistrar is not null; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | lock (_gate) |
| | | 213 | | { |
| | 0 | 214 | | if (_registrations.Count == 0) |
| | | 215 | | { |
| | 0 | 216 | | optionsRegistrar = null; |
| | 0 | 217 | | return false; |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | if (_cachedCombined is null) |
| | | 221 | | { |
| | 0 | 222 | | _cachedCombined = Combine(_registrations); |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | optionsRegistrar = _cachedCombined.OptionsRegistrar; |
| | 0 | 226 | | return optionsRegistrar is not null; |
| | | 227 | | } |
| | 0 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Gets the combined extension registrar (if any extensions are registered). |
| | | 232 | | /// </summary> |
| | | 233 | | /// <param name="extensionRegistrar"> |
| | | 234 | | /// Combined action that invokes all registered extensions. |
| | | 235 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 236 | | /// </param> |
| | | 237 | | /// <returns>True if any extension registrars are registered.</returns> |
| | | 238 | | public static bool TryGetExtensionRegistrar(out Action<object, object>? extensionRegistrar) |
| | | 239 | | { |
| | 0 | 240 | | lock (_gate) |
| | | 241 | | { |
| | 0 | 242 | | if (_extensionRegistrars.Count == 0) |
| | | 243 | | { |
| | 0 | 244 | | extensionRegistrar = null; |
| | 0 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | var registrars = _extensionRegistrars.ToArray(); |
| | 0 | 249 | | extensionRegistrar = (services, config) => |
| | 0 | 250 | | { |
| | 0 | 251 | | foreach (var registrar in registrars) |
| | 0 | 252 | | { |
| | 0 | 253 | | registrar(services, config); |
| | 0 | 254 | | } |
| | 0 | 255 | | }; |
| | 0 | 256 | | return true; |
| | | 257 | | } |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | internal static IDisposable BeginTestScope( |
| | | 261 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 262 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 263 | | { |
| | 10 | 264 | | if (injectableTypeProvider is null) throw new ArgumentNullException(nameof(injectableTypeProvider)); |
| | 10 | 265 | | if (pluginTypeProvider is null) throw new ArgumentNullException(nameof(pluginTypeProvider)); |
| | | 266 | | |
| | 10 | 267 | | var prior = _asyncLocalOverride.Value; |
| | 10 | 268 | | _asyncLocalOverride.Value = new Registration(injectableTypeProvider, pluginTypeProvider); |
| | 10 | 269 | | return new Scope(prior); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private sealed class Scope : IDisposable |
| | | 273 | | { |
| | | 274 | | private readonly Registration? _prior; |
| | | 275 | | |
| | 10 | 276 | | public Scope(Registration? prior) |
| | | 277 | | { |
| | 10 | 278 | | _prior = prior; |
| | 10 | 279 | | } |
| | | 280 | | |
| | | 281 | | public void Dispose() |
| | | 282 | | { |
| | 10 | 283 | | _asyncLocalOverride.Value = _prior; |
| | 10 | 284 | | } |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | private static Registration Combine(IReadOnlyList<Registration> registrations) |
| | | 288 | | { |
| | | 289 | | // Snapshot the current registrations to avoid capturing a mutable List. |
| | 12 | 290 | | var injectableProviders = registrations.Select(r => r.InjectableTypeProvider).ToArray(); |
| | 12 | 291 | | var pluginProviders = registrations.Select(r => r.PluginTypeProvider).ToArray(); |
| | 19 | 292 | | var decoratorAppliers = registrations.Where(r => r.DecoratorApplier is not null).Select(r => r.DecoratorApplier! |
| | 14 | 293 | | var optionsRegistrars = registrations.Where(r => r.OptionsRegistrar is not null).Select(r => r.OptionsRegistrar! |
| | | 294 | | |
| | | 295 | | IReadOnlyList<InjectableTypeInfo> GetInjectableTypes() |
| | | 296 | | { |
| | | 297 | | var result = new List<InjectableTypeInfo>(); |
| | | 298 | | var seen = new HashSet<Type>(); |
| | | 299 | | |
| | | 300 | | foreach (var provider in injectableProviders) |
| | | 301 | | { |
| | | 302 | | foreach (var info in provider()) |
| | | 303 | | { |
| | | 304 | | if (seen.Add(info.Type)) |
| | | 305 | | { |
| | | 306 | | result.Add(info); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | return result; |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | IReadOnlyList<PluginTypeInfo> GetPluginTypes() |
| | | 315 | | { |
| | | 316 | | var result = new List<PluginTypeInfo>(); |
| | | 317 | | var seen = new HashSet<Type>(); |
| | | 318 | | |
| | | 319 | | foreach (var provider in pluginProviders) |
| | | 320 | | { |
| | | 321 | | foreach (var info in provider()) |
| | | 322 | | { |
| | | 323 | | if (seen.Add(info.PluginType)) |
| | | 324 | | { |
| | | 325 | | result.Add(info); |
| | | 326 | | } |
| | | 327 | | } |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | return result; |
| | | 331 | | } |
| | | 332 | | |
| | 5 | 333 | | Action<object>? combinedDecoratorApplier = decoratorAppliers.Length > 0 |
| | 5 | 334 | | ? services => |
| | 5 | 335 | | { |
| | 1022 | 336 | | foreach (var applier in decoratorAppliers) |
| | 5 | 337 | | { |
| | 259 | 338 | | applier(services); |
| | 5 | 339 | | } |
| | 252 | 340 | | } |
| | 5 | 341 | | : null; |
| | | 342 | | |
| | 5 | 343 | | Action<object, object>? combinedOptionsRegistrar = optionsRegistrars.Length > 0 |
| | 5 | 344 | | ? (services, config) => |
| | 5 | 345 | | { |
| | 0 | 346 | | foreach (var registrar in optionsRegistrars) |
| | 5 | 347 | | { |
| | 0 | 348 | | registrar(services, config); |
| | 5 | 349 | | } |
| | 0 | 350 | | } |
| | 5 | 351 | | : null; |
| | | 352 | | |
| | 5 | 353 | | return new Registration(GetInjectableTypes, GetPluginTypes, combinedDecoratorApplier, combinedOptionsRegistrar); |
| | | 354 | | } |
| | | 355 | | } |