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