| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using NexusLabs.Needlr.Generators.Models; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Generators.Export; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Generates the Needlr dependency graph JSON for IDE tooling. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class GraphExporter |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Generates the needlr-graph.json content from the discovery result. |
| | | 16 | | /// </summary> |
| | | 17 | | public static string GenerateGraphJson( |
| | | 18 | | DiscoveryResult discoveryResult, |
| | | 19 | | string assemblyName, |
| | | 20 | | string? projectPath, |
| | | 21 | | IReadOnlyList<CollectedDiagnostic>? diagnostics = null, |
| | | 22 | | IReadOnlyDictionary<string, IReadOnlyList<DiscoveredType>>? referencedAssemblyTypes = null) |
| | | 23 | | { |
| | | 24 | | var graph = BuildGraph(discoveryResult, assemblyName, projectPath, diagnostics, referencedAssemblyTypes); |
| | | 25 | | return SerializeToJson(graph); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | private static NeedlrGraph BuildGraph( |
| | | 29 | | DiscoveryResult discoveryResult, |
| | | 30 | | string assemblyName, |
| | | 31 | | string? projectPath, |
| | | 32 | | IReadOnlyList<CollectedDiagnostic>? diagnostics, |
| | | 33 | | IReadOnlyDictionary<string, IReadOnlyList<DiscoveredType>>? referencedAssemblyTypes) |
| | | 34 | | { |
| | | 35 | | var graph = new NeedlrGraph |
| | | 36 | | { |
| | | 37 | | SchemaVersion = "1.0", |
| | | 38 | | GeneratedAt = DateTime.UtcNow.ToString("O"), |
| | | 39 | | ProjectPath = projectPath, |
| | | 40 | | AssemblyName = assemblyName |
| | | 41 | | }; |
| | | 42 | | |
| | | 43 | | // Build type lookup for resolving dependencies (include referenced assembly types) |
| | | 44 | | var typeLookup = BuildTypeLookup(discoveryResult, referencedAssemblyTypes); |
| | | 45 | | |
| | | 46 | | // Map injectable types from current assembly to graph services |
| | | 47 | | foreach (var type in discoveryResult.InjectableTypes) |
| | | 48 | | { |
| | | 49 | | var service = MapToGraphService(type, assemblyName, typeLookup, discoveryResult); |
| | | 50 | | graph.Services.Add(service); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Add types from referenced assemblies with [GenerateTypeRegistry] |
| | | 54 | | if (referencedAssemblyTypes != null) |
| | | 55 | | { |
| | | 56 | | foreach (var kvp in referencedAssemblyTypes) |
| | | 57 | | { |
| | | 58 | | var refAssemblyName = kvp.Key; |
| | | 59 | | var types = kvp.Value; |
| | | 60 | | foreach (var type in types) |
| | | 61 | | { |
| | | 62 | | var service = MapToGraphService(type, refAssemblyName, typeLookup, discoveryResult); |
| | | 63 | | graph.Services.Add(service); |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // Add diagnostics if provided |
| | | 69 | | if (diagnostics != null) |
| | | 70 | | { |
| | | 71 | | foreach (var diag in diagnostics) |
| | | 72 | | { |
| | | 73 | | graph.Diagnostics.Add(new GraphDiagnostic |
| | | 74 | | { |
| | | 75 | | Id = diag.Id, |
| | | 76 | | Severity = diag.Severity, |
| | | 77 | | Message = diag.Message, |
| | | 78 | | Location = diag.FilePath != null ? new GraphLocation |
| | | 79 | | { |
| | | 80 | | FilePath = diag.FilePath, |
| | | 81 | | Line = diag.Line, |
| | | 82 | | Column = 0 |
| | | 83 | | } : null, |
| | | 84 | | RelatedServices = diag.RelatedServices?.ToList() ?? new List<string>() |
| | | 85 | | }); |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // Compute statistics (include referenced assembly types in count) |
| | | 90 | | graph.Statistics = ComputeStatistics(discoveryResult, referencedAssemblyTypes); |
| | | 91 | | |
| | | 92 | | return graph; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private static Dictionary<string, DiscoveredType> BuildTypeLookup( |
| | | 96 | | DiscoveryResult discoveryResult, |
| | | 97 | | IReadOnlyDictionary<string, IReadOnlyList<DiscoveredType>>? referencedAssemblyTypes) |
| | | 98 | | { |
| | | 99 | | var lookup = new Dictionary<string, DiscoveredType>(); |
| | | 100 | | |
| | | 101 | | // Add types from current assembly |
| | | 102 | | foreach (var type in discoveryResult.InjectableTypes) |
| | | 103 | | { |
| | | 104 | | lookup[type.TypeName] = type; |
| | | 105 | | foreach (var iface in type.InterfaceNames) |
| | | 106 | | { |
| | | 107 | | if (!lookup.ContainsKey(iface)) |
| | | 108 | | { |
| | | 109 | | lookup[iface] = type; |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Add types from referenced assemblies for dependency resolution |
| | | 115 | | if (referencedAssemblyTypes != null) |
| | | 116 | | { |
| | | 117 | | foreach (var kvp in referencedAssemblyTypes) |
| | | 118 | | { |
| | | 119 | | foreach (var type in kvp.Value) |
| | | 120 | | { |
| | | 121 | | if (!lookup.ContainsKey(type.TypeName)) |
| | | 122 | | { |
| | | 123 | | lookup[type.TypeName] = type; |
| | | 124 | | } |
| | | 125 | | foreach (var iface in type.InterfaceNames) |
| | | 126 | | { |
| | | 127 | | if (!lookup.ContainsKey(iface)) |
| | | 128 | | { |
| | | 129 | | lookup[iface] = type; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | return lookup; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | private static GraphService MapToGraphService( |
| | | 140 | | DiscoveredType type, |
| | | 141 | | string assemblyName, |
| | | 142 | | Dictionary<string, DiscoveredType> typeLookup, |
| | | 143 | | DiscoveryResult discoveryResult) |
| | | 144 | | { |
| | | 145 | | var service = new GraphService |
| | | 146 | | { |
| | | 147 | | Id = type.TypeName, |
| | | 148 | | TypeName = GetSimpleTypeName(type.TypeName), |
| | | 149 | | FullTypeName = type.TypeName, |
| | | 150 | | AssemblyName = assemblyName, |
| | | 151 | | Lifetime = type.Lifetime.ToString(), |
| | | 152 | | Location = type.SourceFilePath != null ? new GraphLocation |
| | | 153 | | { |
| | | 154 | | FilePath = type.SourceFilePath, |
| | | 155 | | Line = type.SourceLine, |
| | | 156 | | Column = 0 |
| | | 157 | | } : null, |
| | | 158 | | ServiceKeys = type.ServiceKeys.ToList(), |
| | | 159 | | Metadata = new GraphServiceMetadata |
| | | 160 | | { |
| | | 161 | | IsDisposable = type.IsDisposable, |
| | | 162 | | HasFactory = discoveryResult.Factories.Any(f => f.TypeName == type.TypeName), |
| | | 163 | | IsHostedService = discoveryResult.HostedServices.Any(h => h.TypeName == type.TypeName), |
| | | 164 | | IsPlugin = discoveryResult.PluginTypes.Any(p => p.TypeName == type.TypeName) |
| | | 165 | | } |
| | | 166 | | }; |
| | | 167 | | |
| | | 168 | | // Map interfaces with locations |
| | | 169 | | foreach (var ifaceInfo in type.InterfaceInfos) |
| | | 170 | | { |
| | | 171 | | service.Interfaces.Add(new GraphInterface |
| | | 172 | | { |
| | | 173 | | Name = GetSimpleTypeName(ifaceInfo.FullName), |
| | | 174 | | FullName = ifaceInfo.FullName, |
| | | 175 | | Location = ifaceInfo.HasLocation ? new GraphLocation |
| | | 176 | | { |
| | | 177 | | FilePath = ifaceInfo.SourceFilePath!, |
| | | 178 | | Line = ifaceInfo.SourceLine, |
| | | 179 | | Column = 0 |
| | | 180 | | } : null |
| | | 181 | | }); |
| | | 182 | | } |
| | | 183 | | // Fall back to InterfaceNames if no InterfaceInfos (for backwards compat) |
| | | 184 | | if (type.InterfaceInfos.Length == 0) |
| | | 185 | | { |
| | | 186 | | foreach (var iface in type.InterfaceNames) |
| | | 187 | | { |
| | | 188 | | service.Interfaces.Add(new GraphInterface |
| | | 189 | | { |
| | | 190 | | Name = GetSimpleTypeName(iface), |
| | | 191 | | FullName = iface |
| | | 192 | | }); |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | // Map dependencies from constructor parameters |
| | | 197 | | foreach (var param in type.ConstructorParameters) |
| | | 198 | | { |
| | | 199 | | var dependency = new GraphDependency |
| | | 200 | | { |
| | | 201 | | ParameterName = param.ParameterName ?? string.Empty, |
| | | 202 | | TypeName = GetSimpleTypeName(param.TypeName), |
| | | 203 | | FullTypeName = param.TypeName, |
| | | 204 | | IsKeyed = param.IsKeyed, |
| | | 205 | | ServiceKey = param.ServiceKey |
| | | 206 | | }; |
| | | 207 | | |
| | | 208 | | // Try to resolve the dependency |
| | | 209 | | if (typeLookup.TryGetValue(param.TypeName, out var resolved)) |
| | | 210 | | { |
| | | 211 | | dependency.ResolvedTo = resolved.TypeName; |
| | | 212 | | dependency.ResolvedLifetime = resolved.Lifetime.ToString(); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | service.Dependencies.Add(dependency); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Map decorators |
| | | 219 | | var decorators = discoveryResult.Decorators |
| | | 220 | | .Where(d => type.InterfaceNames.Contains(d.ServiceTypeName)) |
| | | 221 | | .OrderBy(d => d.Order); |
| | | 222 | | |
| | | 223 | | foreach (var decorator in decorators) |
| | | 224 | | { |
| | | 225 | | service.Decorators.Add(new GraphDecorator |
| | | 226 | | { |
| | | 227 | | TypeName = decorator.DecoratorTypeName, |
| | | 228 | | Order = decorator.Order |
| | | 229 | | }); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | // Map interceptors |
| | | 233 | | var intercepted = discoveryResult.InterceptedServices |
| | | 234 | | .FirstOrDefault(i => i.TypeName == type.TypeName); |
| | | 235 | | |
| | | 236 | | if (intercepted.TypeName != null) |
| | | 237 | | { |
| | | 238 | | service.Interceptors = intercepted.AllInterceptorTypeNames.ToList(); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | // Collect attributes |
| | | 242 | | service.Attributes.Add(type.Lifetime.ToString()); |
| | | 243 | | if (type.IsKeyed) |
| | | 244 | | { |
| | | 245 | | service.Attributes.Add("Keyed"); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | return service; |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | private static GraphStatistics ComputeStatistics( |
| | | 252 | | DiscoveryResult discoveryResult, |
| | | 253 | | IReadOnlyDictionary<string, IReadOnlyList<DiscoveredType>>? referencedAssemblyTypes) |
| | | 254 | | { |
| | | 255 | | // Get all types for statistics - current assembly + referenced assemblies |
| | | 256 | | var allTypes = new List<DiscoveredType>(discoveryResult.InjectableTypes); |
| | | 257 | | if (referencedAssemblyTypes != null) |
| | | 258 | | { |
| | | 259 | | foreach (var kvp in referencedAssemblyTypes) |
| | | 260 | | { |
| | | 261 | | allTypes.AddRange(kvp.Value); |
| | | 262 | | } |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | return new GraphStatistics |
| | | 266 | | { |
| | | 267 | | TotalServices = allTypes.Count, |
| | | 268 | | Singletons = allTypes.Count(t => t.Lifetime == GeneratorLifetime.Singleton), |
| | | 269 | | Scoped = allTypes.Count(t => t.Lifetime == GeneratorLifetime.Scoped), |
| | | 270 | | Transient = allTypes.Count(t => t.Lifetime == GeneratorLifetime.Transient), |
| | | 271 | | Decorators = discoveryResult.Decorators.Count, |
| | | 272 | | Interceptors = discoveryResult.InterceptedServices.Count, |
| | | 273 | | Factories = discoveryResult.Factories.Count, |
| | | 274 | | Options = discoveryResult.Options.Count, |
| | | 275 | | HostedServices = discoveryResult.HostedServices.Count, |
| | | 276 | | Plugins = discoveryResult.PluginTypes.Count |
| | | 277 | | }; |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | private static string GetSimpleTypeName(string fullTypeName) |
| | | 281 | | { |
| | | 282 | | // Remove global:: prefix |
| | | 283 | | var name = fullTypeName; |
| | | 284 | | if (name.StartsWith("global::")) |
| | | 285 | | { |
| | | 286 | | name = name.Substring(8); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | // Handle generic types like Lazy<T> or IReadOnlyList<Assembly> |
| | | 290 | | // We want to preserve the generic structure but simplify inner types |
| | | 291 | | var genericStart = name.IndexOf('<'); |
| | | 292 | | if (genericStart >= 0) |
| | | 293 | | { |
| | | 294 | | // Get the outer type name (before generic params) |
| | | 295 | | var outerPart = name.Substring(0, genericStart); |
| | | 296 | | var lastDot = outerPart.LastIndexOf('.'); |
| | | 297 | | var simpleOuter = lastDot >= 0 ? outerPart.Substring(lastDot + 1) : outerPart; |
| | | 298 | | |
| | | 299 | | // Get the generic parameters and simplify them recursively |
| | | 300 | | var genericEnd = name.LastIndexOf('>'); |
| | | 301 | | if (genericEnd > genericStart) |
| | | 302 | | { |
| | | 303 | | var genericParams = name.Substring(genericStart + 1, genericEnd - genericStart - 1); |
| | | 304 | | // Simplify each generic parameter (split by comma, handle nested generics) |
| | | 305 | | var simplifiedParams = SimplifyGenericParameters(genericParams); |
| | | 306 | | return $"{simpleOuter}<{simplifiedParams}>"; |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | return simpleOuter; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | // Get just the type name (after last dot) |
| | | 313 | | var idx = name.LastIndexOf('.'); |
| | | 314 | | return idx >= 0 ? name.Substring(idx + 1) : name; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | private static string SimplifyGenericParameters(string genericParams) |
| | | 318 | | { |
| | | 319 | | // Handle nested generics by tracking depth |
| | | 320 | | var result = new StringBuilder(); |
| | | 321 | | var depth = 0; |
| | | 322 | | var currentParam = new StringBuilder(); |
| | | 323 | | |
| | | 324 | | foreach (var c in genericParams) |
| | | 325 | | { |
| | | 326 | | if (c == '<') |
| | | 327 | | { |
| | | 328 | | depth++; |
| | | 329 | | currentParam.Append(c); |
| | | 330 | | } |
| | | 331 | | else if (c == '>') |
| | | 332 | | { |
| | | 333 | | depth--; |
| | | 334 | | currentParam.Append(c); |
| | | 335 | | } |
| | | 336 | | else if (c == ',' && depth == 0) |
| | | 337 | | { |
| | | 338 | | // End of parameter at top level |
| | | 339 | | if (result.Length > 0) |
| | | 340 | | { |
| | | 341 | | result.Append(", "); |
| | | 342 | | } |
| | | 343 | | result.Append(GetSimpleTypeName(currentParam.ToString().Trim())); |
| | | 344 | | currentParam.Clear(); |
| | | 345 | | } |
| | | 346 | | else |
| | | 347 | | { |
| | | 348 | | currentParam.Append(c); |
| | | 349 | | } |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | // Add last parameter |
| | | 353 | | if (currentParam.Length > 0) |
| | | 354 | | { |
| | | 355 | | if (result.Length > 0) |
| | | 356 | | { |
| | | 357 | | result.Append(", "); |
| | | 358 | | } |
| | | 359 | | result.Append(GetSimpleTypeName(currentParam.ToString().Trim())); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | return result.ToString(); |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | /// <summary> |
| | | 366 | | /// Serializes the graph to JSON without using System.Text.Json (not available in all targets). |
| | | 367 | | /// Uses simple string building for source generator compatibility. |
| | | 368 | | /// </summary> |
| | | 369 | | private static string SerializeToJson(NeedlrGraph graph) |
| | | 370 | | { |
| | | 371 | | var sb = new StringBuilder(); |
| | | 372 | | sb.AppendLine("{"); |
| | | 373 | | sb.AppendLine($" \"schemaVersion\": \"{Escape(graph.SchemaVersion)}\","); |
| | | 374 | | sb.AppendLine($" \"generatedAt\": \"{Escape(graph.GeneratedAt)}\","); |
| | | 375 | | sb.AppendLine($" \"projectPath\": {NullableString(graph.ProjectPath)},"); |
| | | 376 | | sb.AppendLine($" \"assemblyName\": {NullableString(graph.AssemblyName)},"); |
| | | 377 | | |
| | | 378 | | // Services array |
| | | 379 | | sb.AppendLine(" \"services\": ["); |
| | | 380 | | for (int i = 0; i < graph.Services.Count; i++) |
| | | 381 | | { |
| | | 382 | | SerializeService(sb, graph.Services[i], i == graph.Services.Count - 1); |
| | | 383 | | } |
| | | 384 | | sb.AppendLine(" ],"); |
| | | 385 | | |
| | | 386 | | // Diagnostics array |
| | | 387 | | sb.AppendLine(" \"diagnostics\": ["); |
| | | 388 | | for (int i = 0; i < graph.Diagnostics.Count; i++) |
| | | 389 | | { |
| | | 390 | | SerializeDiagnostic(sb, graph.Diagnostics[i], i == graph.Diagnostics.Count - 1); |
| | | 391 | | } |
| | | 392 | | sb.AppendLine(" ],"); |
| | | 393 | | |
| | | 394 | | // Statistics object |
| | | 395 | | sb.AppendLine(" \"statistics\": {"); |
| | | 396 | | sb.AppendLine($" \"totalServices\": {graph.Statistics.TotalServices},"); |
| | | 397 | | sb.AppendLine($" \"singletons\": {graph.Statistics.Singletons},"); |
| | | 398 | | sb.AppendLine($" \"scoped\": {graph.Statistics.Scoped},"); |
| | | 399 | | sb.AppendLine($" \"transient\": {graph.Statistics.Transient},"); |
| | | 400 | | sb.AppendLine($" \"decorators\": {graph.Statistics.Decorators},"); |
| | | 401 | | sb.AppendLine($" \"interceptors\": {graph.Statistics.Interceptors},"); |
| | | 402 | | sb.AppendLine($" \"factories\": {graph.Statistics.Factories},"); |
| | | 403 | | sb.AppendLine($" \"options\": {graph.Statistics.Options},"); |
| | | 404 | | sb.AppendLine($" \"hostedServices\": {graph.Statistics.HostedServices},"); |
| | | 405 | | sb.AppendLine($" \"plugins\": {graph.Statistics.Plugins}"); |
| | | 406 | | sb.AppendLine(" }"); |
| | | 407 | | |
| | | 408 | | sb.AppendLine("}"); |
| | | 409 | | return sb.ToString(); |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | private static void SerializeService(StringBuilder sb, GraphService service, bool isLast) |
| | | 413 | | { |
| | | 414 | | sb.AppendLine(" {"); |
| | | 415 | | sb.AppendLine($" \"id\": \"{Escape(service.Id)}\","); |
| | | 416 | | sb.AppendLine($" \"typeName\": \"{Escape(service.TypeName)}\","); |
| | | 417 | | sb.AppendLine($" \"fullTypeName\": \"{Escape(service.FullTypeName)}\","); |
| | | 418 | | sb.AppendLine($" \"assemblyName\": {NullableString(service.AssemblyName)},"); |
| | | 419 | | |
| | | 420 | | // Interfaces |
| | | 421 | | sb.AppendLine(" \"interfaces\": ["); |
| | | 422 | | for (int i = 0; i < service.Interfaces.Count; i++) |
| | | 423 | | { |
| | | 424 | | var iface = service.Interfaces[i]; |
| | | 425 | | var comma = i < service.Interfaces.Count - 1 ? "," : ""; |
| | | 426 | | sb.AppendLine(" {"); |
| | | 427 | | sb.AppendLine($" \"name\": \"{Escape(iface.Name)}\","); |
| | | 428 | | sb.AppendLine($" \"fullName\": \"{Escape(iface.FullName)}\","); |
| | | 429 | | if (iface.Location != null) |
| | | 430 | | { |
| | | 431 | | sb.AppendLine(" \"location\": {"); |
| | | 432 | | sb.AppendLine($" \"filePath\": {NullableString(iface.Location.FilePath)},"); |
| | | 433 | | sb.AppendLine($" \"line\": {iface.Location.Line},"); |
| | | 434 | | sb.AppendLine($" \"column\": {iface.Location.Column}"); |
| | | 435 | | sb.AppendLine(" }"); |
| | | 436 | | } |
| | | 437 | | else |
| | | 438 | | { |
| | | 439 | | sb.AppendLine(" \"location\": null"); |
| | | 440 | | } |
| | | 441 | | sb.AppendLine($" }}{comma}"); |
| | | 442 | | } |
| | | 443 | | sb.AppendLine(" ],"); |
| | | 444 | | |
| | | 445 | | sb.AppendLine($" \"lifetime\": \"{Escape(service.Lifetime)}\","); |
| | | 446 | | |
| | | 447 | | // Location |
| | | 448 | | if (service.Location != null) |
| | | 449 | | { |
| | | 450 | | sb.AppendLine(" \"location\": {"); |
| | | 451 | | sb.AppendLine($" \"filePath\": {NullableString(service.Location.FilePath)},"); |
| | | 452 | | sb.AppendLine($" \"line\": {service.Location.Line},"); |
| | | 453 | | sb.AppendLine($" \"column\": {service.Location.Column}"); |
| | | 454 | | sb.AppendLine(" },"); |
| | | 455 | | } |
| | | 456 | | else |
| | | 457 | | { |
| | | 458 | | sb.AppendLine(" \"location\": null,"); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | // Dependencies |
| | | 462 | | sb.AppendLine(" \"dependencies\": ["); |
| | | 463 | | for (int i = 0; i < service.Dependencies.Count; i++) |
| | | 464 | | { |
| | | 465 | | var dep = service.Dependencies[i]; |
| | | 466 | | var comma = i < service.Dependencies.Count - 1 ? "," : ""; |
| | | 467 | | sb.AppendLine(" {"); |
| | | 468 | | sb.AppendLine($" \"parameterName\": \"{Escape(dep.ParameterName)}\","); |
| | | 469 | | sb.AppendLine($" \"typeName\": \"{Escape(dep.TypeName)}\","); |
| | | 470 | | sb.AppendLine($" \"fullTypeName\": \"{Escape(dep.FullTypeName)}\","); |
| | | 471 | | sb.AppendLine($" \"resolvedTo\": {NullableString(dep.ResolvedTo)},"); |
| | | 472 | | sb.AppendLine($" \"resolvedLifetime\": {NullableString(dep.ResolvedLifetime)},"); |
| | | 473 | | sb.AppendLine($" \"isKeyed\": {dep.IsKeyed.ToString().ToLowerInvariant()},"); |
| | | 474 | | sb.AppendLine($" \"serviceKey\": {NullableString(dep.ServiceKey)}"); |
| | | 475 | | sb.AppendLine($" }}{comma}"); |
| | | 476 | | } |
| | | 477 | | sb.AppendLine(" ],"); |
| | | 478 | | |
| | | 479 | | // Decorators |
| | | 480 | | sb.AppendLine(" \"decorators\": ["); |
| | | 481 | | for (int i = 0; i < service.Decorators.Count; i++) |
| | | 482 | | { |
| | | 483 | | var dec = service.Decorators[i]; |
| | | 484 | | var comma = i < service.Decorators.Count - 1 ? "," : ""; |
| | | 485 | | sb.AppendLine($" {{ \"typeName\": \"{Escape(dec.TypeName)}\", \"order\": {dec.Order} }}{comma}"); |
| | | 486 | | } |
| | | 487 | | sb.AppendLine(" ],"); |
| | | 488 | | |
| | | 489 | | // Interceptors |
| | | 490 | | sb.Append(" \"interceptors\": ["); |
| | | 491 | | sb.Append(string.Join(", ", service.Interceptors.Select(i => $"\"{Escape(i)}\""))); |
| | | 492 | | sb.AppendLine("],"); |
| | | 493 | | |
| | | 494 | | // Attributes |
| | | 495 | | sb.Append(" \"attributes\": ["); |
| | | 496 | | sb.Append(string.Join(", ", service.Attributes.Select(a => $"\"{Escape(a)}\""))); |
| | | 497 | | sb.AppendLine("],"); |
| | | 498 | | |
| | | 499 | | // Service keys |
| | | 500 | | sb.Append(" \"serviceKeys\": ["); |
| | | 501 | | sb.Append(string.Join(", ", service.ServiceKeys.Select(k => $"\"{Escape(k)}\""))); |
| | | 502 | | sb.AppendLine("],"); |
| | | 503 | | |
| | | 504 | | // Metadata |
| | | 505 | | sb.AppendLine(" \"metadata\": {"); |
| | | 506 | | sb.AppendLine($" \"hasFactory\": {service.Metadata.HasFactory.ToString().ToLowerInvariant()},"); |
| | | 507 | | sb.AppendLine($" \"hasOptions\": {service.Metadata.HasOptions.ToString().ToLowerInvariant()},"); |
| | | 508 | | sb.AppendLine($" \"isHostedService\": {service.Metadata.IsHostedService.ToString().ToLowerInvariant()},") |
| | | 509 | | sb.AppendLine($" \"isDisposable\": {service.Metadata.IsDisposable.ToString().ToLowerInvariant()},"); |
| | | 510 | | sb.AppendLine($" \"isPlugin\": {service.Metadata.IsPlugin.ToString().ToLowerInvariant()}"); |
| | | 511 | | sb.AppendLine(" }"); |
| | | 512 | | |
| | | 513 | | sb.AppendLine(isLast ? " }" : " },"); |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | private static void SerializeDiagnostic(StringBuilder sb, GraphDiagnostic diagnostic, bool isLast) |
| | | 517 | | { |
| | | 518 | | sb.AppendLine(" {"); |
| | | 519 | | sb.AppendLine($" \"id\": \"{Escape(diagnostic.Id)}\","); |
| | | 520 | | sb.AppendLine($" \"severity\": \"{Escape(diagnostic.Severity)}\","); |
| | | 521 | | sb.AppendLine($" \"message\": \"{Escape(diagnostic.Message)}\","); |
| | | 522 | | |
| | | 523 | | if (diagnostic.Location != null) |
| | | 524 | | { |
| | | 525 | | sb.AppendLine(" \"location\": {"); |
| | | 526 | | sb.AppendLine($" \"filePath\": {NullableString(diagnostic.Location.FilePath)},"); |
| | | 527 | | sb.AppendLine($" \"line\": {diagnostic.Location.Line},"); |
| | | 528 | | sb.AppendLine($" \"column\": {diagnostic.Location.Column}"); |
| | | 529 | | sb.AppendLine(" },"); |
| | | 530 | | } |
| | | 531 | | else |
| | | 532 | | { |
| | | 533 | | sb.AppendLine(" \"location\": null,"); |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | sb.Append(" \"relatedServices\": ["); |
| | | 537 | | sb.Append(string.Join(", ", diagnostic.RelatedServices.Select(s => $"\"{Escape(s)}\""))); |
| | | 538 | | sb.AppendLine("]"); |
| | | 539 | | |
| | | 540 | | sb.AppendLine(isLast ? " }" : " },"); |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | private static string Escape(string value) |
| | | 544 | | { |
| | | 545 | | if (string.IsNullOrEmpty(value)) |
| | | 546 | | return value; |
| | | 547 | | |
| | | 548 | | return value |
| | | 549 | | .Replace("\\", "\\\\") |
| | | 550 | | .Replace("\"", "\\\"") |
| | | 551 | | .Replace("\n", "\\n") |
| | | 552 | | .Replace("\r", "\\r") |
| | | 553 | | .Replace("\t", "\\t"); |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | private static string NullableString(string? value) |
| | | 557 | | { |
| | | 558 | | return value == null ? "null" : $"\"{Escape(value)}\""; |
| | | 559 | | } |
| | | 560 | | } |
| | | 561 | | |
| | | 562 | | /// <summary> |
| | | 563 | | /// Represents a diagnostic collected during generation for inclusion in the graph. |
| | | 564 | | /// </summary> |
| | | 565 | | internal sealed class CollectedDiagnostic |
| | | 566 | | { |
| | 0 | 567 | | public string Id { get; set; } = string.Empty; |
| | 0 | 568 | | public string Severity { get; set; } = string.Empty; |
| | 0 | 569 | | public string Message { get; set; } = string.Empty; |
| | 0 | 570 | | public string? FilePath { get; set; } |
| | 0 | 571 | | public int Line { get; set; } |
| | 0 | 572 | | public IReadOnlyList<string>? RelatedServices { get; set; } |
| | | 573 | | } |