Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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.CapturedOutput, 
                stdErr.CapturedOutput);

            return result;
        }
Esempio n. 3
0
        public CommandResult Execute()
        {
            Reporter.Verbose.WriteLine(string.Format(
                                           LocalizableStrings.RunningFileNameArguments,
                                           _process.StartInfo.FileName,
                                           _process.StartInfo.Arguments));

            ThrowIfRunning();

            _running = true;

            _process.EnableRaisingEvents = true;

#if DEBUG
            var sw = Stopwatch.StartNew();

            Reporter.Verbose.WriteLine($"> {FormatProcessInfo(_process.StartInfo)}".White());
#endif
            using (PerfTrace.Current.CaptureTiming($"{Path.GetFileNameWithoutExtension(_process.StartInfo.FileName)} {_process.StartInfo.Arguments}"))
            {
                _process.Start();

                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.ProcessId,
                                               _process.Id));

                var taskOut = _stdOut?.BeginRead(_process.StandardOutput);
                var taskErr = _stdErr?.BeginRead(_process.StandardError);
                _process.WaitForExit();

                taskOut?.Wait();
                taskErr?.Wait();
            }

            var exitCode = _process.ExitCode;

#if DEBUG
            var message = string.Format(
                LocalizableStrings.ProcessExitedWithCode,
                FormatProcessInfo(_process.StartInfo),
                exitCode,
                sw.ElapsedMilliseconds);
            if (exitCode == 0)
            {
                Reporter.Verbose.WriteLine(message.Green());
            }
            else
            {
                Reporter.Verbose.WriteLine(message.Red().Bold());
            }
#endif

            return(new CommandResult(
                       _process.StartInfo,
                       exitCode,
                       _stdOut?.CapturedOutput,
                       _stdErr?.CapturedOutput));
        }
Esempio n. 4
0
        public CommandResult Execute()
        {
            TextWriter originalConsoleOut       = _environment.GetConsoleOut();
            TextWriter originalConsoleError     = _environment.GetConsoleError();
            string     originalWorkingDirectory = _environment.GetWorkingDirectory();

            try
            {
                // redirecting the standard out and error so we can forward
                // the output to the caller
                using (BlockingMemoryStream outStream = new BlockingMemoryStream())
                    using (BlockingMemoryStream errorStream = new BlockingMemoryStream())
                    {
                        _environment.SetConsoleOut(new StreamWriter(outStream)
                        {
                            AutoFlush = true
                        });
                        _environment.SetConsoleError(new StreamWriter(errorStream)
                        {
                            AutoFlush = true
                        });

                        // Reset the Reporters to the new Console Out and Error.
                        Reporter.Reset();

                        if (!string.IsNullOrEmpty(_workingDirectory))
                        {
                            _environment.SetWorkingDirectory(_workingDirectory);
                        }

                        var taskOut = _stdOut.BeginRead(new StreamReader(outStream));
                        var taskErr = _stdErr.BeginRead(new StreamReader(errorStream));

                        int exitCode = _builtInCommand(_commandArgs.ToArray());

                        outStream.DoneWriting();
                        errorStream.DoneWriting();

                        Task.WaitAll(taskOut, taskErr);

                        // fake out a ProcessStartInfo using the Muxer command name, since this is a built-in command
                        ProcessStartInfo startInfo = new ProcessStartInfo(new Muxer().MuxerPath, $"{CommandName} {CommandArgs}");
                        return(new CommandResult(startInfo, exitCode, null, null));
                    }
            }
            finally
            {
                _environment.SetConsoleOut(originalConsoleOut);
                _environment.SetConsoleError(originalConsoleError);
                _environment.SetWorkingDirectory(originalWorkingDirectory);

                Reporter.Reset();
            }
        }
Esempio n. 5
0
        public CommandResult Execute()
        {
            Reporter.Verbose.WriteLine($"Running {_process.StartInfo.FileName} {_process.StartInfo.Arguments}");

            ThrowIfRunning();
            _running = true;

            _process.EnableRaisingEvents = true;

#if DEBUG
            var sw = Stopwatch.StartNew();
            Reporter.Verbose.WriteLine($"> {FormatProcessInfo(_process.StartInfo)}".White());
#endif
            _process.Start();

            Reporter.Verbose.WriteLine($"Process ID: {_process.Id}");

            var threadOut = _stdOut.BeginRead(_process.StandardOutput);
            var threadErr = _stdErr.BeginRead(_process.StandardError);

            _process.WaitForExit();
            threadOut.Join();
            threadErr.Join();

            var exitCode = _process.ExitCode;

#if DEBUG
            var message = $"< {FormatProcessInfo(_process.StartInfo)} exited with {exitCode} in {sw.ElapsedMilliseconds} ms.";
            if (exitCode == 0)
            {
                Reporter.Verbose.WriteLine(message.Green());
            }
            else
            {
                Reporter.Verbose.WriteLine(message.Red().Bold());
            }
#endif

            return(new CommandResult(
                       this._process.StartInfo,
                       exitCode,
                       _stdOut.CapturedOutput,
                       _stdErr.CapturedOutput));
        }
Esempio n. 6
0
        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;
        }
Esempio n. 7
0
        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;
        }
Esempio n. 8
0
        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;
            }
        }