| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | using NexusLabs.Needlr.Generators.Models; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Generators.CodeGen; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Generates the ServiceCatalog implementation from DiscoveryResult. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class ServiceCatalogCodeGenerator |
| | | 14 | | { |
| | | 15 | | internal static string GenerateServiceCatalogSource( |
| | | 16 | | GeneratedRegistrationPlan registrationPlan, |
| | | 17 | | string? projectDirectory, |
| | | 18 | | BreadcrumbWriter breadcrumbs) |
| | | 19 | | { |
| | 590 | 20 | | var builder = new StringBuilder(); |
| | 590 | 21 | | var discoveryResult = registrationPlan.DiscoveryResult; |
| | 590 | 22 | | var assemblyName = registrationPlan.AssemblyName; |
| | 590 | 23 | | var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName); |
| | | 24 | | |
| | 590 | 25 | | builder.AppendLine("// <auto-generated/>"); |
| | 590 | 26 | | builder.AppendLine("// Needlr Service Catalog"); |
| | 590 | 27 | | builder.AppendLine(); |
| | 590 | 28 | | builder.AppendLine("#nullable enable"); |
| | 590 | 29 | | builder.AppendLine(); |
| | 590 | 30 | | builder.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 590 | 31 | | builder.AppendLine(); |
| | | 32 | | |
| | 590 | 33 | | breadcrumbs.WriteInlineComment(builder, "", $"ServiceCatalog: {GeneratorHelpers.Literal(discoveryResult.Injectab |
| | | 34 | | |
| | 590 | 35 | | builder.AppendLine("/// <summary>"); |
| | 590 | 36 | | builder.AppendLine("/// Compile-time service catalog containing all discovered registrations."); |
| | 590 | 37 | | builder.AppendLine("/// </summary>"); |
| | 590 | 38 | | builder.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generators\", \"1 |
| | 590 | 39 | | builder.AppendLine($"public sealed class ServiceCatalog : global::NexusLabs.Needlr.Catalog.IServiceCatalog"); |
| | 590 | 40 | | builder.AppendLine("{"); |
| | 590 | 41 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 42 | | builder.AppendLine($" public string AssemblyName => \"{GeneratorHelpers.EscapeStringLiteral(assemblyName)}\"; |
| | 590 | 43 | | builder.AppendLine(); |
| | | 44 | | |
| | | 45 | | // Services |
| | 590 | 46 | | GenerateServicesProperty(builder, discoveryResult.InjectableTypes, projectDirectory); |
| | | 47 | | |
| | | 48 | | // Decorators |
| | 590 | 49 | | GenerateDecoratorsProperty(builder, discoveryResult.Decorators, projectDirectory); |
| | | 50 | | |
| | | 51 | | // Hosted Services |
| | 590 | 52 | | GenerateHostedServicesProperty(builder, discoveryResult.HostedServices, projectDirectory); |
| | | 53 | | |
| | | 54 | | // Intercepted Services |
| | 590 | 55 | | GenerateInterceptedServicesProperty(builder, discoveryResult.InterceptedServices, projectDirectory); |
| | | 56 | | |
| | | 57 | | // Options |
| | 590 | 58 | | GenerateOptionsProperty(builder, discoveryResult.Options, projectDirectory); |
| | | 59 | | |
| | | 60 | | // Plugins |
| | 590 | 61 | | GeneratePluginsProperty(builder, discoveryResult.PluginTypes, projectDirectory); |
| | | 62 | | |
| | 590 | 63 | | builder.AppendLine("}"); |
| | | 64 | | |
| | 590 | 65 | | return builder.ToString(); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private static void GenerateServicesProperty( |
| | | 69 | | StringBuilder builder, |
| | | 70 | | IReadOnlyList<DiscoveredType> types, |
| | | 71 | | string? projectDirectory) |
| | | 72 | | { |
| | 590 | 73 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 74 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 75 | | builder.AppendLine(" {"); |
| | | 76 | | |
| | 532794 | 77 | | foreach (var type in types) |
| | | 78 | | { |
| | 265807 | 79 | | var shortName = GeneratorHelpers.GetShortTypeName(type.TypeName); |
| | 265807 | 80 | | var lifetimeStr = type.Lifetime switch |
| | 265807 | 81 | | { |
| | 265776 | 82 | | GeneratorLifetime.Singleton => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Singleton", |
| | 25 | 83 | | GeneratorLifetime.Scoped => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped", |
| | 6 | 84 | | GeneratorLifetime.Transient => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Transient", |
| | 0 | 85 | | _ => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped" |
| | 265807 | 86 | | }; |
| | 265807 | 87 | | var sourceFilePath = GetRelativeSourcePath(type.SourceFilePath, projectDirectory); |
| | 265807 | 88 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 265807 | 89 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 265807 | 90 | | : "null"; |
| | | 91 | | |
| | | 92 | | // Build interfaces array (for backwards compat) |
| | 266228 | 93 | | var interfacesArray = $"new string[] {{ {string.Join(", ", type.InterfaceNames.Select(i => $"\"{GeneratorHel |
| | | 94 | | |
| | | 95 | | // Build interface entries array with locations |
| | 265807 | 96 | | var interfaceEntriesBuilder = new StringBuilder(); |
| | 265807 | 97 | | interfaceEntriesBuilder.Append("new global::NexusLabs.Needlr.Catalog.InterfaceEntry[] { "); |
| | 532456 | 98 | | foreach (var ifaceInfo in type.InterfaceInfos) |
| | | 99 | | { |
| | 421 | 100 | | var ifaceFilePath = GetRelativeSourcePath(ifaceInfo.SourceFilePath, projectDirectory); |
| | 421 | 101 | | var ifaceFilePathLiteral = ifaceFilePath != null |
| | 421 | 102 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(ifaceFilePath)}\"" |
| | 421 | 103 | | : "null"; |
| | 421 | 104 | | interfaceEntriesBuilder.Append($"new global::NexusLabs.Needlr.Catalog.InterfaceEntry(\"{GeneratorHelpers |
| | | 105 | | } |
| | 265807 | 106 | | interfaceEntriesBuilder.Append('}'); |
| | | 107 | | |
| | | 108 | | // Build constructor parameters array |
| | 265807 | 109 | | var constructorParamsBuilder = new StringBuilder(); |
| | 265807 | 110 | | constructorParamsBuilder.Append("new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry[] { "); |
| | 749606 | 111 | | foreach (var param in type.ConstructorParameters) |
| | | 112 | | { |
| | 108996 | 113 | | var serviceKeyLiteral = param.ServiceKey != null |
| | 108996 | 114 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(param.ServiceKey)}\"" |
| | 108996 | 115 | | : "null"; |
| | 108996 | 116 | | var paramName = param.ParameterName ?? "unknown"; |
| | 108996 | 117 | | constructorParamsBuilder.Append($"new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry(\"{Gene |
| | | 118 | | } |
| | 265807 | 119 | | constructorParamsBuilder.Append('}'); |
| | | 120 | | |
| | | 121 | | // Build service keys array |
| | 265813 | 122 | | var serviceKeysArray = $"new string[] {{ {string.Join(", ", type.ServiceKeys.Select(k => $"\"{GeneratorHelpe |
| | | 123 | | |
| | 265807 | 124 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.ServiceCatalogEntry(\"{GeneratorHelpers.Es |
| | | 125 | | } |
| | | 126 | | |
| | 590 | 127 | | builder.AppendLine(" };"); |
| | 590 | 128 | | builder.AppendLine(); |
| | 590 | 129 | | } |
| | | 130 | | |
| | | 131 | | private static void GenerateDecoratorsProperty( |
| | | 132 | | StringBuilder builder, |
| | | 133 | | IReadOnlyList<DiscoveredDecorator> decorators, |
| | | 134 | | string? projectDirectory) |
| | | 135 | | { |
| | 590 | 136 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 137 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 138 | | builder.AppendLine(" {"); |
| | | 139 | | |
| | 1274 | 140 | | foreach (var decorator in decorators) |
| | | 141 | | { |
| | 47 | 142 | | var shortName = GeneratorHelpers.GetShortTypeName(decorator.DecoratorTypeName); |
| | 47 | 143 | | var sourceFilePath = GetRelativeSourcePath(decorator.SourceFilePath, projectDirectory); |
| | 47 | 144 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 47 | 145 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 47 | 146 | | : "null"; |
| | | 147 | | |
| | 47 | 148 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.DecoratorCatalogEntry(\"{GeneratorHelpers. |
| | | 149 | | } |
| | | 150 | | |
| | 590 | 151 | | builder.AppendLine(" };"); |
| | 590 | 152 | | builder.AppendLine(); |
| | 590 | 153 | | } |
| | | 154 | | |
| | | 155 | | private static void GenerateHostedServicesProperty( |
| | | 156 | | StringBuilder builder, |
| | | 157 | | IReadOnlyList<DiscoveredHostedService> hostedServices, |
| | | 158 | | string? projectDirectory) |
| | | 159 | | { |
| | 590 | 160 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 161 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 162 | | builder.AppendLine(" {"); |
| | | 163 | | |
| | 1198 | 164 | | foreach (var hosted in hostedServices) |
| | | 165 | | { |
| | 9 | 166 | | var shortName = GeneratorHelpers.GetShortTypeName(hosted.TypeName); |
| | 9 | 167 | | var sourceFilePath = GetRelativeSourcePath(hosted.SourceFilePath, projectDirectory); |
| | 9 | 168 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 9 | 169 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 9 | 170 | | : "null"; |
| | | 171 | | |
| | | 172 | | // Build constructor parameters array |
| | 9 | 173 | | var constructorParamsBuilder = new StringBuilder(); |
| | 9 | 174 | | constructorParamsBuilder.Append("new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry[] { "); |
| | 22 | 175 | | foreach (var param in hosted.ConstructorParameters) |
| | | 176 | | { |
| | 2 | 177 | | var serviceKeyLiteral = param.ServiceKey != null |
| | 2 | 178 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(param.ServiceKey)}\"" |
| | 2 | 179 | | : "null"; |
| | 2 | 180 | | var paramName = param.ParameterName ?? "unknown"; |
| | 2 | 181 | | constructorParamsBuilder.Append($"new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry(\"{Gene |
| | | 182 | | } |
| | 9 | 183 | | constructorParamsBuilder.Append('}'); |
| | | 184 | | |
| | 9 | 185 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.HostedServiceCatalogEntry(\"{GeneratorHelp |
| | | 186 | | } |
| | | 187 | | |
| | 590 | 188 | | builder.AppendLine(" };"); |
| | 590 | 189 | | builder.AppendLine(); |
| | 590 | 190 | | } |
| | | 191 | | |
| | | 192 | | private static void GenerateInterceptedServicesProperty( |
| | | 193 | | StringBuilder builder, |
| | | 194 | | IReadOnlyList<DiscoveredInterceptedService> interceptedServices, |
| | | 195 | | string? projectDirectory) |
| | | 196 | | { |
| | 590 | 197 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 198 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 199 | | builder.AppendLine(" {"); |
| | | 200 | | |
| | 1210 | 201 | | foreach (var intercepted in interceptedServices) |
| | | 202 | | { |
| | 15 | 203 | | var shortName = GeneratorHelpers.GetShortTypeName(intercepted.TypeName); |
| | 15 | 204 | | var lifetimeStr = intercepted.Lifetime switch |
| | 15 | 205 | | { |
| | 4 | 206 | | GeneratorLifetime.Singleton => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Singleton", |
| | 11 | 207 | | GeneratorLifetime.Scoped => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped", |
| | 0 | 208 | | GeneratorLifetime.Transient => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Transient", |
| | 0 | 209 | | _ => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped" |
| | 15 | 210 | | }; |
| | 15 | 211 | | var sourceFilePath = GetRelativeSourcePath(intercepted.SourceFilePath, projectDirectory); |
| | 15 | 212 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 15 | 213 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 15 | 214 | | : "null"; |
| | | 215 | | |
| | 30 | 216 | | var interfacesArray = $"new string[] {{ {string.Join(", ", intercepted.InterfaceNames.Select(i => $"\"{Gener |
| | 33 | 217 | | var interceptorsArray = $"new string[] {{ {string.Join(", ", intercepted.AllInterceptorTypeNames.Select(i => |
| | | 218 | | |
| | 15 | 219 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.InterceptedServiceCatalogEntry(\"{Generato |
| | | 220 | | } |
| | | 221 | | |
| | 590 | 222 | | builder.AppendLine(" };"); |
| | 590 | 223 | | builder.AppendLine(); |
| | 590 | 224 | | } |
| | | 225 | | |
| | | 226 | | private static void GenerateOptionsProperty( |
| | | 227 | | StringBuilder builder, |
| | | 228 | | IReadOnlyList<DiscoveredOptions> options, |
| | | 229 | | string? projectDirectory) |
| | | 230 | | { |
| | 590 | 231 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 232 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 233 | | builder.AppendLine(" {"); |
| | | 234 | | |
| | 1532 | 235 | | foreach (var opt in options) |
| | | 236 | | { |
| | 176 | 237 | | var shortName = GeneratorHelpers.GetShortTypeName(opt.TypeName); |
| | 176 | 238 | | var sourceFilePath = GetRelativeSourcePath(opt.SourceFilePath, projectDirectory); |
| | 176 | 239 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 176 | 240 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 176 | 241 | | : "null"; |
| | 176 | 242 | | var nameLiteral = opt.Name != null |
| | 176 | 243 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(opt.Name)}\"" |
| | 176 | 244 | | : "null"; |
| | | 245 | | |
| | 176 | 246 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.OptionsCatalogEntry(\"{GeneratorHelpers.Es |
| | | 247 | | } |
| | | 248 | | |
| | 590 | 249 | | builder.AppendLine(" };"); |
| | 590 | 250 | | builder.AppendLine(); |
| | 590 | 251 | | } |
| | | 252 | | |
| | | 253 | | private static void GeneratePluginsProperty( |
| | | 254 | | StringBuilder builder, |
| | | 255 | | IReadOnlyList<DiscoveredPlugin> plugins, |
| | | 256 | | string? projectDirectory) |
| | | 257 | | { |
| | 590 | 258 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 590 | 259 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 590 | 260 | | builder.AppendLine(" {"); |
| | | 261 | | |
| | 4650 | 262 | | foreach (var plugin in plugins) |
| | | 263 | | { |
| | 1735 | 264 | | var shortName = GeneratorHelpers.GetShortTypeName(plugin.TypeName); |
| | 1735 | 265 | | var sourceFilePath = GetRelativeSourcePath(plugin.SourceFilePath, projectDirectory); |
| | 1735 | 266 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 1735 | 267 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 1735 | 268 | | : "null"; |
| | | 269 | | |
| | 3484 | 270 | | var interfacesArray = $"new string[] {{ {string.Join(", ", plugin.InterfaceNames.Select(i => $"\"{GeneratorH |
| | | 271 | | |
| | 1735 | 272 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.PluginCatalogEntry(\"{GeneratorHelpers.Esc |
| | | 273 | | } |
| | | 274 | | |
| | 590 | 275 | | builder.AppendLine(" };"); |
| | 590 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static string? GetRelativeSourcePath(string? fullPath, string? projectDirectory) |
| | | 279 | | { |
| | 268210 | 280 | | if (string.IsNullOrEmpty(fullPath)) |
| | | 281 | | { |
| | 268198 | 282 | | return null; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | // Delegates to the shared helper so the catalog cannot drift from every other |
| | | 286 | | // emitter. The shared helper normalizes separators to '/' and falls back to the |
| | | 287 | | // bare file name rather than leaking an absolute build-machine path. |
| | 12 | 288 | | return BreadcrumbWriter.GetRelativeSourcePath(fullPath, projectDirectory); |
| | | 289 | | } |
| | | 290 | | } |