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; }
public static int ExecuteAndCaptureOutput(this ProcessStartInfo startInfo, out string stdOut, out string stdErr) { var outStream = new StreamForwarder().Capture(); var errStream = new StreamForwarder().Capture(); startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; var process = new Process { StartInfo = startInfo }; process.EnableRaisingEvents = true; using (var reaper = new ProcessReaper(process)) { process.Start(); reaper.NotifyProcessStarted(); var taskOut = outStream.BeginRead(process.StandardOutput); var taskErr = errStream.BeginRead(process.StandardError); process.WaitForExit(); taskOut.Wait(); taskErr.Wait(); stdOut = outStream.CapturedOutput; stdErr = errStream.CapturedOutput; } return(process.ExitCode); }
internal BuiltInCommand(string commandName, IEnumerable<string> commandArgs, Func<string[], int> builtInCommand, IBuiltInCommandEnvironment environment) { CommandName = commandName; _commandArgs = commandArgs; _builtInCommand = builtInCommand; _environment = environment; _stdOut = new StreamForwarder(); _stdErr = new StreamForwarder(); }
public virtual CommandResult ExecuteWithCapturedOutput(string args = "") { Console.WriteLine($"Executing (Captured Output) - {_command} {args}"); var commandPath = Env.GetCommandPath(_command, ".exe", ".cmd", "") ?? Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, _command, ".exe", ".cmd", ""); var stdOut = new StreamForwarder(); var stdErr = new StreamForwarder(); stdOut.Capture(); stdErr.Capture(); return RunProcess(commandPath, args, stdOut, stdErr); }
public virtual CommandResult Execute(string args = "") { var commandPath = _command; ResolveCommand(ref commandPath, ref args); Log($"Executing - {commandPath} {args}"); var stdOut = new StreamForwarder(); var stdErr = new StreamForwarder(); stdOut.ForwardTo(Log); stdErr.ForwardTo(Log); return RunProcess(commandPath, args, stdOut, stdErr); }
public virtual Task<CommandResult> ExecuteAsync(string args = "") { var commandPath = _command; ResolveCommand(ref commandPath, ref args); Console.WriteLine($"Executing - {commandPath} {args}"); var stdOut = new StreamForwarder(); var stdErr = new StreamForwarder(); stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine); stdErr.ForwardTo(writeLine: Reporter.Output.WriteLine); return RunProcessAsync(commandPath, args, stdOut, stdErr); }
public virtual CommandResult ExecuteWithCapturedOutput(string args = "") { var command = _command; ResolveCommand(ref command, ref args); var commandPath = Env.GetCommandPath(command, ".exe", ".cmd", "") ?? Env.GetCommandPathFromRootPath(_baseDirectory, command, ".exe", ".cmd", ""); Console.WriteLine($"Executing (Captured Output) - {commandPath} {args}"); var stdOut = new StreamForwarder(); var stdErr = new StreamForwarder(); stdOut.Capture(); stdErr.Capture(); return RunProcess(commandPath, args, stdOut, stdErr); }
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 }; 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; } }
public virtual CommandResult Execute(string args = "") { var commandPath = _command; if (!Path.IsPathRooted(_command)) { _command = Env.GetCommandPath(_command) ?? Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, _command); } Console.WriteLine($"Executing - {_command} {args}"); var stdOut = new StreamForwarder(); var stdErr = new StreamForwarder(); stdOut.ForwardTo(write: Reporter.Output.Write, writeLine: Reporter.Output.WriteLine); stdErr.ForwardTo(write: Reporter.Error.Write, writeLine: Reporter.Output.WriteLine); return RunProcess(commandPath, args, stdOut, stdErr); }
private Command(string executable, string args) { // Set the things we need var psi = new ProcessStartInfo() { FileName = executable, Arguments = args, RedirectStandardError = true, RedirectStandardOutput = true }; _process = new Process() { StartInfo = psi }; _stdOut = new StreamForwarder(); _stdErr = new StreamForwarder(); }
private Command(CommandSpec commandSpec) { var psi = new ProcessStartInfo { FileName = commandSpec.Path, Arguments = commandSpec.Args, RedirectStandardError = true, RedirectStandardOutput = true }; _stdOut = new StreamForwarder(); _stdErr = new StreamForwarder(); _process = new Process { StartInfo = psi }; ResolutionStrategy = commandSpec.ResolutionStrategy; }
private Command(string executable, string args, CommandResolutionStrategy resolutionStrategy) { // Set the things we need var psi = new ProcessStartInfo() { FileName = executable, Arguments = args, RedirectStandardError = true, RedirectStandardOutput = true }; _process = new Process() { StartInfo = psi }; _stdOut = new StreamForwarder(); _stdErr = new StreamForwarder(); ResolutionStrategy = resolutionStrategy; }
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; } 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.CapturedOutput, stdErr.CapturedOutput); return result; }
private void EnsureStdErr() { _stdErr = _stdErr ?? new StreamForwarder(); _process.StartInfo.RedirectStandardError = true; }
private void EnsureStdOut() { _stdOut = _stdOut ?? new StreamForwarder(); _process.StartInfo.RedirectStandardOutput = true; }
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 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 static void Forward(int bufferSize, ForwardOptions options, string str, string expectedCaptured, string[] expectedWrites) { var forwarder = new StreamForwarder(bufferSize); var writes = new List<string>(); if ((options & ForwardOptions.WriteLine) != 0) { forwarder.ForwardTo( write: (options & ForwardOptions.Write) == 0 ? (Action<string>)null : writes.Add, writeLine: s => writes.Add(s + "\n")); } if ((options & ForwardOptions.Capture) != 0) { forwarder.Capture(); } forwarder.Read(new StringReader(str)); Assert.Equal(expectedWrites, writes); var captured = forwarder.GetCapturedOutput(); Assert.Equal(expectedCaptured, captured); }
private void TestCapturingAndForwardingHelper(ForwardOptions options, string str, string expectedCaptured, string[] expectedWrites) { var forwarder = new StreamForwarder(); var writes = new List<string>(); if ((options & ForwardOptions.WriteLine) != 0) { forwarder.ForwardTo(writeLine: s => writes.Add(s + Environment.NewLine)); } if ((options & ForwardOptions.Capture) != 0) { forwarder.Capture(); } forwarder.Read(new StringReader(str)); Assert.Equal(expectedWrites, writes); var captured = forwarder.CapturedOutput; Assert.Equal(expectedCaptured, captured); }