Fix std out/err in InvokeProcess

This commit is contained in:
Caelan Sayler
2024-08-08 14:43:55 +01:00
parent e1b8843c18
commit 240f3ed54e
2 changed files with 10 additions and 11 deletions

View File

@@ -8,16 +8,16 @@ public class ProcessFailedException : Exception
public string Command { get; } public string Command { get; }
public string StdOutput { get; } public string StdOutput { get; }
public ProcessFailedException(string command, string stdOutput) public ProcessFailedException(string command, string stdOutput, string stdErr)
: base($"Process failed: '{command}'{Environment.NewLine}Output was -{Environment.NewLine}{stdOutput}") : base($"Process failed: '{command}'{Environment.NewLine}Output was -{Environment.NewLine}{stdOutput}{Environment.NewLine}StdErr was -{Environment.NewLine}{stdErr}")
{ {
Command = command; Command = command;
StdOutput = stdOutput; StdOutput = stdOutput;
} }
public static void ThrowIfNonZero((int ExitCode, string StdOutput, string Command) result) public static void ThrowIfNonZero((int ExitCode, string StdOutput, string StdErr, string Command) result)
{ {
if (result.ExitCode != 0) if (result.ExitCode != 0)
throw new ProcessFailedException(result.Command, result.StdOutput); throw new ProcessFailedException(result.Command, result.StdOutput, result.StdErr);
} }
} }

View File

@@ -12,7 +12,7 @@ public static class Exe
if (VelopackRuntimeInfo.IsWindows) { if (VelopackRuntimeInfo.IsWindows) {
var output = InvokeAndThrowIfNonZero("where", new[] { binaryName }, null); var output = InvokeAndThrowIfNonZero("where", new[] { binaryName }, null);
if (String.IsNullOrWhiteSpace(output) || !File.Exists(output)) if (String.IsNullOrWhiteSpace(output) || !File.Exists(output))
throw new ProcessFailedException("", ""); throw new ProcessFailedException("", "", "");
} else if (VelopackRuntimeInfo.IsOSX) { } else if (VelopackRuntimeInfo.IsOSX) {
InvokeAndThrowIfNonZero("command", new[] { "-v", binaryName }, null); InvokeAndThrowIfNonZero("command", new[] { "-v", binaryName }, null);
} else if (VelopackRuntimeInfo.IsLinux) { } else if (VelopackRuntimeInfo.IsLinux) {
@@ -65,7 +65,7 @@ public static class Exe
process.WaitForExit(); process.WaitForExit();
var stdout = Utility.Retry(() => File.ReadAllText(outputFile).Trim(), 10, 1000); var stdout = Utility.Retry(() => File.ReadAllText(outputFile).Trim(), 10, 1000);
var result = (process.ExitCode, stdout, command); var result = (process.ExitCode, stdout, "", command);
ProcessFailedException.ThrowIfNonZero(result); ProcessFailedException.ThrowIfNonZero(result);
return result.Item2; return result.Item2;
} }
@@ -103,7 +103,7 @@ public static class Exe
return result.StdOutput; return result.StdOutput;
} }
public static (int ExitCode, string StdOutput) InvokeProcess(ProcessStartInfo psi, CancellationToken ct) public static (int ExitCode, string StdOutput, string StdErr) InvokeProcess(ProcessStartInfo psi, CancellationToken ct)
{ {
var process = new Process(); var process = new Process();
process.StartInfo = psi; process.StartInfo = psi;
@@ -134,11 +134,10 @@ public static class Exe
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
} }
var all = (sOut.ToString().Trim()) + Environment.NewLine + (sOut.ToString().Trim()); return (process.ExitCode, sOut.ToString().Trim(), sErr.ToString().Trim());
return (process.ExitCode, all.Trim());
} }
public static (int ExitCode, string StdOutput, string Command) InvokeProcess(string fileName, IEnumerable<string> args, string workingDirectory, CancellationToken ct = default, public static (int ExitCode, string StdOutput, string StdErr, string Command) InvokeProcess(string fileName, IEnumerable<string> args, string workingDirectory, CancellationToken ct = default,
IDictionary<string, string> envVar = null) IDictionary<string, string> envVar = null)
{ {
var psi = CreateProcessStartInfo(fileName, workingDirectory); var psi = CreateProcessStartInfo(fileName, workingDirectory);
@@ -150,7 +149,7 @@ public static class Exe
psi.AppendArgumentListSafe(args, out var argString); psi.AppendArgumentListSafe(args, out var argString);
var p = InvokeProcess(psi, ct); var p = InvokeProcess(psi, ct);
return (p.ExitCode, p.StdOutput, $"{fileName} {argString}"); return (p.ExitCode, p.StdOutput, p.StdErr, $"{fileName} {argString}");
} }
public static ProcessStartInfo CreateProcessStartInfo(string fileName, string workingDirectory) public static ProcessStartInfo CreateProcessStartInfo(string fileName, string workingDirectory)