summaryrefslogtreecommitdiff
path: root/CommandLineWrapper
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-01-09 23:48:17 +0800
committer魏曹先生 <1992414357@qq.com>2026-01-09 23:48:17 +0800
commit6dedba9e3cedf6463f25460eba84111d6a0bd0dd (patch)
tree4dcc6855ada5b28e20835ab60800c1028e505f7a /CommandLineWrapper
parent6b9268d2d88c81c18f2cc21e343d321989dad99c (diff)
Add command line wrapper for JVCS operations
Diffstat (limited to 'CommandLineWrapper')
-rw-r--r--CommandLineWrapper/Class1.cs6
-rw-r--r--CommandLineWrapper/Constants.cs74
-rw-r--r--CommandLineWrapper/JVCSCommandInvoker.cs41
-rw-r--r--CommandLineWrapper/JVCSWorkspace.cs35
4 files changed, 150 insertions, 6 deletions
diff --git a/CommandLineWrapper/Class1.cs b/CommandLineWrapper/Class1.cs
deleted file mode 100644
index f8ed117..0000000
--- a/CommandLineWrapper/Class1.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace CommandLineWrapper;
-
-public class Class1
-{
-
-}
diff --git a/CommandLineWrapper/Constants.cs b/CommandLineWrapper/Constants.cs
new file mode 100644
index 0000000..91d0bd8
--- /dev/null
+++ b/CommandLineWrapper/Constants.cs
@@ -0,0 +1,74 @@
+namespace CommandLineWrapper;
+
+public static class Constants
+{
+ // Command line program path
+ public static FileInfo CommandLinePath => new FileInfo(
+ Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
+ OperatingSystem.IsWindows() ? "JustEnoughVCS.exe" : "JustEnoughVCS")
+ );
+
+ public static class CommandParameterGenerator
+ {
+ // Workspace Creation
+ public static string[] Initialize() => ["init"];
+ public static string[] Create(string name) => ["create", name];
+
+ // Upstream Binding
+ public static string[] Login(string account, string upstream) => ["login", account, upstream, "-C"];
+ public static string[] Direct(string upstream) => ["direct", upstream, "-C"];
+ public static string[] Unstain() => ["unstain", "-C"];
+ public static string[] Update() => ["update"];
+
+ // Account Management
+ public static string[] AccountAs(string account) => ["account", "as", account];
+ public static string[] AccountAdd(string account) => ["account", "add", account];
+ public static string[] AccountAddWithKey(string account) => ["account", "add", account, "--keygen"];
+ public static string[] AccountMoveKey(string account, FileInfo privateKey) => ["account", "movekey", account, privateKey.ToString()];
+ public static string[] AccountRemove(string account) => ["account", "remove", account];
+ public static string[] AccountGeneratePublicKey(string account, DirectoryInfo directory)
+ => ["account", "genpub", account, directory.ToString()];
+
+ // Sheet Management
+ public static string[] SheetUse(string sheet) => ["sheet", "use", sheet];
+ public static string[] SheetExit() => ["sheet", "exit"];
+ public static string[] SheetSwitch(string sheet) => ["use", sheet];
+ public static string[] SheetMake(string sheet) => ["sheet", "make", sheet, "-C"];
+ public static string[] SheetDrop(string sheet) => ["sheet", "drop", sheet, "-C"];
+ public static string[] SheetAlignOperation(string operationA, string operationB) => ["sheet", "align", operationA, operationB];
+
+ // Share
+ public static string[] Share(string mappingPattern, string sheet, string description) => ["share", mappingPattern, sheet, description];
+ public static string[] ShareImportSafeMode(string shareId) => ["share", shareId, "--safe"];
+ public static string[] ShareImportSkipMode(string shareId) => ["share", shareId, "--skip"];
+ public static string[] ShareImportOverwriteMode(string shareId) => ["share", shareId, "--overwrite"];
+ public static string[] ShareReject(string shareId) => ["share", shareId, "--reject"];
+
+ // File Operations
+ public static string[] Track(string mappingPattern) => ["track", mappingPattern];
+ public static string[] TrackWithUpdate(string mappingPattern, string nextVersion, string description)
+ => ["track", mappingPattern, "--version", nextVersion, "--desc", description];
+ public static string[] TrackAndOverwrite(string mappingPattern)
+ => ["track", mappingPattern, "--overwrite"];
+ public static string[] Hold(string mappingPattern) => ["hold", mappingPattern];
+ public static string[] Throw(string mappingPattern) => ["throw", mappingPattern];
+ public static string[] TryHold(string mappingPattern) => ["hold", mappingPattern, "--skip-failed"];
+ public static string[] TryThrow(string mappingPattern) => ["throw", mappingPattern, "--skip-failed"];
+ public static string[] HoldUnchecked(string mappingPattern) => ["hold", mappingPattern, "--force"];
+ public static string[] ThrowUnchecked(string mappingPattern) => ["throw", mappingPattern, "--force"];
+
+ // Informations
+ public static string[] Here() => ["here", "--json"];
+ public static string[] Status() => ["status", "--json"];
+ public static string[] Info(string mappingName) => ["info", mappingName, "--json"];
+ public static string[] AccountList() => ["account", "list", "--json"];
+ public static string[] SheetList() => ["sheet", "list", "--json"];
+ public static string[] SheetAlignList() => ["sheet", "align", "--json"];
+ public static string[] ShareList() => ["share", "list", "--json"];
+ public static string[] ShareSee(string shareId) => ["share", "see", shareId, "--json"];
+ public static string[] GetWorkspaceDirectory() => ["_workspace_dir"];
+ public static string[] GetCurrentAccount() => ["_account"];
+ public static string[] GetCurrentUpstream() => ["_upstream"];
+ public static string[] GetCurrentSheet() => ["_sheet"];
+ }
+}
diff --git a/CommandLineWrapper/JVCSCommandInvoker.cs b/CommandLineWrapper/JVCSCommandInvoker.cs
new file mode 100644
index 0000000..56d67ca
--- /dev/null
+++ b/CommandLineWrapper/JVCSCommandInvoker.cs
@@ -0,0 +1,41 @@
+using System.Diagnostics;
+using CommandLineWrapper;
+
+public static class JVCSCommandInvoker
+{
+ // Structure to hold the result of a command invocation
+ public struct InvokeResult
+ {
+ public int ExitCode { get; set; }
+ public string StandardOutput { get; set; }
+ }
+
+ // Invokes a command-line process with the given arguments and optional working directory
+ public static async Task<InvokeResult> Invoke(string[] args, string? workingDirectory = null)
+ {
+ var startInfo = new ProcessStartInfo
+ {
+ FileName = Constants.CommandLinePath.FullName,
+ Arguments = string.Join(" ", args),
+ RedirectStandardOutput = true,
+ UseShellExecute = false,
+ CreateNoWindow = true
+ };
+
+ if (!string.IsNullOrEmpty(workingDirectory))
+ startInfo.WorkingDirectory = workingDirectory;
+
+ using (var process = new Process { StartInfo = startInfo })
+ {
+ process.Start();
+ string output = await process.StandardOutput.ReadToEndAsync();
+ await process.WaitForExitAsync();
+
+ return new InvokeResult
+ {
+ ExitCode = process.ExitCode,
+ StandardOutput = output.Trim()
+ };
+ }
+ }
+}
diff --git a/CommandLineWrapper/JVCSWorkspace.cs b/CommandLineWrapper/JVCSWorkspace.cs
new file mode 100644
index 0000000..6a39bbf
--- /dev/null
+++ b/CommandLineWrapper/JVCSWorkspace.cs
@@ -0,0 +1,35 @@
+using CommandLineWrapper;
+
+public class JVCSWorkspace
+{
+ private string? _workspaceDirectory;
+
+ public async Task InitializeAsync(string directory)
+ {
+ // If the specified directory does not exist, create it
+ if (!Directory.Exists(directory))
+ Directory.CreateDirectory(directory);
+
+ // Invoke command to get workspace directory
+ var result = await JVCSCommandInvoker.Invoke(Constants.CommandParameterGenerator.GetWorkspaceDirectory(), directory);
+ var currentWorkspace = result.StandardOutput;
+
+ // Check if the obtained workspace directory is valid (not empty and exists)
+ if (string.IsNullOrWhiteSpace(currentWorkspace) &&
+ Directory.Exists(currentWorkspace))
+ {
+ _workspaceDirectory = currentWorkspace;
+ }
+ else
+ {
+ // If the workspace is invalid, initialize a new workspace
+ 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));
+ }
+}