using System.ComponentModel; using System.Diagnostics; namespace Build.Tools; /// /// Runs one external tool and lets it write straight to the terminal. /// /// /// The output is deliberately not captured. A gate which swallows the output of a failing test run /// and then prints "failed" leaves the person who has to fix it with nothing to go on, while the /// tools it runs already say everything worth saying -- which test, which line, which lint. /// public static class CommandRunner { /// /// What a tool which could not be started at all reports. /// /// /// Anything but zero counts as a failure, so the exact number matters only in that it is not /// one a tool would plausibly return itself. /// public const int COULD_NOT_START = 127; /// /// Runs a tool and waits for it. /// /// Where the tool should run. /// The tool, as it is called on the PATH. /// What to pass it. /// The exit code of the tool, or COULD_NOT_START when it never ran. public static async Task RunAsync(string workingDirectory, string fileName, string arguments) { Console.WriteLine($"- Running '{fileName} {arguments}' in '{workingDirectory}' ..."); var startInfo = new ProcessStartInfo { FileName = fileName, Arguments = arguments, WorkingDirectory = workingDirectory, UseShellExecute = false, }; try { using var process = Process.Start(startInfo); if (process is null) return ReportNotStarted(fileName); await process.WaitForExitAsync(); return process.ExitCode; } catch (Win32Exception exception) { return ReportCouldNotStart(fileName, exception); } } /// /// Runs a tool with a list of arguments and waits for it. /// /// /// Each argument reaches the tool unchanged, also when it contains spaces or quotes, because the system quotes /// it as the platform needs it. Paths which a user chose, such as an output folder, therefore need no quoting. /// /// Where the tool should run. /// The tool, as it is called on the PATH. /// What to pass it, one argument per entry. /// Writes the standard input of the tool, or null when the tool reads none. The input ends once the callback returns. /// The exit code of the tool, or COULD_NOT_START when it never ran. public static async Task RunAsync(string workingDirectory, string fileName, IReadOnlyList arguments, Func? writeInput = null) { Console.WriteLine($"- Running '{fileName} {DescribeArguments(arguments)}' in '{workingDirectory}' ..."); var startInfo = CreateStartInfo(workingDirectory, fileName, arguments); startInfo.RedirectStandardInput = writeInput is not null; try { using var process = Process.Start(startInfo); if (process is null) return ReportNotStarted(fileName); if (writeInput is not null) { try { await using var input = process.StandardInput.BaseStream; await writeInput(input); } catch (IOException) { // // The tool closed its input early, which it does when it fails. Its exit code // and what it wrote to the terminal tell more than the broken pipe could: // } } await process.WaitForExitAsync(); return process.ExitCode; } catch (Win32Exception exception) { return ReportCouldNotStart(fileName, exception); } } /// /// Runs a tool with a list of arguments and collects what it writes to its standard output. /// /// /// Its standard error still goes to the terminal, so a failing tool explains itself there. The standard output /// stays clean for reading answers such as a commit hash, which a warning on the same stream would spoil. /// /// Where the tool should run. /// The tool, as it is called on the PATH. /// What to pass it, one argument per entry. /// The standard output and the exit code of the tool; the exit code is COULD_NOT_START when it never ran. public static async Task ReadOutputAsync(string workingDirectory, string fileName, IReadOnlyList arguments) { var startInfo = CreateStartInfo(workingDirectory, fileName, arguments); startInfo.RedirectStandardOutput = true; try { using var process = Process.Start(startInfo); if (process is null) return new(string.Empty, ReportNotStarted(fileName)); var text = await process.StandardOutput.ReadToEndAsync(); await process.WaitForExitAsync(); return new(text, process.ExitCode); } catch (Win32Exception exception) { return new(string.Empty, ReportCouldNotStart(fileName, exception)); } } private static ProcessStartInfo CreateStartInfo(string workingDirectory, string fileName, IReadOnlyList arguments) { var startInfo = new ProcessStartInfo { FileName = fileName, WorkingDirectory = workingDirectory, UseShellExecute = false, }; foreach (var argument in arguments) startInfo.ArgumentList.Add(argument); return startInfo; } private static string DescribeArguments(IReadOnlyList arguments) => string.Join(' ', arguments.Select(argument => argument.Contains(' ') ? $"\"{argument}\"" : argument)); private static int ReportNotStarted(string fileName) { Console.WriteLine($"- Error: '{fileName}' did not start, and the system did not say why."); return COULD_NOT_START; } private static int ReportCouldNotStart(string fileName, Win32Exception exception) { Console.WriteLine($"- Error: '{fileName}' could not be started: {exception.Message}"); Console.WriteLine($" Is '{fileName}' installed and on the PATH?"); return COULD_NOT_START; } }