public static async Task <CommandLineResult> Execute( string command, string args, DirectoryInfo workingDir = null, TimeSpan?timeout = null) { args ??= ""; var stdOut = new StringBuilder(); var stdErr = new StringBuilder(); using (var operation = ConfirmOnExit(command, args)) using (var process = StartProcess( command, args, workingDir, output: data => { stdOut.AppendLine(data); }, error: data => { stdErr.AppendLine(data); })) { var exitCode = await process.Complete().Timeout(timeout ?? TimeSpan.FromMinutes(1)); var output = stdOut.Replace("\r\n", "\n").ToString().Split('\n'); var error = stdErr.Replace("\r\n", "\n").ToString().Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); if (error.All(string.IsNullOrWhiteSpace)) { error = null; } var result = new CommandLineResult( exitCode: exitCode, output: output, error: error); if (exitCode == 0) { operation.Succeed( "> {command} {args} -> exited with {code}", process.StartInfo.FileName, process.StartInfo.Arguments, process.ExitCode); } else { operation.Fail(new CommandLineInvocationException(result)); } return(result); } }
public CommandLineInvocationException(CommandLineResult result, string message = null) : base( $"{message}{Environment.NewLine}Exit code {result.ExitCode}: {string.Join("\n", result.Error)}".Trim()) { }