/// <summary> /// Initializes a new instance of <see cref="DefaultBenchManager"/> /// with a <see cref="DefaultExecutionHost"/> and a <see cref="ConsoleUserInterface"/>. /// </summary> /// <param name="config">The Bench configuration.</param> public DefaultBenchManager(BenchConfiguration config) { Config = config; Downloader = BenchTasks.InitializeDownloader(config); Env = new BenchEnvironment(Config); ProcessExecutionHost = new DefaultExecutionHost(); UI = new ConsoleUserInterface(); }
/// <summary> /// Starts a Windows process in a synchronous fashion. /// </summary> /// <param name="env">The environment variables of Bench.</param> /// <param name="cwd">The working directory, to start the process in.</param> /// <param name="exe">The path to the executable.</param> /// <param name="arguments">The string with the command line arguments.</param> /// <param name="monitoring">A flag to control the level of monitoring.</param> /// <returns>An instance of <see cref="ProcessExecutionResult"/> with the exit code /// and optionally the output of the process.</returns> /// <seealso cref="CommandLine.FormatArgumentList(string[])"/> public ProcessExecutionResult RunProcess(BenchEnvironment env, string cwd, string exe, string arguments, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(DefaultExecutionHost)); } var p = new Process(); if (!File.Exists(exe)) { throw new FileNotFoundException("The executable could not be found.", exe); } if (!Directory.Exists(cwd)) { throw new DirectoryNotFoundException("The working directory could not be found: " + cwd); } PreparePowerShellScriptExecution(ref exe, ref arguments); var collectOutput = (monitoring & ProcessMonitoring.Output) == ProcessMonitoring.Output; StringBuilder sbStd = null; StringBuilder sbErr = null; var si = new ProcessStartInfo(exe, arguments); si.UseShellExecute = false; si.WorkingDirectory = cwd; si.CreateNoWindow = collectOutput; si.RedirectStandardOutput = collectOutput; si.RedirectStandardError = collectOutput; env.Load(si.EnvironmentVariables); p.StartInfo = si; if (collectOutput) { sbStd = new StringBuilder(); p.OutputDataReceived += (s, e) => sbStd.AppendLine(e.Data); sbErr = new StringBuilder(); p.ErrorDataReceived += (s, e) => sbErr.AppendLine(e.Data); } p.Start(); if (collectOutput) { p.BeginOutputReadLine(); p.BeginErrorReadLine(); } p.WaitForExit(); if (collectOutput) { string output; try { output = FormatOutput(sbStd.ToString()) + Environment.NewLine + FormatOutput(sbErr.ToString()); } catch (Exception e) { output = e.ToString(); } return new ProcessExecutionResult(p.ExitCode, output); } else { return new ProcessExecutionResult(p.ExitCode); } }
/// <summary> /// Starts a Windows process in an asynchronous fashion. /// </summary> /// <param name="env">The environment variables of Bench.</param> /// <param name="cwd">The working directory, to start the process in.</param> /// <param name="exe">The path to the executable.</param> /// <param name="arguments">The string with the command line arguments.</param> /// <param name="cb">The handler method to call when the execution of the process finishes.</param> /// <param name="monitoring">A flag to control the level of monitoring.</param> /// <seealso cref="CommandLine.FormatArgumentList(string[])"/> public void StartProcess(BenchEnvironment env, string cwd, string exe, string arguments, ProcessExitCallback cb, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(DefaultExecutionHost)); } AsyncManager.StartTask(() => { cb(RunProcess(env, cwd, exe, arguments, monitoring)); }); }