| | | 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 | | { |
| | 265 | 20 | | public Registration( |
| | 265 | 21 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | 265 | 22 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | 265 | 23 | | Action<object>? decoratorApplier = null, |
| | 265 | 24 | | Action<object, object>? optionsRegistrar = null) |
| | | 25 | | { |
| | 265 | 26 | | InjectableTypeProvider = injectableTypeProvider; |
| | 265 | 27 | | PluginTypeProvider = pluginTypeProvider; |
| | 265 | 28 | | DecoratorApplier = decoratorApplier; |
| | 265 | 29 | | OptionsRegistrar = optionsRegistrar; |
| | 265 | 30 | | } |
| | | 31 | | |
| | 2354 | 32 | | public Func<IReadOnlyList<InjectableTypeInfo>> InjectableTypeProvider { get; } |
| | 2354 | 33 | | public Func<IReadOnlyList<PluginTypeInfo>> PluginTypeProvider { get; } |
| | 2404 | 34 | | public Action<object>? DecoratorApplier { get; } |
| | 2070 | 35 | | public Action<object, object>? OptionsRegistrar { get; } |
| | | 36 | | } |
| | | 37 | | |
| | 29 | 38 | | private static readonly object _gate = new object(); |
| | 29 | 39 | | private static readonly List<Registration> _registrations = new List<Registration>(); |
| | 29 | 40 | | private static readonly List<Action<object, object>> _extensionRegistrars = new List<Action<object, object>>(); |
| | | 41 | | |
| | 29 | 42 | | private static readonly AsyncLocal<Registration?> _asyncLocalOverride = new AsyncLocal<Registration?>(); |
| | | 43 | | |
| | | 44 | | private static Registration? _cachedCombined; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Registers plugin types that were emitted by another source generator and are therefore |
| | | 48 | | /// invisible to <c>TypeRegistryGenerator</c> at compile time. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="pluginTypeProvider">Provider for the generator-emitted plugin types.</param> |
| | | 51 | | /// <remarks> |
| | | 52 | | /// <para> |
| | | 53 | | /// Roslyn source generators run in isolation — each generator receives the original |
| | | 54 | | /// compilation and cannot see types emitted by other generators. This means |
| | | 55 | | /// <c>TypeRegistryGenerator</c> cannot discover types produced by a second generator |
| | | 56 | | /// (e.g., a <c>CacheProviderGenerator</c> emitting <c>*CacheConfiguration</c> records). |
| | | 57 | | /// </para> |
| | | 58 | | /// <para> |
| | | 59 | | /// The solution is a runtime registration: the second generator emits a |
| | | 60 | | /// <c>[ModuleInitializer]</c> that calls <c>RegisterPlugins()</c>. Module initializers run |
| | | 61 | | /// before any user code, so by the time the application calls |
| | | 62 | | /// <c>IPluginFactory.CreatePluginsFromAssemblies<T>()</c> all providers are combined. |
| | | 63 | | /// </para> |
| | | 64 | | /// <para> |
| | | 65 | | /// Example of what the second generator should emit: |
| | | 66 | | /// <code> |
| | | 67 | | /// [ModuleInitializer] |
| | | 68 | | /// internal static void Initialize() |
| | | 69 | | /// { |
| | | 70 | | /// NeedlrSourceGenBootstrap.RegisterPlugins(() => |
| | | 71 | | /// [ |
| | | 72 | | /// new PluginTypeInfo( |
| | | 73 | | /// typeof(MyCacheConfiguration), |
| | | 74 | | /// [typeof(CacheConfiguration)], |
| | | 75 | | /// static () => new MyCacheConfiguration(), |
| | | 76 | | /// []) |
| | | 77 | | /// ]); |
| | | 78 | | /// } |
| | | 79 | | /// </code> |
| | | 80 | | /// </para> |
| | | 81 | | /// </remarks> |
| | | 82 | | public static void RegisterPlugins(Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 83 | | { |
| | 16 | 84 | | if (pluginTypeProvider is null) throw new ArgumentNullException(nameof(pluginTypeProvider)); |
| | 32 | 85 | | Register(() => Array.Empty<InjectableTypeInfo>(), pluginTypeProvider); |
| | 14 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Registers the generated type and plugin providers for this application. |
| | | 90 | | /// </summary> |
| | | 91 | | public static void Register( |
| | | 92 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 93 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 94 | | { |
| | 96 | 95 | | Register(injectableTypeProvider, pluginTypeProvider, (Action<object>?)null); |
| | 94 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Registers the generated type, plugin, and decorator providers for this application. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="injectableTypeProvider">Provider for injectable types.</param> |
| | | 102 | | /// <param name="pluginTypeProvider">Provider for plugin types.</param> |
| | | 103 | | /// <param name="decoratorApplier"> |
| | | 104 | | /// Action that applies decorators to the service collection. |
| | | 105 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 106 | | /// </param> |
| | | 107 | | public static void Register( |
| | | 108 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 109 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 110 | | Action<object>? decoratorApplier) |
| | | 111 | | { |
| | 101 | 112 | | Register(injectableTypeProvider, pluginTypeProvider, decoratorApplier, null); |
| | 99 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Registers the generated type, plugin, decorator, and options providers for this application. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="injectableTypeProvider">Provider for injectable types.</param> |
| | | 119 | | /// <param name="pluginTypeProvider">Provider for plugin types.</param> |
| | | 120 | | /// <param name="decoratorApplier"> |
| | | 121 | | /// Action that applies decorators to the service collection. |
| | | 122 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 123 | | /// </param> |
| | | 124 | | /// <param name="optionsRegistrar"> |
| | | 125 | | /// Action that registers options with the service collection and configuration. |
| | | 126 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 127 | | /// </param> |
| | | 128 | | public static void Register( |
| | | 129 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 130 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 131 | | Action<object>? decoratorApplier, |
| | | 132 | | Action<object, object>? optionsRegistrar) |
| | | 133 | | { |
| | 164 | 134 | | if (injectableTypeProvider is null) throw new ArgumentNullException(nameof(injectableTypeProvider)); |
| | 163 | 135 | | if (pluginTypeProvider is null) throw new ArgumentNullException(nameof(pluginTypeProvider)); |
| | | 136 | | |
| | 161 | 137 | | lock (_gate) |
| | | 138 | | { |
| | 161 | 139 | | _registrations.Add(new Registration(injectableTypeProvider, pluginTypeProvider, decoratorApplier, optionsReg |
| | 161 | 140 | | _cachedCombined = null; |
| | 161 | 141 | | } |
| | 161 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Registers an extension that provides additional service registrations. |
| | | 146 | | /// Extensions are invoked after the main options registrar during BuildServiceProvider. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="extensionRegistrar"> |
| | | 149 | | /// Action that registers extension services with the service collection and configuration. |
| | | 150 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 151 | | /// </param> |
| | | 152 | | /// <remarks> |
| | | 153 | | /// <para> |
| | | 154 | | /// Use this method from extension package module initializers to register additional services. |
| | | 155 | | /// For example, FluentValidation can register its validators without modifying core Needlr. |
| | | 156 | | /// </para> |
| | | 157 | | /// <para> |
| | | 158 | | /// Needlr's own runtime composition (<c>ConfiguredSyringe</c>, <c>WebApplicationSyringe</c>, |
| | | 159 | | /// and <c>MauiSyringe</c>) reads options and extension registrars from |
| | | 160 | | /// <c>NexusLabs.Needlr.SourceGenRegistry</c> so that <c>NexusLabs.Needlr.Injection</c> does not |
| | | 161 | | /// need a dependency on this assembly. Extension packages that already depend on |
| | | 162 | | /// <c>NexusLabs.Needlr.Generators.Attributes</c> may keep using this registry; extension |
| | | 163 | | /// packages that only depend on <c>NexusLabs.Needlr</c> should use |
| | | 164 | | /// <c>SourceGenRegistry.RegisterExtension</c> instead. |
| | | 165 | | /// </para> |
| | | 166 | | /// </remarks> |
| | | 167 | | public static void RegisterExtension(Action<object, object> extensionRegistrar) |
| | | 168 | | { |
| | 11 | 169 | | if (extensionRegistrar is null) throw new ArgumentNullException(nameof(extensionRegistrar)); |
| | | 170 | | |
| | 9 | 171 | | lock (_gate) |
| | | 172 | | { |
| | 9 | 173 | | _extensionRegistrars.Add(extensionRegistrar); |
| | 9 | 174 | | _cachedCombined = null; |
| | 9 | 175 | | } |
| | 9 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Gets the registered providers (if any). |
| | | 180 | | /// </summary> |
| | | 181 | | public static bool TryGetProviders( |
| | | 182 | | out Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 183 | | out Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 184 | | { |
| | 300 | 185 | | var local = _asyncLocalOverride.Value; |
| | 300 | 186 | | if (local is not null) |
| | | 187 | | { |
| | 13 | 188 | | injectableTypeProvider = local.InjectableTypeProvider; |
| | 13 | 189 | | pluginTypeProvider = local.PluginTypeProvider; |
| | 13 | 190 | | return true; |
| | | 191 | | } |
| | | 192 | | |
| | 287 | 193 | | lock (_gate) |
| | | 194 | | { |
| | 287 | 195 | | if (_registrations.Count == 0) |
| | | 196 | | { |
| | 7 | 197 | | injectableTypeProvider = null!; |
| | 7 | 198 | | pluginTypeProvider = null!; |
| | 7 | 199 | | return false; |
| | | 200 | | } |
| | | 201 | | |
| | 280 | 202 | | if (_cachedCombined is null) |
| | | 203 | | { |
| | 82 | 204 | | _cachedCombined = Combine(_registrations); |
| | | 205 | | } |
| | | 206 | | |
| | 280 | 207 | | injectableTypeProvider = _cachedCombined.InjectableTypeProvider; |
| | 280 | 208 | | pluginTypeProvider = _cachedCombined.PluginTypeProvider; |
| | 280 | 209 | | return true; |
| | | 210 | | } |
| | 287 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Gets the decorator applier (if any). |
| | | 215 | | /// </summary> |
| | | 216 | | /// <param name="decoratorApplier"> |
| | | 217 | | /// Action that applies decorators to the service collection. |
| | | 218 | | /// The parameter is an IServiceCollection, but typed as object to avoid dependency on Microsoft.Extensions.Dependen |
| | | 219 | | /// </param> |
| | | 220 | | /// <returns>True if a decorator applier is registered.</returns> |
| | | 221 | | public static bool TryGetDecoratorApplier(out Action<object>? decoratorApplier) |
| | | 222 | | { |
| | 326 | 223 | | var local = _asyncLocalOverride.Value; |
| | 326 | 224 | | if (local is not null) |
| | | 225 | | { |
| | 2 | 226 | | decoratorApplier = local.DecoratorApplier; |
| | 2 | 227 | | return decoratorApplier is not null; |
| | | 228 | | } |
| | | 229 | | |
| | 324 | 230 | | lock (_gate) |
| | | 231 | | { |
| | 324 | 232 | | if (_registrations.Count == 0) |
| | | 233 | | { |
| | 8 | 234 | | decoratorApplier = null; |
| | 8 | 235 | | return false; |
| | | 236 | | } |
| | | 237 | | |
| | 316 | 238 | | if (_cachedCombined is null) |
| | | 239 | | { |
| | 3 | 240 | | _cachedCombined = Combine(_registrations); |
| | | 241 | | } |
| | | 242 | | |
| | 316 | 243 | | decoratorApplier = _cachedCombined.DecoratorApplier; |
| | 316 | 244 | | return decoratorApplier is not null; |
| | | 245 | | } |
| | 324 | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Gets the options registrar (if any). |
| | | 250 | | /// </summary> |
| | | 251 | | /// <param name="optionsRegistrar"> |
| | | 252 | | /// Action that registers options with the service collection and configuration. |
| | | 253 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 254 | | /// </param> |
| | | 255 | | /// <returns>True if an options registrar is registered.</returns> |
| | | 256 | | /// <remarks> |
| | | 257 | | /// The options registrar is supplied by the generated module initializer through the |
| | | 258 | | /// four-argument <see cref="Register(Func{IReadOnlyList{InjectableTypeInfo}}, Func{IReadOnlyList{PluginTypeInfo}}, |
| | | 259 | | /// overload. Needlr's own runtime composition reads options registrars from |
| | | 260 | | /// <c>NexusLabs.Needlr.SourceGenRegistry</c>; this accessor exists for hosts that compose |
| | | 261 | | /// generated registrations directly against this assembly. |
| | | 262 | | /// </remarks> |
| | | 263 | | public static bool TryGetOptionsRegistrar(out Action<object, object>? optionsRegistrar) |
| | | 264 | | { |
| | 6 | 265 | | var local = _asyncLocalOverride.Value; |
| | 6 | 266 | | if (local is not null) |
| | | 267 | | { |
| | 1 | 268 | | optionsRegistrar = local.OptionsRegistrar; |
| | 1 | 269 | | return optionsRegistrar is not null; |
| | | 270 | | } |
| | | 271 | | |
| | 5 | 272 | | lock (_gate) |
| | | 273 | | { |
| | 5 | 274 | | if (_registrations.Count == 0) |
| | | 275 | | { |
| | 2 | 276 | | optionsRegistrar = null; |
| | 2 | 277 | | return false; |
| | | 278 | | } |
| | | 279 | | |
| | 3 | 280 | | if (_cachedCombined is null) |
| | | 281 | | { |
| | 3 | 282 | | _cachedCombined = Combine(_registrations); |
| | | 283 | | } |
| | | 284 | | |
| | 3 | 285 | | optionsRegistrar = _cachedCombined.OptionsRegistrar; |
| | 3 | 286 | | return optionsRegistrar is not null; |
| | | 287 | | } |
| | 5 | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Gets the combined extension registrar (if any extensions are registered). |
| | | 292 | | /// </summary> |
| | | 293 | | /// <param name="extensionRegistrar"> |
| | | 294 | | /// Combined action that invokes all registered extensions. |
| | | 295 | | /// Parameters are (IServiceCollection, IConfiguration), typed as object to avoid dependencies. |
| | | 296 | | /// </param> |
| | | 297 | | /// <returns>True if any extension registrars are registered.</returns> |
| | | 298 | | /// <remarks> |
| | | 299 | | /// The returned action invokes every registrar in registration order. Unlike |
| | | 300 | | /// <see cref="TryGetProviders"/>, extension registrars are not affected by test scopes. |
| | | 301 | | /// </remarks> |
| | | 302 | | public static bool TryGetExtensionRegistrar(out Action<object, object>? extensionRegistrar) |
| | | 303 | | { |
| | 7 | 304 | | lock (_gate) |
| | | 305 | | { |
| | 7 | 306 | | if (_extensionRegistrars.Count == 0) |
| | | 307 | | { |
| | 2 | 308 | | extensionRegistrar = null; |
| | 2 | 309 | | return false; |
| | | 310 | | } |
| | | 311 | | |
| | 5 | 312 | | var registrars = _extensionRegistrars.ToArray(); |
| | 5 | 313 | | extensionRegistrar = (services, config) => |
| | 5 | 314 | | { |
| | 20 | 315 | | foreach (var registrar in registrars) |
| | 5 | 316 | | { |
| | 6 | 317 | | registrar(services, config); |
| | 5 | 318 | | } |
| | 9 | 319 | | }; |
| | 5 | 320 | | return true; |
| | | 321 | | } |
| | 7 | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Clears every global registration, including extension registrars and the cached |
| | | 326 | | /// combined registration. For testing purposes only. |
| | | 327 | | /// </summary> |
| | | 328 | | internal static void ClearRegistrationsForTesting() |
| | | 329 | | { |
| | 94 | 330 | | lock (_gate) |
| | | 331 | | { |
| | 94 | 332 | | _registrations.Clear(); |
| | 94 | 333 | | _extensionRegistrars.Clear(); |
| | 94 | 334 | | _cachedCombined = null; |
| | 94 | 335 | | } |
| | 94 | 336 | | } |
| | | 337 | | |
| | | 338 | | internal static IDisposable BeginTestScope( |
| | | 339 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 340 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 341 | | { |
| | 19 | 342 | | if (injectableTypeProvider is null) throw new ArgumentNullException(nameof(injectableTypeProvider)); |
| | 18 | 343 | | if (pluginTypeProvider is null) throw new ArgumentNullException(nameof(pluginTypeProvider)); |
| | | 344 | | |
| | 16 | 345 | | var prior = _asyncLocalOverride.Value; |
| | 16 | 346 | | _asyncLocalOverride.Value = new Registration(injectableTypeProvider, pluginTypeProvider); |
| | 16 | 347 | | return new Scope(prior); |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | private sealed class Scope : IDisposable |
| | | 351 | | { |
| | | 352 | | private readonly Registration? _prior; |
| | | 353 | | |
| | 16 | 354 | | public Scope(Registration? prior) |
| | | 355 | | { |
| | 16 | 356 | | _prior = prior; |
| | 16 | 357 | | } |
| | | 358 | | |
| | | 359 | | public void Dispose() |
| | | 360 | | { |
| | 16 | 361 | | _asyncLocalOverride.Value = _prior; |
| | 16 | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | private static Registration Combine(IReadOnlyList<Registration> registrations) |
| | | 366 | | { |
| | | 367 | | // Snapshot the current registrations to avoid capturing a mutable List. |
| | 2149 | 368 | | var injectableProviders = registrations.Select(r => r.InjectableTypeProvider).ToArray(); |
| | 2149 | 369 | | var pluginProviders = registrations.Select(r => r.PluginTypeProvider).ToArray(); |
| | 2174 | 370 | | var decoratorAppliers = registrations.Where(r => r.DecoratorApplier is not null).Select(r => r.DecoratorApplier! |
| | 2154 | 371 | | var optionsRegistrars = registrations.Where(r => r.OptionsRegistrar is not null).Select(r => r.OptionsRegistrar! |
| | | 372 | | |
| | | 373 | | IReadOnlyList<InjectableTypeInfo> GetInjectableTypes() |
| | | 374 | | { |
| | | 375 | | var result = new List<InjectableTypeInfo>(); |
| | | 376 | | var seen = new HashSet<Type>(); |
| | | 377 | | |
| | | 378 | | foreach (var provider in injectableProviders) |
| | | 379 | | { |
| | | 380 | | foreach (var info in provider()) |
| | | 381 | | { |
| | | 382 | | if (seen.Add(info.Type)) |
| | | 383 | | { |
| | | 384 | | result.Add(info); |
| | | 385 | | } |
| | | 386 | | } |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | return result; |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | IReadOnlyList<PluginTypeInfo> GetPluginTypes() |
| | | 393 | | { |
| | | 394 | | var result = new List<PluginTypeInfo>(); |
| | | 395 | | var seen = new HashSet<Type>(); |
| | | 396 | | |
| | | 397 | | foreach (var provider in pluginProviders) |
| | | 398 | | { |
| | | 399 | | foreach (var info in provider()) |
| | | 400 | | { |
| | | 401 | | if (seen.Add(info.PluginType)) |
| | | 402 | | { |
| | | 403 | | result.Add(info); |
| | | 404 | | } |
| | | 405 | | } |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | return result; |
| | | 409 | | } |
| | | 410 | | |
| | 88 | 411 | | Action<object>? combinedDecoratorApplier = decoratorAppliers.Length > 0 |
| | 88 | 412 | | ? services => |
| | 88 | 413 | | { |
| | 1290 | 414 | | foreach (var applier in decoratorAppliers) |
| | 88 | 415 | | { |
| | 330 | 416 | | applier(services); |
| | 88 | 417 | | } |
| | 315 | 418 | | } |
| | 88 | 419 | | : null; |
| | | 420 | | |
| | 88 | 421 | | Action<object, object>? combinedOptionsRegistrar = optionsRegistrars.Length > 0 |
| | 88 | 422 | | ? (services, config) => |
| | 88 | 423 | | { |
| | 10 | 424 | | foreach (var registrar in optionsRegistrars) |
| | 88 | 425 | | { |
| | 3 | 426 | | registrar(services, config); |
| | 88 | 427 | | } |
| | 2 | 428 | | } |
| | 88 | 429 | | : null; |
| | | 430 | | |
| | 88 | 431 | | return new Registration(GetInjectableTypes, GetPluginTypes, combinedDecoratorApplier, combinedOptionsRegistrar); |
| | | 432 | | } |
| | | 433 | | } |