예제 #1
0
        internal static int ExecuteCommand(string executable, string args, bool writeOutput = true, string workingDirectory = "")
        {
            var command = $"{executable} {args}";

            if (writeOutput)
            {
                Output.CommandExecution(command);
            }

            var consoleColour = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Gray;

            Process p;

            try
            {
                p = Process.Start(new ProcessStartInfo
                {
                    FileName               = executable,
                    Arguments              = args,
                    RedirectStandardError  = !writeOutput,
                    RedirectStandardOutput = !writeOutput,
                    WorkingDirectory       = workingDirectory,
                });
            }
            catch (Exception e)
            {
                throw new Exception($"Failed to start command `{command}`", e);
            }

            p.WaitForExit();

            Console.ForegroundColor = consoleColour;

            if (writeOutput)
            {
                Output.ExitCode(executable, p.ExitCode);
            }

            if (p.ExitCode != 0 && writeOutput)
            {
                Output.Error($"`{command}` exited with code {p.ExitCode}");
            }

            return(p.ExitCode);
        }