| | | 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 | | DiscoveryResult discoveryResult, |
| | | 17 | | string assemblyName, |
| | | 18 | | string? projectDirectory, |
| | | 19 | | BreadcrumbWriter breadcrumbs) |
| | | 20 | | { |
| | 434 | 21 | | var builder = new StringBuilder(); |
| | 434 | 22 | | var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName); |
| | 434 | 23 | | var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | 24 | | |
| | 434 | 25 | | builder.AppendLine("// <auto-generated/>"); |
| | 434 | 26 | | builder.AppendLine("// Needlr Service Catalog"); |
| | 434 | 27 | | builder.AppendLine($"// Generated: {timestamp} UTC"); |
| | 434 | 28 | | builder.AppendLine(); |
| | 434 | 29 | | builder.AppendLine("#nullable enable"); |
| | 434 | 30 | | builder.AppendLine(); |
| | 434 | 31 | | builder.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 434 | 32 | | builder.AppendLine(); |
| | | 33 | | |
| | 434 | 34 | | breadcrumbs.WriteInlineComment(builder, "", $"ServiceCatalog: {discoveryResult.InjectableTypes.Count} services, |
| | | 35 | | |
| | 434 | 36 | | builder.AppendLine("/// <summary>"); |
| | 434 | 37 | | builder.AppendLine("/// Compile-time service catalog containing all discovered registrations."); |
| | 434 | 38 | | builder.AppendLine("/// </summary>"); |
| | 434 | 39 | | builder.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generators\", \"1 |
| | 434 | 40 | | builder.AppendLine($"public sealed class ServiceCatalog : global::NexusLabs.Needlr.Catalog.IServiceCatalog"); |
| | 434 | 41 | | builder.AppendLine("{"); |
| | 434 | 42 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 43 | | builder.AppendLine($" public string AssemblyName => \"{GeneratorHelpers.EscapeStringLiteral(assemblyName)}\"; |
| | 434 | 44 | | builder.AppendLine(); |
| | 434 | 45 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 46 | | builder.AppendLine($" public string GeneratedAt => \"{timestamp}\";"); |
| | 434 | 47 | | builder.AppendLine(); |
| | | 48 | | |
| | | 49 | | // Services |
| | 434 | 50 | | GenerateServicesProperty(builder, discoveryResult.InjectableTypes, projectDirectory); |
| | | 51 | | |
| | | 52 | | // Decorators |
| | 434 | 53 | | GenerateDecoratorsProperty(builder, discoveryResult.Decorators, projectDirectory); |
| | | 54 | | |
| | | 55 | | // Hosted Services |
| | 434 | 56 | | GenerateHostedServicesProperty(builder, discoveryResult.HostedServices, projectDirectory); |
| | | 57 | | |
| | | 58 | | // Intercepted Services |
| | 434 | 59 | | GenerateInterceptedServicesProperty(builder, discoveryResult.InterceptedServices, projectDirectory); |
| | | 60 | | |
| | | 61 | | // Options |
| | 434 | 62 | | GenerateOptionsProperty(builder, discoveryResult.Options, projectDirectory); |
| | | 63 | | |
| | | 64 | | // Plugins |
| | 434 | 65 | | GeneratePluginsProperty(builder, discoveryResult.PluginTypes, projectDirectory); |
| | | 66 | | |
| | 434 | 67 | | builder.AppendLine("}"); |
| | | 68 | | |
| | 434 | 69 | | return builder.ToString(); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | private static void GenerateServicesProperty( |
| | | 73 | | StringBuilder builder, |
| | | 74 | | IReadOnlyList<DiscoveredType> types, |
| | | 75 | | string? projectDirectory) |
| | | 76 | | { |
| | 434 | 77 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 78 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 79 | | builder.AppendLine(" {"); |
| | | 80 | | |
| | 457032 | 81 | | foreach (var type in types) |
| | | 82 | | { |
| | 228082 | 83 | | var shortName = GeneratorHelpers.GetShortTypeName(type.TypeName); |
| | 228082 | 84 | | var lifetimeStr = type.Lifetime switch |
| | 228082 | 85 | | { |
| | 228055 | 86 | | GeneratorLifetime.Singleton => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Singleton", |
| | 23 | 87 | | GeneratorLifetime.Scoped => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped", |
| | 4 | 88 | | GeneratorLifetime.Transient => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Transient", |
| | 0 | 89 | | _ => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped" |
| | 228082 | 90 | | }; |
| | 228082 | 91 | | var sourceFilePath = GetRelativeSourcePath(type.SourceFilePath, projectDirectory); |
| | 228082 | 92 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 228082 | 93 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 228082 | 94 | | : "null"; |
| | | 95 | | |
| | | 96 | | // Build interfaces array |
| | 228328 | 97 | | var interfacesArray = $"new string[] {{ {string.Join(", ", type.InterfaceNames.Select(i => $"\"{GeneratorHel |
| | | 98 | | |
| | | 99 | | // Build constructor parameters array |
| | 228082 | 100 | | var constructorParamsBuilder = new StringBuilder(); |
| | 228082 | 101 | | constructorParamsBuilder.Append("new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry[] { "); |
| | 564096 | 102 | | foreach (var param in type.ConstructorParameters) |
| | | 103 | | { |
| | 53966 | 104 | | var serviceKeyLiteral = param.ServiceKey != null |
| | 53966 | 105 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(param.ServiceKey)}\"" |
| | 53966 | 106 | | : "null"; |
| | 53966 | 107 | | var paramName = param.ParameterName ?? "unknown"; |
| | 53966 | 108 | | constructorParamsBuilder.Append($"new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry(\"{Gene |
| | | 109 | | } |
| | 228082 | 110 | | constructorParamsBuilder.Append('}'); |
| | | 111 | | |
| | | 112 | | // Build service keys array |
| | 228087 | 113 | | var serviceKeysArray = $"new string[] {{ {string.Join(", ", type.ServiceKeys.Select(k => $"\"{GeneratorHelpe |
| | | 114 | | |
| | 228082 | 115 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.ServiceCatalogEntry(\"{GeneratorHelpers.Es |
| | | 116 | | } |
| | | 117 | | |
| | 434 | 118 | | builder.AppendLine(" };"); |
| | 434 | 119 | | builder.AppendLine(); |
| | 434 | 120 | | } |
| | | 121 | | |
| | | 122 | | private static void GenerateDecoratorsProperty( |
| | | 123 | | StringBuilder builder, |
| | | 124 | | IReadOnlyList<DiscoveredDecorator> decorators, |
| | | 125 | | string? projectDirectory) |
| | | 126 | | { |
| | 434 | 127 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 128 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 129 | | builder.AppendLine(" {"); |
| | | 130 | | |
| | 922 | 131 | | foreach (var decorator in decorators) |
| | | 132 | | { |
| | 27 | 133 | | var shortName = GeneratorHelpers.GetShortTypeName(decorator.DecoratorTypeName); |
| | 27 | 134 | | var sourceFilePath = GetRelativeSourcePath(decorator.SourceFilePath, projectDirectory); |
| | 27 | 135 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 27 | 136 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 27 | 137 | | : "null"; |
| | | 138 | | |
| | 27 | 139 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.DecoratorCatalogEntry(\"{GeneratorHelpers. |
| | | 140 | | } |
| | | 141 | | |
| | 434 | 142 | | builder.AppendLine(" };"); |
| | 434 | 143 | | builder.AppendLine(); |
| | 434 | 144 | | } |
| | | 145 | | |
| | | 146 | | private static void GenerateHostedServicesProperty( |
| | | 147 | | StringBuilder builder, |
| | | 148 | | IReadOnlyList<DiscoveredHostedService> hostedServices, |
| | | 149 | | string? projectDirectory) |
| | | 150 | | { |
| | 434 | 151 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 152 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 153 | | builder.AppendLine(" {"); |
| | | 154 | | |
| | 882 | 155 | | foreach (var hosted in hostedServices) |
| | | 156 | | { |
| | 7 | 157 | | var shortName = GeneratorHelpers.GetShortTypeName(hosted.TypeName); |
| | 7 | 158 | | var sourceFilePath = GetRelativeSourcePath(hosted.SourceFilePath, projectDirectory); |
| | 7 | 159 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 7 | 160 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 7 | 161 | | : "null"; |
| | | 162 | | |
| | | 163 | | // Build constructor parameters array |
| | 7 | 164 | | var constructorParamsBuilder = new StringBuilder(); |
| | 7 | 165 | | constructorParamsBuilder.Append("new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry[] { "); |
| | 16 | 166 | | foreach (var param in hosted.ConstructorParameters) |
| | | 167 | | { |
| | 1 | 168 | | var serviceKeyLiteral = param.ServiceKey != null |
| | 1 | 169 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(param.ServiceKey)}\"" |
| | 1 | 170 | | : "null"; |
| | 1 | 171 | | var paramName = param.ParameterName ?? "unknown"; |
| | 1 | 172 | | constructorParamsBuilder.Append($"new global::NexusLabs.Needlr.Catalog.ConstructorParameterEntry(\"{Gene |
| | | 173 | | } |
| | 7 | 174 | | constructorParamsBuilder.Append('}'); |
| | | 175 | | |
| | 7 | 176 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.HostedServiceCatalogEntry(\"{GeneratorHelp |
| | | 177 | | } |
| | | 178 | | |
| | 434 | 179 | | builder.AppendLine(" };"); |
| | 434 | 180 | | builder.AppendLine(); |
| | 434 | 181 | | } |
| | | 182 | | |
| | | 183 | | private static void GenerateInterceptedServicesProperty( |
| | | 184 | | StringBuilder builder, |
| | | 185 | | IReadOnlyList<DiscoveredInterceptedService> interceptedServices, |
| | | 186 | | string? projectDirectory) |
| | | 187 | | { |
| | 434 | 188 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 189 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 190 | | builder.AppendLine(" {"); |
| | | 191 | | |
| | 898 | 192 | | foreach (var intercepted in interceptedServices) |
| | | 193 | | { |
| | 15 | 194 | | var shortName = GeneratorHelpers.GetShortTypeName(intercepted.TypeName); |
| | 15 | 195 | | var lifetimeStr = intercepted.Lifetime switch |
| | 15 | 196 | | { |
| | 2 | 197 | | GeneratorLifetime.Singleton => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Singleton", |
| | 13 | 198 | | GeneratorLifetime.Scoped => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped", |
| | 0 | 199 | | GeneratorLifetime.Transient => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Transient", |
| | 0 | 200 | | _ => "global::NexusLabs.Needlr.Catalog.ServiceCatalogLifetime.Scoped" |
| | 15 | 201 | | }; |
| | 15 | 202 | | var sourceFilePath = GetRelativeSourcePath(intercepted.SourceFilePath, projectDirectory); |
| | 15 | 203 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 15 | 204 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 15 | 205 | | : "null"; |
| | | 206 | | |
| | 30 | 207 | | var interfacesArray = $"new string[] {{ {string.Join(", ", intercepted.InterfaceNames.Select(i => $"\"{Gener |
| | 33 | 208 | | var interceptorsArray = $"new string[] {{ {string.Join(", ", intercepted.AllInterceptorTypeNames.Select(i => |
| | | 209 | | |
| | 15 | 210 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.InterceptedServiceCatalogEntry(\"{Generato |
| | | 211 | | } |
| | | 212 | | |
| | 434 | 213 | | builder.AppendLine(" };"); |
| | 434 | 214 | | builder.AppendLine(); |
| | 434 | 215 | | } |
| | | 216 | | |
| | | 217 | | private static void GenerateOptionsProperty( |
| | | 218 | | StringBuilder builder, |
| | | 219 | | IReadOnlyList<DiscoveredOptions> options, |
| | | 220 | | string? projectDirectory) |
| | | 221 | | { |
| | 434 | 222 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 223 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 224 | | builder.AppendLine(" {"); |
| | | 225 | | |
| | 1198 | 226 | | foreach (var opt in options) |
| | | 227 | | { |
| | 165 | 228 | | var shortName = GeneratorHelpers.GetShortTypeName(opt.TypeName); |
| | 165 | 229 | | var sourceFilePath = GetRelativeSourcePath(opt.SourceFilePath, projectDirectory); |
| | 165 | 230 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 165 | 231 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 165 | 232 | | : "null"; |
| | 165 | 233 | | var nameLiteral = opt.Name != null |
| | 165 | 234 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(opt.Name)}\"" |
| | 165 | 235 | | : "null"; |
| | | 236 | | |
| | 165 | 237 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.OptionsCatalogEntry(\"{GeneratorHelpers.Es |
| | | 238 | | } |
| | | 239 | | |
| | 434 | 240 | | builder.AppendLine(" };"); |
| | 434 | 241 | | builder.AppendLine(); |
| | 434 | 242 | | } |
| | | 243 | | |
| | | 244 | | private static void GeneratePluginsProperty( |
| | | 245 | | StringBuilder builder, |
| | | 246 | | IReadOnlyList<DiscoveredPlugin> plugins, |
| | | 247 | | string? projectDirectory) |
| | | 248 | | { |
| | 434 | 249 | | builder.AppendLine($" /// <inheritdoc />"); |
| | 434 | 250 | | builder.AppendLine($" public global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.Catalo |
| | 434 | 251 | | builder.AppendLine(" {"); |
| | | 252 | | |
| | 3626 | 253 | | foreach (var plugin in plugins) |
| | | 254 | | { |
| | 1379 | 255 | | var shortName = GeneratorHelpers.GetShortTypeName(plugin.TypeName); |
| | 1379 | 256 | | var sourceFilePath = GetRelativeSourcePath(plugin.SourceFilePath, projectDirectory); |
| | 1379 | 257 | | var sourceFilePathLiteral = sourceFilePath != null |
| | 1379 | 258 | | ? $"\"{GeneratorHelpers.EscapeStringLiteral(sourceFilePath)}\"" |
| | 1379 | 259 | | : "null"; |
| | | 260 | | |
| | 2768 | 261 | | var interfacesArray = $"new string[] {{ {string.Join(", ", plugin.InterfaceNames.Select(i => $"\"{GeneratorH |
| | | 262 | | |
| | 1379 | 263 | | builder.AppendLine($" new global::NexusLabs.Needlr.Catalog.PluginCatalogEntry(\"{GeneratorHelpers.Esc |
| | | 264 | | } |
| | | 265 | | |
| | 434 | 266 | | builder.AppendLine(" };"); |
| | 434 | 267 | | } |
| | | 268 | | |
| | | 269 | | private static string? GetRelativeSourcePath(string? fullPath, string? projectDirectory) |
| | | 270 | | { |
| | 229675 | 271 | | if (fullPath == null || projectDirectory == null) |
| | 229675 | 272 | | return fullPath; |
| | | 273 | | |
| | 0 | 274 | | if (fullPath.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase)) |
| | | 275 | | { |
| | 0 | 276 | | var relative = fullPath.Substring(projectDirectory.Length); |
| | 0 | 277 | | return relative.TrimStart('/', '\\'); |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | return fullPath; |
| | | 281 | | } |
| | | 282 | | } |