| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Generators; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Utility methods for source code generation. |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class GeneratorHelpers |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Sanitizes an assembly name to be a valid C# identifier for use in namespaces. |
| | | 18 | | /// </summary> |
| | | 19 | | public static string SanitizeIdentifier(string name) |
| | | 20 | | { |
| | 75218 | 21 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 22 | | return "Generated"; |
| | | 23 | | |
| | 75218 | 24 | | var sb = new StringBuilder(name.Length); |
| | 3815348 | 25 | | foreach (var c in name) |
| | | 26 | | { |
| | 1832456 | 27 | | if (char.IsLetterOrDigit(c) || c == '_') |
| | | 28 | | { |
| | 1686387 | 29 | | sb.Append(c); |
| | | 30 | | } |
| | 146069 | 31 | | else if (c == '.' || c == '-' || c == ' ') |
| | | 32 | | { |
| | | 33 | | // Keep dots for namespace segments, replace dashes/spaces with underscores |
| | 146069 | 34 | | sb.Append(c == '.' ? '.' : '_'); |
| | | 35 | | } |
| | | 36 | | // Skip other characters |
| | | 37 | | } |
| | | 38 | | |
| | 75218 | 39 | | var result = sb.ToString(); |
| | | 40 | | |
| | | 41 | | // Ensure each segment doesn't start with a digit |
| | 75218 | 42 | | var segments = result.Split('.'); |
| | 593010 | 43 | | for (int i = 0; i < segments.Length; i++) |
| | | 44 | | { |
| | 221287 | 45 | | if (segments[i].Length > 0 && char.IsDigit(segments[i][0])) |
| | | 46 | | { |
| | 0 | 47 | | segments[i] = "_" + segments[i]; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 296505 | 51 | | return string.Join(".", segments.Where(s => s.Length > 0)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Escapes a string for use in a regular C# string literal. |
| | | 56 | | /// </summary> |
| | | 57 | | public static string EscapeStringLiteral(string value) |
| | | 58 | | { |
| | 800367 | 59 | | if (string.IsNullOrEmpty(value)) |
| | 1082 | 60 | | return string.Empty; |
| | 799285 | 61 | | return value.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Escapes a string for use in a verbatim C# string literal. |
| | | 66 | | /// </summary> |
| | | 67 | | public static string EscapeVerbatimStringLiteral(string value) |
| | | 68 | | { |
| | 475 | 69 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 70 | | return string.Empty; |
| | | 71 | | // In verbatim strings, only double-quotes need escaping (by doubling them) |
| | 475 | 72 | | return value.Replace("\"", "\"\""); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Escapes content for use in XML documentation. |
| | | 77 | | /// </summary> |
| | | 78 | | public static string EscapeXmlContent(string content) |
| | | 79 | | { |
| | | 80 | | // The content from GetDocumentationCommentXml() is already parsed, |
| | | 81 | | // so entities like < are already decoded. We need to re-encode them. |
| | 7 | 82 | | return content |
| | 7 | 83 | | .Replace("&", "&") |
| | 7 | 84 | | .Replace("<", "<") |
| | 7 | 85 | | .Replace(">", ">") |
| | 7 | 86 | | .Replace("\"", """) |
| | 7 | 87 | | .Replace("'", "'"); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the simple type name from a fully qualified name. |
| | | 92 | | /// "global::System.String" -> "String" |
| | | 93 | | /// </summary> |
| | | 94 | | public static string GetSimpleTypeName(string fullyQualifiedName) |
| | | 95 | | { |
| | 132 | 96 | | var parts = fullyQualifiedName.Split('.'); |
| | 132 | 97 | | return parts[parts.Length - 1]; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Converts a name to camelCase, removing leading 'I' for interfaces. |
| | | 102 | | /// </summary> |
| | | 103 | | public static string ToCamelCase(string name) |
| | | 104 | | { |
| | 142 | 105 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 106 | | return name; |
| | | 107 | | |
| | | 108 | | // Remove leading 'I' for interfaces |
| | 142 | 109 | | if (name.Length > 1 && name[0] == 'I' && char.IsUpper(name[1])) |
| | 98 | 110 | | name = name.Substring(1); |
| | | 111 | | |
| | 142 | 112 | | return char.ToLowerInvariant(name[0]) + name.Substring(1); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Strips the "global::" prefix from a type name if present. |
| | | 117 | | /// </summary> |
| | | 118 | | public static string StripGlobalPrefix(string name) |
| | | 119 | | { |
| | 18300 | 120 | | return name.StartsWith("global::", StringComparison.Ordinal) |
| | 18300 | 121 | | ? name.Substring(8) |
| | 18300 | 122 | | : name; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets the short type name from a fully qualified name. |
| | | 127 | | /// Removes global:: prefix and namespace. |
| | | 128 | | /// </summary> |
| | | 129 | | public static string GetShortTypeName(string fullyQualifiedTypeName) |
| | | 130 | | { |
| | 6098995 | 131 | | var name = fullyQualifiedTypeName; |
| | 6098995 | 132 | | if (name.StartsWith("global::", StringComparison.Ordinal)) |
| | 6033338 | 133 | | name = name.Substring(8); |
| | | 134 | | |
| | | 135 | | // For generic types, find the last dot before the generic type parameter |
| | | 136 | | // e.g., "Microsoft.Extensions.Options.IOptions<Foo.Bar>" -> find dot before "IOptions" |
| | 6098995 | 137 | | var genericStart = name.IndexOf('<'); |
| | | 138 | | |
| | 6098995 | 139 | | if (genericStart < 0) |
| | | 140 | | { |
| | | 141 | | // Non-generic type: just find the last dot |
| | 6095668 | 142 | | var lastDot = name.LastIndexOf('.'); |
| | 6095668 | 143 | | return lastDot >= 0 ? name.Substring(lastDot + 1) : name; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // Generic type: shorten the outer type and recursively shorten type arguments |
| | 3327 | 147 | | var lastDot2 = name.LastIndexOf('.', genericStart - 1); |
| | 3327 | 148 | | var outerType = lastDot2 >= 0 ? name.Substring(lastDot2 + 1, genericStart - lastDot2 - 1) : name.Substring(0, ge |
| | | 149 | | |
| | | 150 | | // Extract and shorten the type arguments |
| | 3327 | 151 | | var genericEnd = name.LastIndexOf('>'); |
| | 3327 | 152 | | if (genericEnd > genericStart) |
| | | 153 | | { |
| | 3327 | 154 | | var typeArgsStr = name.Substring(genericStart + 1, genericEnd - genericStart - 1); |
| | 3327 | 155 | | var shortenedArgs = ShortenGenericTypeArgs(typeArgsStr); |
| | 3327 | 156 | | return $"{outerType}<{shortenedArgs}>"; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | return outerType; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | private static string ShortenGenericTypeArgs(string typeArgsStr) |
| | | 163 | | { |
| | | 164 | | // Handle multiple type arguments and nested generics |
| | 3327 | 165 | | var result = new System.Text.StringBuilder(); |
| | 3327 | 166 | | var depth = 0; |
| | 3327 | 167 | | var start = 0; |
| | | 168 | | |
| | 213398 | 169 | | for (int i = 0; i < typeArgsStr.Length; i++) |
| | | 170 | | { |
| | 103372 | 171 | | var c = typeArgsStr[i]; |
| | 103976 | 172 | | if (c == '<') depth++; |
| | 103372 | 173 | | else if (c == '>') depth--; |
| | 102164 | 174 | | else if (c == ',' && depth == 0) |
| | | 175 | | { |
| | | 176 | | // Found a top-level comma, process this argument |
| | 609 | 177 | | var arg = typeArgsStr.Substring(start, i - start).Trim(); |
| | 611 | 178 | | if (result.Length > 0) result.Append(", "); |
| | 609 | 179 | | result.Append(GetShortTypeName(arg)); |
| | 609 | 180 | | start = i + 1; |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | // Process the last (or only) argument |
| | 3327 | 185 | | var lastArg = typeArgsStr.Substring(start).Trim(); |
| | 3934 | 186 | | if (result.Length > 0) result.Append(", "); |
| | 3327 | 187 | | result.Append(GetShortTypeName(lastArg)); |
| | | 188 | | |
| | 3327 | 189 | | return result.ToString(); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets the proxy type name for an intercepted service. |
| | | 194 | | /// </summary> |
| | | 195 | | public static string GetProxyTypeName(string fullyQualifiedTypeName) |
| | | 196 | | { |
| | 30 | 197 | | var shortName = GetShortTypeName(fullyQualifiedTypeName); |
| | 30 | 198 | | return $"{shortName}_InterceptorProxy"; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Gets the fully qualified validator class name for an options type. |
| | | 203 | | /// E.g., "global::TestApp.StripeOptions" -> "global::TestApp.Generated.StripeOptionsValidator" |
| | | 204 | | /// </summary> |
| | | 205 | | public static string GetValidatorClassName(string optionsTypeName) |
| | | 206 | | { |
| | 0 | 207 | | var shortName = GetShortTypeName(optionsTypeName); |
| | | 208 | | |
| | 0 | 209 | | var name = optionsTypeName; |
| | 0 | 210 | | if (name.StartsWith("global::", StringComparison.Ordinal)) |
| | 0 | 211 | | name = name.Substring(8); |
| | | 212 | | |
| | 0 | 213 | | var lastDot = name.LastIndexOf('.'); |
| | 0 | 214 | | var ns = lastDot >= 0 ? name.Substring(0, lastDot) : ""; |
| | | 215 | | |
| | 0 | 216 | | var validatorName = shortName + "Validator"; |
| | 0 | 217 | | return string.IsNullOrEmpty(ns) |
| | 0 | 218 | | ? $"global::{validatorName}" |
| | 0 | 219 | | : $"global::{ns}.Generated.{validatorName}"; |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Extracts the generic type argument from a generic type name. |
| | | 224 | | /// E.g., "Task<string>" -> "string" |
| | | 225 | | /// </summary> |
| | | 226 | | public static string ExtractGenericTypeArgument(string genericTypeName) |
| | | 227 | | { |
| | 1 | 228 | | var openBracket = genericTypeName.IndexOf('<'); |
| | 1 | 229 | | var closeBracket = genericTypeName.LastIndexOf('>'); |
| | 1 | 230 | | if (openBracket >= 0 && closeBracket > openBracket) |
| | | 231 | | { |
| | 1 | 232 | | return genericTypeName.Substring(openBracket + 1, closeBracket - openBracket - 1); |
| | | 233 | | } |
| | 0 | 234 | | return "object"; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Gets the base name of a generic type (without type arguments). |
| | | 239 | | /// E.g., "IHandler<Order>" -> "IHandler" |
| | | 240 | | /// </summary> |
| | | 241 | | public static string GetGenericBaseName(string typeName) |
| | | 242 | | { |
| | 21 | 243 | | var angleBracketIndex = typeName.IndexOf('<'); |
| | 21 | 244 | | return angleBracketIndex >= 0 ? typeName.Substring(0, angleBracketIndex) : typeName; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Creates a closed generic type name from an open generic decorator and a closed interface. |
| | | 249 | | /// For example: LoggingDecorator{T} + IHandler{Order} = LoggingDecorator{Order} |
| | | 250 | | /// </summary> |
| | | 251 | | public static string CreateClosedGenericType(string openDecoratorTypeName, string closedInterfaceName, string openIn |
| | | 252 | | { |
| | 7 | 253 | | var closedArgs = ExtractGenericArguments(closedInterfaceName); |
| | 7 | 254 | | var openDecoratorBaseName = GetGenericBaseName(openDecoratorTypeName); |
| | | 255 | | |
| | 7 | 256 | | if (closedArgs.Length == 0) |
| | 0 | 257 | | return openDecoratorTypeName; |
| | | 258 | | |
| | 7 | 259 | | return $"{openDecoratorBaseName}<{string.Join(", ", closedArgs)}>"; |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Extracts the generic type arguments from a closed generic type name. |
| | | 264 | | /// For example: "IHandler{Order, Payment}" returns ["Order", "Payment"] |
| | | 265 | | /// </summary> |
| | | 266 | | public static string[] ExtractGenericArguments(string typeName) |
| | | 267 | | { |
| | 7 | 268 | | var angleBracketIndex = typeName.IndexOf('<'); |
| | 7 | 269 | | if (angleBracketIndex < 0) |
| | 0 | 270 | | return Array.Empty<string>(); |
| | | 271 | | |
| | 7 | 272 | | var argsStart = angleBracketIndex + 1; |
| | 7 | 273 | | var argsEnd = typeName.LastIndexOf('>'); |
| | 7 | 274 | | if (argsEnd <= argsStart) |
| | 0 | 275 | | return Array.Empty<string>(); |
| | | 276 | | |
| | 7 | 277 | | var argsString = typeName.Substring(argsStart, argsEnd - argsStart); |
| | | 278 | | |
| | | 279 | | // Handle nested generics by parsing with bracket depth tracking |
| | 7 | 280 | | var args = new List<string>(); |
| | 7 | 281 | | var depth = 0; |
| | 7 | 282 | | var start = 0; |
| | | 283 | | |
| | 474 | 284 | | for (int i = 0; i < argsString.Length; i++) |
| | | 285 | | { |
| | 230 | 286 | | var c = argsString[i]; |
| | 230 | 287 | | if (c == '<') depth++; |
| | 230 | 288 | | else if (c == '>') depth--; |
| | 230 | 289 | | else if (c == ',' && depth == 0) |
| | | 290 | | { |
| | 1 | 291 | | args.Add(argsString.Substring(start, i - start).Trim()); |
| | 1 | 292 | | start = i + 1; |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | // Add the last argument |
| | 7 | 297 | | if (start < argsString.Length) |
| | 7 | 298 | | args.Add(argsString.Substring(start).Trim()); |
| | | 299 | | |
| | 7 | 300 | | return args.ToArray(); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Converts a type name to a valid Mermaid node ID. |
| | | 305 | | /// </summary> |
| | | 306 | | public static string GetMermaidNodeId(string typeName) |
| | | 307 | | { |
| | 53235 | 308 | | return GetShortTypeName(typeName).Replace(".", "_").Replace("<", "_").Replace(">", "_").Replace(",", "_"); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Calculates a percentage, handling division by zero. |
| | | 313 | | /// </summary> |
| | | 314 | | public static int Percentage(int count, int total) |
| | | 315 | | { |
| | 327 | 316 | | if (total == 0) return 0; |
| | 327 | 317 | | return (int)Math.Round(100.0 * count / total); |
| | | 318 | | } |
| | | 319 | | } |