| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | using Microsoft.CodeAnalysis; |
| | | 9 | | |
| | | 10 | | using NexusLabs.Needlr.Generators.Models; |
| | | 11 | | |
| | | 12 | | namespace NexusLabs.Needlr.Generators.CodeGen; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Generates decorator registration, hosted service registration, |
| | | 16 | | /// provider registration, and open-decorator expansion code. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class DecoratorsCodeGenerator |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Emits the <c>ApplyDecorators(IServiceCollection)</c> method body into the TypeRegistry class. |
| | | 22 | | /// </summary> |
| | | 23 | | internal static void GenerateApplyDecoratorsMethod(StringBuilder builder, IReadOnlyList<DiscoveredDecorator> decorat |
| | | 24 | | { |
| | 455 | 25 | | builder.AppendLine(" /// <summary>"); |
| | 455 | 26 | | builder.AppendLine(" /// Applies all discovered decorators, interceptors, and hosted services to the service |
| | 455 | 27 | | builder.AppendLine(" /// Decorators are applied in order, with lower Order values applied first (closer to th |
| | 455 | 28 | | builder.AppendLine(" /// </summary>"); |
| | 455 | 29 | | builder.AppendLine(" /// <param name=\"services\">The service collection to apply decorators to.</param>"); |
| | 455 | 30 | | builder.AppendLine(" public static void ApplyDecorators(IServiceCollection services)"); |
| | 455 | 31 | | builder.AppendLine(" {"); |
| | | 32 | | |
| | | 33 | | // Register ServiceCatalog first |
| | 455 | 34 | | breadcrumbs.WriteInlineComment(builder, " ", "Register service catalog for DI resolution"); |
| | 455 | 35 | | builder.AppendLine($" services.AddSingleton<global::NexusLabs.Needlr.Catalog.IServiceCatalog, global::{sa |
| | 455 | 36 | | builder.AppendLine(); |
| | | 37 | | |
| | | 38 | | // Register hosted services first (before decorators apply) |
| | 455 | 39 | | if (hasHostedServices) |
| | | 40 | | { |
| | 6 | 41 | | breadcrumbs.WriteInlineComment(builder, " ", "Register hosted services"); |
| | 6 | 42 | | builder.AppendLine(" RegisterHostedServices(services);"); |
| | 6 | 43 | | if (decorators.Count > 0 || hasInterceptors) |
| | | 44 | | { |
| | 0 | 45 | | builder.AppendLine(); |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | 455 | 49 | | if (decorators.Count == 0 && !hasInterceptors) |
| | | 50 | | { |
| | 424 | 51 | | if (!hasHostedServices) |
| | | 52 | | { |
| | 418 | 53 | | breadcrumbs.WriteInlineComment(builder, " ", "No decorators, interceptors, or hosted services dis |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 31 | 58 | | if (decorators.Count > 0) |
| | | 59 | | { |
| | | 60 | | // Group decorators by service type and order by Order property |
| | 17 | 61 | | var decoratorsByService = decorators |
| | 26 | 62 | | .GroupBy(d => d.ServiceTypeName) |
| | 37 | 63 | | .OrderBy(g => g.Key); |
| | | 64 | | |
| | 74 | 65 | | foreach (var serviceGroup in decoratorsByService) |
| | | 66 | | { |
| | | 67 | | // Write verbose breadcrumb for decorator chain |
| | 20 | 68 | | if (breadcrumbs.Level == BreadcrumbLevel.Verbose) |
| | | 69 | | { |
| | 0 | 70 | | var chainItems = serviceGroup.OrderBy(d => d.Order).ToList(); |
| | 0 | 71 | | var lines = new List<string> |
| | 0 | 72 | | { |
| | 0 | 73 | | "Resolution order (outer → inner → target):" |
| | 0 | 74 | | }; |
| | 0 | 75 | | for (int i = 0; i < chainItems.Count; i++) |
| | | 76 | | { |
| | 0 | 77 | | var dec = chainItems[i]; |
| | 0 | 78 | | var sourcePath = dec.SourceFilePath != null |
| | 0 | 79 | | ? BreadcrumbWriter.GetRelativeSourcePath(dec.SourceFilePath, projectDirectory) |
| | 0 | 80 | | : $"[{dec.AssemblyName}]"; |
| | 0 | 81 | | lines.Add($" {i + 1}. {dec.DecoratorTypeName.Split('.').Last()} (Order={dec.Order}) ← {sour |
| | | 82 | | } |
| | 0 | 83 | | lines.Add($"Triggered by: [DecoratorFor<{serviceGroup.Key.Split('.').Last()}>] attributes"); |
| | | 84 | | |
| | 0 | 85 | | breadcrumbs.WriteVerboseBox(builder, " ", |
| | 0 | 86 | | $"Decorator Chain: {serviceGroup.Key.Split('.').Last()}", |
| | 0 | 87 | | lines.ToArray()); |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | 20 | 91 | | breadcrumbs.WriteInlineComment(builder, " ", $"Decorators for {serviceGroup.Key}"); |
| | | 92 | | } |
| | | 93 | | |
| | 118 | 94 | | foreach (var decorator in serviceGroup.OrderBy(d => d.Order)) |
| | | 95 | | { |
| | 26 | 96 | | builder.AppendLine($" services.AddDecorator<{decorator.ServiceTypeName}, {decorator.Decor |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 31 | 101 | | if (hasInterceptors) |
| | | 102 | | { |
| | 14 | 103 | | builder.AppendLine(); |
| | 14 | 104 | | breadcrumbs.WriteInlineComment(builder, " ", "Register intercepted services with their proxies"); |
| | 14 | 105 | | builder.AppendLine($" global::{safeAssemblyName}.Generated.InterceptorRegistrations.RegisterInter |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | 455 | 109 | | builder.AppendLine(" }"); |
| | 455 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Emits the <c>RegisterHostedServices(IServiceCollection)</c> method body. |
| | | 114 | | /// </summary> |
| | | 115 | | internal static void GenerateRegisterHostedServicesMethod(StringBuilder builder, IReadOnlyList<DiscoveredHostedServi |
| | | 116 | | { |
| | 6 | 117 | | builder.AppendLine(" /// <summary>"); |
| | 6 | 118 | | builder.AppendLine(" /// Registers all discovered hosted services (BackgroundService and IHostedService imple |
| | 6 | 119 | | builder.AppendLine(" /// Each service is registered as singleton and also as IHostedService for the host to d |
| | 6 | 120 | | builder.AppendLine(" /// </summary>"); |
| | 6 | 121 | | builder.AppendLine(" /// <param name=\"services\">The service collection to register to.</param>"); |
| | 6 | 122 | | builder.AppendLine(" private static void RegisterHostedServices(IServiceCollection services)"); |
| | 6 | 123 | | builder.AppendLine(" {"); |
| | | 124 | | |
| | 26 | 125 | | foreach (var hostedService in hostedServices) |
| | | 126 | | { |
| | 7 | 127 | | var typeName = hostedService.TypeName; |
| | 7 | 128 | | var shortName = typeName.Split('.').Last(); |
| | 7 | 129 | | var sourcePath = hostedService.SourceFilePath != null |
| | 7 | 130 | | ? BreadcrumbWriter.GetRelativeSourcePath(hostedService.SourceFilePath, projectDirectory) |
| | 7 | 131 | | : $"[{hostedService.AssemblyName}]"; |
| | | 132 | | |
| | 7 | 133 | | breadcrumbs.WriteInlineComment(builder, " ", $"Hosted service: {shortName} ← {sourcePath}"); |
| | | 134 | | |
| | | 135 | | // Register the concrete type as singleton |
| | 7 | 136 | | builder.AppendLine($" services.AddSingleton<{typeName}>();"); |
| | | 137 | | |
| | | 138 | | // Register as IHostedService that forwards to the concrete type |
| | 7 | 139 | | builder.AppendLine($" services.AddSingleton<global::Microsoft.Extensions.Hosting.IHostedService>(sp = |
| | | 140 | | } |
| | | 141 | | |
| | 6 | 142 | | builder.AppendLine(" }"); |
| | 6 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Expands open generic decorators into concrete decorator registrations |
| | | 147 | | /// for each discovered closed implementation of the open generic interface. |
| | | 148 | | /// </summary> |
| | | 149 | | internal static void ExpandOpenDecorators( |
| | | 150 | | IReadOnlyList<DiscoveredType> injectableTypes, |
| | | 151 | | IReadOnlyList<DiscoveredOpenDecorator> openDecorators, |
| | | 152 | | List<DiscoveredDecorator> decorators) |
| | | 153 | | { |
| | 40 | 154 | | foreach (var discoveredType in injectableTypes) |
| | | 155 | | { |
| | | 156 | | // We need to check each interface this type implements to see if it's a closed version of an open generic |
| | 60 | 157 | | foreach (var openDecorator in openDecorators) |
| | | 158 | | { |
| | | 159 | | // Check if this type implements the open generic interface |
| | 46 | 160 | | foreach (var interfaceName in discoveredType.InterfaceNames) |
| | | 161 | | { |
| | | 162 | | // This is string-based matching - we need to match the interface name pattern |
| | | 163 | | // For example, if open generic is IHandler<> and the interface is IHandler<Order>, we should match |
| | 7 | 164 | | var openGenericName = TypeDiscoveryHelper.GetFullyQualifiedName(openDecorator.OpenGenericInterface); |
| | | 165 | | |
| | | 166 | | // Extract the base name (before the <>) |
| | 7 | 167 | | var openGenericBaseName = GeneratorHelpers.GetGenericBaseName(openGenericName); |
| | 7 | 168 | | var interfaceBaseName = GeneratorHelpers.GetGenericBaseName(interfaceName); |
| | | 169 | | |
| | 7 | 170 | | if (openGenericBaseName == interfaceBaseName) |
| | | 171 | | { |
| | | 172 | | // This interface is a closed version of the open generic |
| | | 173 | | // Create a closed decorator registration |
| | 7 | 174 | | var closedDecoratorTypeName = GeneratorHelpers.CreateClosedGenericType( |
| | 7 | 175 | | TypeDiscoveryHelper.GetFullyQualifiedName(openDecorator.DecoratorType), |
| | 7 | 176 | | interfaceName, |
| | 7 | 177 | | openGenericName); |
| | | 178 | | |
| | 7 | 179 | | decorators.Add(new DiscoveredDecorator( |
| | 7 | 180 | | closedDecoratorTypeName, |
| | 7 | 181 | | interfaceName, |
| | 7 | 182 | | openDecorator.Order, |
| | 7 | 183 | | openDecorator.AssemblyName, |
| | 7 | 184 | | openDecorator.SourceFilePath)); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |
| | 6 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Emits the <c>RegisterProviders(IServiceCollection)</c> method body. |
| | | 193 | | /// </summary> |
| | | 194 | | internal static void GenerateRegisterProvidersMethod(StringBuilder builder, IReadOnlyList<DiscoveredProvider> provid |
| | | 195 | | { |
| | 17 | 196 | | builder.AppendLine(" /// <summary>"); |
| | 17 | 197 | | builder.AppendLine(" /// Registers all generated providers as Singletons."); |
| | 17 | 198 | | builder.AppendLine(" /// Providers are strongly-typed service locators that expose services via typed propert |
| | 17 | 199 | | builder.AppendLine(" /// </summary>"); |
| | 17 | 200 | | builder.AppendLine(" /// <param name=\"services\">The service collection to register to.</param>"); |
| | 17 | 201 | | builder.AppendLine(" public static void RegisterProviders(IServiceCollection services)"); |
| | 17 | 202 | | builder.AppendLine(" {"); |
| | | 203 | | |
| | 70 | 204 | | foreach (var provider in providers) |
| | | 205 | | { |
| | 18 | 206 | | var shortName = provider.SimpleTypeName; |
| | 18 | 207 | | var sourcePath = provider.SourceFilePath != null |
| | 18 | 208 | | ? BreadcrumbWriter.GetRelativeSourcePath(provider.SourceFilePath, projectDirectory) |
| | 18 | 209 | | : $"[{provider.AssemblyName}]"; |
| | | 210 | | |
| | 18 | 211 | | breadcrumbs.WriteInlineComment(builder, " ", $"Provider: {shortName} ← {sourcePath}"); |
| | | 212 | | |
| | 18 | 213 | | if (provider.IsInterface) |
| | | 214 | | { |
| | | 215 | | // Interface mode: register the generated implementation |
| | 12 | 216 | | var implName = provider.ImplementationTypeName; |
| | 12 | 217 | | builder.AppendLine($" services.AddSingleton<{provider.TypeName}, global::{safeAssemblyName}.Gener |
| | | 218 | | } |
| | 6 | 219 | | else if (provider.IsPartial) |
| | | 220 | | { |
| | | 221 | | // Shorthand class mode: register the partial class as its generated interface |
| | 6 | 222 | | var interfaceName = provider.InterfaceTypeName; |
| | 6 | 223 | | var providerNamespace = GeneratorHelpers.GetNamespaceFromTypeName(provider.TypeName); |
| | 6 | 224 | | builder.AppendLine($" services.AddSingleton<global::{providerNamespace}.{interfaceName}, {provide |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | 17 | 228 | | builder.AppendLine(" }"); |
| | 17 | 229 | | } |
| | | 230 | | } |