Beispiel #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);
        }
Beispiel #2
0
        public static int Execute(this ProcessStartInfo startInfo)
        {
            if (startInfo == null)
            {
                throw new ArgumentNullException(nameof(startInfo));
            }

            var process = new Process
            {
                StartInfo = startInfo
            };

            using (var reaper = new ProcessReaper(process))
            {
                process.Start();
                reaper.NotifyProcessStarted();
                process.WaitForExit();
            }

            return(process.ExitCode);
        }
Beispiel #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}"))
            {
                using (var reaper = new ProcessReaper(_process))
                {
                    _process.Start();
                    reaper.NotifyProcessStarted();

                    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));
        }