private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { var psi = new ProcessStartInfo { FileName = executable, Arguments = args, RedirectStandardError = true, RedirectStandardOutput = true }; var process = new Process { StartInfo = psi }; process.EnableRaisingEvents = true; process.Start(); var threadOut = stdOut.BeginRead(process.StandardOutput); var threadErr = stdErr.BeginRead(process.StandardError); process.WaitForExit(); threadOut.Join(); threadErr.Join(); var result = new CommandResult( process.StartInfo, process.ExitCode, stdOut.GetCapturedOutput(), stdErr.GetCapturedOutput()); return(result); }
private static CommandResult ExecuteMiniCover(string minicoverDirectory, string command, string parameter) { StreamForwarder stdOut = new StreamForwarder(); StreamForwarder stdError = new StreamForwarder(); ProcessStartInfo processStartInfo = new ProcessStartInfo("dotnet", $"minicover {command} {parameter}") { WorkingDirectory = minicoverDirectory, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true }; stdOut.Capture(); stdError.Capture(); var process = new Process { StartInfo = processStartInfo }; process.Start(); var threadOut = stdOut.BeginRead(process.StandardOutput); var threadErr = stdError.BeginRead(process.StandardError); process.WaitForExit(); threadOut.Wait(); threadErr.Wait(); process.HasExited.ShouldBe(true); return(new CommandResult(processStartInfo, process.ExitCode, stdOut.CapturedOutput, stdError.CapturedOutput)); }
private CommandResult RunProcess(string executable, string[] args, StreamForwarder stdOut, StreamForwarder stdErr) { var psi = new ProcessStartInfo { FileName = executable, Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), RedirectStandardError = true, RedirectStandardOutput = true }; Log("Executing: ".Bold().Blue() + $"{psi.FileName} {psi.Arguments}"); foreach (var item in Environment) { psi.Environment[item.Key] = item.Value; } if (!string.IsNullOrWhiteSpace(WorkingDirectory)) { psi.WorkingDirectory = WorkingDirectory; Log($"Working directory: {WorkingDirectory}"); } var process = new Process { StartInfo = psi, EnableRaisingEvents = true }; using (process) { process.Start(); var threadOut = stdOut.BeginRead(process.StandardOutput); var threadErr = stdErr.BeginRead(process.StandardError); process.WaitForExit(); Task.WaitAll(threadOut, threadErr); var result = new CommandResult( process.StartInfo, process.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); return(result); } }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { CurrentProcess = StartProcess(executable, args); var taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); var taskErr = stdErr.BeginRead(CurrentProcess.StandardError); CurrentProcess.WaitForExit(); Task.WaitAll(taskOut, taskErr); var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); return(result); }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { var psi = new ProcessStartInfo { FileName = executable, Arguments = args, RedirectStandardError = true, RedirectStandardOutput = true }; foreach (var item in Environment) { psi.Environment[item.Key] = item.Value; } if (!string.IsNullOrWhiteSpace(WorkingDirectory)) { psi.WorkingDirectory = WorkingDirectory; Log($"Working directory: {WorkingDirectory}"); } var process = new Process { StartInfo = psi, EnableRaisingEvents = true }; process.Start(); var threadOut = stdOut.BeginRead(process.StandardOutput); var threadErr = stdErr.BeginRead(process.StandardError); process.WaitForExit(); threadOut.Join(); threadErr.Join(); var result = new CommandResult( process.StartInfo, process.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); return(result); }
private Task <CommandResult> RunProcessAsync(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { CurrentProcess = StartProcess(executable, args); var taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); var taskErr = stdErr.BeginRead(CurrentProcess.StandardError); var tcs = new TaskCompletionSource <CommandResult>(); CurrentProcess.Exited += (sender, arg) => { Task.WaitAll(taskOut, taskErr); var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); tcs.SetResult(result); }; return(tcs.Task); }
private Task<CommandResult> RunProcessAsync(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { Task taskOut = null; Task taskErr = null; CurrentProcess = CreateProcess(executable, args); CurrentProcess.Start(); try { taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } try { taskErr = stdErr.BeginRead(CurrentProcess.StandardError); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } var tcs = new TaskCompletionSource<CommandResult>(); CurrentProcess.Exited += (sender, arg) => { var tasksToAwait = new List<Task>(); if (taskOut != null) { tasksToAwait.Add(taskOut); } if (taskErr != null) { tasksToAwait.Add(taskErr); } if (tasksToAwait.Any()) { try { Task.WaitAll(tasksToAwait.ToArray()); } catch (System.ObjectDisposedException e) { taskErr = null; taskOut = null; } } var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut?.CapturedOutput ?? CurrentProcess.StandardOutput.ReadToEnd(), stdErr?.CapturedOutput ?? CurrentProcess.StandardError.ReadToEnd()); tcs.SetResult(result); }; return tcs.Task; }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { Task taskOut = null; Task taskErr = null; CurrentProcess = CreateProcess(executable, args); CurrentProcess.Start(); try { taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } try { taskErr = stdErr.BeginRead(CurrentProcess.StandardError); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } CurrentProcess.WaitForExit(); var tasksToAwait = new List <Task>(); if (taskOut != null) { tasksToAwait.Add(taskOut); } if (taskErr != null) { tasksToAwait.Add(taskErr); } if (tasksToAwait.Any()) { Task.WaitAll(tasksToAwait.ToArray()); } var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut?.CapturedOutput ?? CurrentProcess.StandardOutput.ReadToEnd(), stdErr?.CapturedOutput ?? CurrentProcess.StandardError.ReadToEnd()); return(result); }