/// <summary> /// Runs the specified command-line app. /// </summary> /// <param name="path">The path of the command-line app.</param> /// <param name="settings">The settings to use when running the app.</param> public static int RunApp(string path, AppRunnerSettings settings) { if (path == null) throw new ArgumentNullException(nameof(path)); if (settings == null) throw new ArgumentNullException(nameof(settings)); var args = ArgumentEscaper.EscapeAndConcatenate((settings.Arguments ?? Enumerable.Empty<string>()).Where(x => x != null)); var exitCode = 0; try { Command.Run(name: path, args: args, workingDirectory: settings.WorkingDirectory, noEcho: settings.NoEcho); } catch (NonZeroExitCodeException exception) { exitCode = exception.ExitCode; } var isExitCodeSuccess = settings.IsExitCodeSuccess ?? (x => x == 0); if (!isExitCodeSuccess(exitCode)) throw new ApplicationException($"The process failed with exit code {exitCode}."); return exitCode; }
/// <summary> /// Runs the specified .NET Framework command-line app. /// </summary> /// <param name="path">The path of the command-line app.</param> /// <param name="settings">The settings to use when running the app.</param> /// <remarks>On Linux and macOS, Mono is used to run the app.</remarks> public static int RunDotNetFrameworkApp(string path, AppRunnerSettings settings) { if (BuildEnvironment.IsUnix()) { settings = settings.Clone(); settings.Arguments = new[] { path }.Concat(settings.Arguments ?? Enumerable.Empty <string>()).ToList(); path = "mono"; } return(RunApp(path, settings)); }
/// <summary> /// Runs the specified command-line app. /// </summary> /// <param name="path">The path of the command-line app.</param> /// <param name="settings">The settings to use when running the app.</param> public static int RunApp(string path, AppRunnerSettings settings) { // adapted from https://github.com/adamralph/simple-exec; allows non-exceptional non-zero edit code string args = EscapeAndConcatenate(settings.Arguments ?? Enumerable.Empty <string>()); var startInfo = new ProcessStartInfo { WorkingDirectory = settings.WorkingDirectory, UseShellExecute = false, RedirectStandardError = false, RedirectStandardOutput = false, }; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { startInfo.FileName = "cmd.exe"; startInfo.Arguments = $"/c \"\"{path}\" {args}\""; } else { startInfo.FileName = path; startInfo.Arguments = args; } if (!settings.NoEcho) { if (startInfo.WorkingDirectory.Length != 0) { Console.Error.WriteLine($"Working directory: {startInfo.WorkingDirectory}"); } Console.Error.WriteLine($"{startInfo.FileName} {startInfo.Arguments}"); } using (var process = new Process { StartInfo = startInfo }) { process.Start(); process.WaitForExit(); int exitCode = process.ExitCode; var isExitCodeSuccess = settings.IsExitCodeSuccess ?? (x => x == 0); if (!isExitCodeSuccess(exitCode)) { throw new ApplicationException($"The process failed with exit code {exitCode}."); } return(exitCode); } }
/// <summary> /// Runs the local tool with the specified settings. /// </summary> /// <param name="settings">The settings to use when running the tool.</param> public int Run(AppRunnerSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (settings.WorkingDirectory != null) { throw new ArgumentException($"{nameof(settings.WorkingDirectory)} not supported for local tools.", nameof(settings)); } settings = settings.Clone(); settings.Arguments = new[] { "tool", "run", m_name, "--" }.Concat(settings.Arguments ?? Enumerable.Empty <string>()); settings.WorkingDirectory = m_directory; return(RunDotNet(settings)); }
/// <summary> /// Runs <c>dotnet</c> with the specified settings. /// </summary> /// <param name="settings">The settings to use when running the app.</param> public static int RunDotNet(AppRunnerSettings settings) => RunApp(GetDotNetFullPath(), settings);
/// <summary> /// Runs the specified .NET tool with the specified settings. /// </summary> /// <param name="name">The name (or path) of the tool.</param> /// <param name="settings">The settings to use when running the app.</param> public static int RunDotNetTool(string name, AppRunnerSettings settings) { settings = settings.Clone(); settings.Arguments = new[] { "tool", "run", name, "--" }.Concat(settings.Arguments ?? Enumerable.Empty <string>()); return(RunDotNet(settings)); }
/// <summary> /// Runs MSBuild with the specified settings. /// </summary> /// <param name="settings">The MSBuild settings.</param> /// <param name="runnerSettings">The settings to use when running the app.</param> public static int RunMSBuild(MSBuildSettings?settings, AppRunnerSettings runnerSettings) => RunApp(GetMSBuildPath(settings), runnerSettings);
/// <summary> /// Runs <c>dotnet</c> with the specified settings. /// </summary> /// <param name="settings">The settings to use when running the app.</param> public static int RunDotNet(AppRunnerSettings settings) => RunApp(DotNetExe.FullPath, settings);