-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolProcess.cs
More file actions
110 lines (99 loc) · 3.61 KB
/
Copy pathToolProcess.cs
File metadata and controls
110 lines (99 loc) · 3.61 KB
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
using System.Diagnostics;
using System.Text;
namespace XISOSharp.BattleTests;
/// <summary>
/// Generic CLI runner: starts an exe, captures stdout/stderr, enforces a timeout
/// with full-tree kill. Used for both XISOSharp.Cli.exe and extract-xiso.exe.
/// </summary>
internal sealed class ToolProcess
{
/// <summary>Gets the per-run timeout in milliseconds.</summary>
public int TimeoutMs { get; init; } = 3_600_000;
/// <summary>Gets whether the exe exists on disk.</summary>
public bool Available => File.Exists(ExePath);
/// <summary>Gets the resolved exe path.</summary>
public string ExePath { get; }
public ToolProcess(string exePath)
{
ExePath = Path.GetFullPath(exePath);
}
/// <summary>Runs the exe with args; returns exit code, captured output, and the
/// exe's wall-clock seconds (start → exit, excludes harness overhead).</summary>
public (int ExitCode, string StdOut, string StdErr, double Seconds) Run(params string[] args)
{
ProcessStartInfo psi = new()
{
FileName = ExePath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
};
foreach (string a in args)
{
psi.ArgumentList.Add(a);
}
Stopwatch sw = Stopwatch.StartNew();
using Process proc = Process.Start(psi) ?? throw new InvalidOperationException($"Failed to start {ExePath}");
Task<string> stdoutTask = proc.StandardOutput.ReadToEndAsync();
Task<string> stderrTask = proc.StandardError.ReadToEndAsync();
if (!proc.WaitForExit(TimeoutMs))
{
TryKill(proc);
proc.WaitForExit(5000);
throw new TimeoutException(
$"{Path.GetFileName(ExePath)} timed out after {TimeoutMs} ms: {string.Join(' ', args)}");
}
sw.Stop();
return (proc.ExitCode, stdoutTask.GetAwaiter().GetResult(), stderrTask.GetAwaiter().GetResult(),
sw.Elapsed.TotalSeconds);
}
/// <summary>Probes the tool banner: -v first, then --version, then --help
/// (first non-empty line). Tolerates CLIs with different version flags.</summary>
public string GetVersion()
{
try
{
foreach (string flag in new[] { "-v", "--version", "--help" })
{
(int code, string so, string se, _) = Run(flag);
string txt = string.IsNullOrWhiteSpace(so) ? se : so;
string first = txt.Split('\n', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault()?.Trim() ??
string.Empty;
if (code == 0 && !string.IsNullOrWhiteSpace(first) &&
!first.StartsWith("error", StringComparison.OrdinalIgnoreCase))
{
return first;
}
}
return "version probe failed";
}
catch (Exception ex)
{
return $"version probe failed: {ex.GetType().Name}";
}
}
private static void TryKill(Process proc)
{
try
{
proc.Kill(entireProcessTree: true);
}
catch (InvalidOperationException)
{
// Process already exited between WaitForExit and Kill.
}
catch (NotSupportedException)
{
try
{
proc.Kill();
}
catch (InvalidOperationException)
{
}
}
}
}