summaryrefslogtreecommitdiff
path: root/CommandLineWrapper/JVCSCommandInvoker.cs
blob: 3d4e59496e4f2d9399887d16320e19c8166cabe9 (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
using System.Diagnostics;

namespace 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();
        process.StartInfo = startInfo;
        process.Start();
        string output = await process.StandardOutput.ReadToEndAsync();
        await process.WaitForExitAsync();

        return new InvokeResult
        {
            ExitCode = process.ExitCode,
            StandardOutput = output.Trim()
        };
    }
}