summaryrefslogtreecommitdiff
path: root/CommandLineWrapper/WorkspaceCommandGenerator/Program.cs
blob: dd00afc1bcfb7c46e312b3ac819609885ed4fa3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System.Text;
using System.Text.RegularExpressions;

namespace CommandLineWrapper.CodeGenerator
{
    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<CommandMethod> ParseCommandMethods(string constantsContent)
        {
            var methods = new List<CommandMethod>();

            // 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" || methodName == "Login")
                {
                    continue;
                }

                // Parse parameters
                var paramList = new List<MethodParameter>();
                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<CommandMethod> methods)
        {
            var sb = new StringBuilder();

            // Add using statements and namespace
            sb.AppendLine("using static JVCSCommandInvoker;");
            sb.AppendLine();
            sb.AppendLine("namespace CommandLineWrapper");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// Provides extension methods for JVCSWorkspace");
            sb.AppendLine("    /// These methods are auto-generated based on command templates in Constants.CommandParameterGenerator");
            sb.AppendLine("    /// </summary>");
            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($"        /// <summary>");
            sb.AppendLine($"        /// {GetMethodDescription(method.Name)}");
            sb.AppendLine($"        /// </summary>");
            foreach (var param in method.Parameters)
            {
                sb.AppendLine($"        /// <param name=\"{param.Name}\"> {param.Name} </param>");
            }
            sb.AppendLine($"        /// <param name=\"workspace\"> Workspace </param>");
            sb.AppendLine($"        /// <returns> Task </returns>");

            // Generate method body
            sb.AppendLine($"        public static async Task<InvokeResult> {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<MethodParameter> Parameters { get; set; } = new List<MethodParameter>();
    }

    class MethodParameter
    {
        public string Type { get; set; } = "";
        public string Name { get; set; } = "";
    }
}