using System.Text; using System.Text.RegularExpressions; namespace CommandLineWrapper.WorkspaceCommandGenerator { class Program { static void Main(string[] args) { try { // Get the current working directory (project root) var projectRoot = Directory.GetCurrentDirectory(); var constantsFilePath = Path.Combine(projectRoot, "Constants.cs"); var extensionsFilePath = Path.Combine(projectRoot, "JVCSWorkspaceCommands.cs"); // Check if files exist if (!File.Exists(constantsFilePath)) { Console.WriteLine($"Error: Constants.cs not found at {constantsFilePath}"); Environment.Exit(1); } Console.WriteLine($"Reading constants file: {constantsFilePath}"); Console.WriteLine($"Writing extensions file: {extensionsFilePath}"); // Read the Constants.cs file var constantsContent = File.ReadAllText(constantsFilePath); // Parse methods from the CommandParameterGenerator class var methods = ParseCommandMethods(constantsContent); // Generate the content for JVCSWorkspaceExtensions.cs var extensionsContent = GenerateExtensionsClass(methods); // Write to file File.WriteAllText(extensionsFilePath, extensionsContent); Console.WriteLine($"Successfully generated {methods.Count} extension methods in JVCSWorkspaceExtensions.cs"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); Environment.Exit(1); } } static List ParseCommandMethods(string constantsContent) { var methods = new List(); // Find the CommandParameterGenerator class var classPattern = @"public\s+static\s+class\s+CommandParameterGenerator\s*\{([^}]+(?:\{[^{}]*\}[^}]*)*)\}"; var classMatch = Regex.Match(constantsContent, classPattern, RegexOptions.Singleline); if (!classMatch.Success) { throw new InvalidOperationException("Could not find CommandParameterGenerator class"); } var classContent = classMatch.Groups[1].Value; // Match methods: public static string[] MethodName(params) var methodPattern = @"public\s+static\s+string\[\]\s+(\w+)\(([^)]*)\)\s*=>"; var methodMatches = Regex.Matches(classContent, methodPattern); foreach (Match methodMatch in methodMatches) { var methodName = methodMatch.Groups[1].Value; var parameters = methodMatch.Groups[2].Value; // Skip methods already defined in JVCSWorkspace if (methodName == "GetWorkspaceDirectory") { continue; } // Parse parameters var paramList = new List(); if (!string.IsNullOrWhiteSpace(parameters)) { var paramParts = parameters.Split(','); foreach (var paramPart in paramParts) { var trimmed = paramPart.Trim(); if (string.IsNullOrEmpty(trimmed)) continue; var paramMatch = Regex.Match(trimmed, @"(\w+)\s+(\w+)"); if (paramMatch.Success) { paramList.Add(new MethodParameter { Type = paramMatch.Groups[1].Value, Name = paramMatch.Groups[2].Value }); } } } methods.Add(new CommandMethod { Name = methodName, Parameters = paramList }); } return methods; } static string GenerateExtensionsClass(List methods) { var sb = new StringBuilder(); // Add using statements and namespace sb.AppendLine("using static CommandLineWrapper.JVCSCommandInvoker;"); sb.AppendLine(); sb.AppendLine("namespace CommandLineWrapper"); sb.AppendLine("{"); sb.AppendLine(" /// "); sb.AppendLine(" /// Provides extension methods for JVCSWorkspace"); sb.AppendLine(" /// These methods are auto-generated based on command templates in Constants.CommandParameterGenerator"); sb.AppendLine(" /// "); sb.AppendLine(" public static class JVCSWorkspaceExtensions"); sb.AppendLine(" {"); sb.AppendLine(); // Generate each extension method foreach (var method in methods) { GenerateExtensionMethod(sb, method); sb.AppendLine(); } sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } static void GenerateExtensionMethod(StringBuilder sb, CommandMethod method) { // Generate method signature var paramString = string.Join(", ", method.Parameters.Select(p => $"{p.Type} {p.Name}")); var fullParamString = string.IsNullOrEmpty(paramString) ? "this JVCSWorkspace workspace" : $"this JVCSWorkspace workspace, {paramString}"; // Generate method documentation sb.AppendLine($" /// "); sb.AppendLine($" /// {GetMethodDescription(method.Name)}"); sb.AppendLine($" /// "); foreach (var param in method.Parameters) { sb.AppendLine($" /// {param.Name} "); } sb.AppendLine($" /// Workspace "); sb.AppendLine($" /// Task "); // Generate method body sb.AppendLine($" public static async Task {method.Name}({fullParamString})"); sb.AppendLine(" {"); // Generate parameter call string var callParams = string.Join(", ", method.Parameters.Select(p => p.Name)); if (!string.IsNullOrEmpty(callParams)) { sb.AppendLine($" return await JVCSCommandInvoker.Invoke(Constants.CommandParameterGenerator.{method.Name}({callParams}), workspace.WorkspaceDirectory);"); } else { sb.AppendLine($" return await JVCSCommandInvoker.Invoke(Constants.CommandParameterGenerator.{method.Name}(), workspace.WorkspaceDirectory);"); } sb.AppendLine(" }"); } static string GetMethodDescription(string methodName) { // Generate description based on method name if (methodName.StartsWith("Get")) return $"Get {methodName.Substring(3)}"; if (methodName.StartsWith("Try")) return $"Try {methodName.Substring(3)}"; if (methodName.EndsWith("Async")) return $"{methodName.Replace("Async", "")}"; // Simple conversion: Convert camelCase to English description var description = Regex.Replace(methodName, "([a-z])([A-Z])", "$1 $2"); return char.ToUpper(description[0]) + description.Substring(1); } } class CommandMethod { public string Name { get; set; } = ""; public List Parameters { get; set; } = new List(); } class MethodParameter { public string Type { get; set; } = ""; public string Name { get; set; } = ""; } }