public IAppResult Run(string path, string arguments = "", string workingDirectory = null, bool ignoreExitCode = false, int expectedExitCode = 0, bool suppressOutput = false, bool captureOutput = false) { var details = new AppRunDetails { Path = path, Argument = arguments, WorkingDirectory = workingDirectory }; if (!RunAllFilterBeforeRun(details)) return new AppResult(details.ExitCode ?? 0, null); var p = new Process { StartInfo = new ProcessStartInfo { FileName = details.Path, Arguments = details.Argument, UseShellExecute = false } }; if (details.WorkingDirectory != null) p.StartInfo.WorkingDirectory = details.WorkingDirectory; var output = new StringBuilder(); if (suppressOutput || captureOutput) { p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.OutputDataReceived += (sender, args) => { if (suppressOutput == false) _output.WriteInfoLine(args.Data); output.AppendLine(args.Data); }; p.ErrorDataReceived += (sender, args) => { if (suppressOutput == false) _output.WriteInfoLine(args.Data); output.AppendLine(args.Data); }; } try { p.Start(); } catch (Exception e) { throw new CouldNotStartExternalTool(path, arguments, e); } if (suppressOutput || captureOutput) { p.BeginOutputReadLine(); p.BeginErrorReadLine(); } p.WaitForExit(); details.ExitCode = p.ExitCode; if (!ignoreExitCode && p.ExitCode != expectedExitCode) throw new UnexpectedExitCode(path, arguments, p.ExitCode, expectedExitCode, output.ToString()); RunAllFiltersAfterRun(details); return new AppResult(details.ExitCode ?? 0, output.ToString()); }
private bool RunAllFilterBeforeRun(AppRunDetails details) { int cancelIndex = -1; var allFilters = _filterRegistration.AllFilters; for (var i = 0; i < allFilters.Length; i++) { var filter = allFilters[i]; if (!filter.BeforeRun(details)) { cancelIndex = i; break; } } if (cancelIndex != -1) { for (int i = cancelIndex; i >= 0; i--) allFilters[i].AfterRun(details); } return cancelIndex == -1; }
private void RunAllFiltersAfterRun(AppRunDetails details) { foreach (var filter in _filterRegistration.AllFilters) filter.AfterRun(details); }