summaryrefslogtreecommitdiff
path: root/CommandLineWrapper
diff options
context:
space:
mode:
Diffstat (limited to 'CommandLineWrapper')
-rw-r--r--CommandLineWrapper/.gitignore4
-rw-r--r--CommandLineWrapper/CommandLineWrapper.csproj13
-rw-r--r--CommandLineWrapper/JVCSWorkspace.cs8
-rw-r--r--CommandLineWrapper/WorkspaceCommandGenerator/Program.cs206
-rw-r--r--CommandLineWrapper/WorkspaceCommandGenerator/WorkspaceCommandGenerator.csproj10
5 files changed, 235 insertions, 6 deletions
diff --git a/CommandLineWrapper/.gitignore b/CommandLineWrapper/.gitignore
new file mode 100644
index 0000000..e2651ec
--- /dev/null
+++ b/CommandLineWrapper/.gitignore
@@ -0,0 +1,4 @@
+############################
+### Auto-generated Codes ###
+############################
+JVCSWorkspaceCommands.cs
diff --git a/CommandLineWrapper/CommandLineWrapper.csproj b/CommandLineWrapper/CommandLineWrapper.csproj
index b92e51b..753d77a 100644
--- a/CommandLineWrapper/CommandLineWrapper.csproj
+++ b/CommandLineWrapper/CommandLineWrapper.csproj
@@ -4,6 +4,19 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <Target Name="GenerateWorkspaceExtensions" BeforeTargets="BeforeBuild">
+ <Exec
+ Command="dotnet run --project WorkspaceCommandGenerator/WorkspaceCommandGenerator.csproj"
+ WorkingDirectory="$(MSBuildProjectDirectory)"
+ />
+ </Target>
+
+ <PropertyGroup>
+ <TargetFramework>net8.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
</PropertyGroup>
</Project>
diff --git a/CommandLineWrapper/JVCSWorkspace.cs b/CommandLineWrapper/JVCSWorkspace.cs
index 6a39bbf..d88209c 100644
--- a/CommandLineWrapper/JVCSWorkspace.cs
+++ b/CommandLineWrapper/JVCSWorkspace.cs
@@ -2,6 +2,8 @@ using CommandLineWrapper;
public class JVCSWorkspace
{
+ public string WorkspaceDirectory => _workspaceDirectory ?? Directory.GetCurrentDirectory();
+
private string? _workspaceDirectory;
public async Task InitializeAsync(string directory)
@@ -26,10 +28,4 @@ public class JVCSWorkspace
await JVCSCommandInvoker.Invoke(Constants.CommandParameterGenerator.Initialize());
}
}
-
- // Login to a upstream vault
- public async Task Login(string account, string upstream)
- {
- await JVCSCommandInvoker.Invoke(Constants.CommandParameterGenerator.Login(account, upstream));
- }
}
diff --git a/CommandLineWrapper/WorkspaceCommandGenerator/Program.cs b/CommandLineWrapper/WorkspaceCommandGenerator/Program.cs
new file mode 100644
index 0000000..dd00afc
--- /dev/null
+++ b/CommandLineWrapper/WorkspaceCommandGenerator/Program.cs
@@ -0,0 +1,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; } = "";
+ }
+}
diff --git a/CommandLineWrapper/WorkspaceCommandGenerator/WorkspaceCommandGenerator.csproj b/CommandLineWrapper/WorkspaceCommandGenerator/WorkspaceCommandGenerator.csproj
new file mode 100644
index 0000000..91b464a
--- /dev/null
+++ b/CommandLineWrapper/WorkspaceCommandGenerator/WorkspaceCommandGenerator.csproj
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net8.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+</Project>