private async Task <ProcessResult> RunProcessInternal(IOutputWriter outputWriter, string workingDirectory, string executablePath, string argumentsFormat, IReadOnlyDictionary <string, string> environmentVariables,
                                                              ProcessResultSelector processResultSelector, params object[] arguments)
        {
            string parameters = string.Format(argumentsFormat, arguments);

            outputWriter.WriteLine("Starting external program: \"{0}\" {1} in {2}", executablePath, parameters, workingDirectory);
            var psi = CreateProcessStartInfo(workingDirectory, executablePath, parameters, environmentVariables);

            using (var process = new Process {
                StartInfo = psi
            })
            {
                var result  = Execute(process);
                var message = new StringBuilder();
                if (!string.IsNullOrEmpty(result.StdOutput))
                {
                    message.AppendLine("StdOut:").Append(result.StdOutput).AppendLine();
                }
                if (!string.IsNullOrEmpty(result.StdError))
                {
                    message.AppendLine("StdError:").Append(result.StdError).AppendLine();
                }
                outputWriter.WriteLine(message.ToString());
                message = new StringBuilder($"Process \"{psi.FileName}\" {psi.Arguments} executed in {result.ExecutionTime} with ExitCode:{result.ExitCode}.");

                if (result.ExecutionTime > Timeout)
                {
                    message.Append($"Took longer than {Timeout}.");
                    throw new TimeoutException(message.ToString());
                }

                outputWriter.WriteLine(message.ToString());
                return(result);
            }
        }
Esempio n. 2
0
        private async Task<TResult> RunProcessInternal<TResult>(IOutputWriter outputWriter, string workingDirectory, string executablePath, string argumentsFormat, IReadOnlyDictionary<string, string> environmentVariables,
            ProcessResultSelector<TResult> processResultSelector, params object[] arguments)
        {
            string parameters = string.Format(argumentsFormat, arguments);

            outputWriter.WriteLine("Starting external program: \"{0}\" {1} in {2}", executablePath, parameters, workingDirectory);
            var psi = CreateProcessStartInfo(workingDirectory, executablePath, parameters, environmentVariables);

            using (var process = new Process { StartInfo = psi, EnableRaisingEvents = true })
            {
                var stdError = new StringBuilder();
                var stdOutput = new StringBuilder();

                process.ErrorDataReceived += (sender, e) => { stdError.Append(e.Data); };
                process.OutputDataReceived += (sender, e) => { stdOutput.Append(e.Data); };

                var before = DateTime.Now;
                process.Start();

                process.BeginErrorReadLine();

                return await processResultSelector(outputWriter, executablePath, process, psi, before, parameters, stdOutput, stdError);
            }
        }