예제 #1
0
        /// <summary>
        /// This simply executes a binary and returns the exit code or throws if the binary failed to start
        /// <para>This method shares the same console and does not capture the output</para>
        /// <para>Use <see cref="Start(string,string[])"/> or overloads if you want to capture output and write to console in realtime</para>
        /// </summary>
        /// <param name="timeout">The maximum runtime of the started program</param>
        /// <exception cref="Exception">If the application fails to start</exception>
        /// <returns>The exit code of the binary being run</returns>
        public static int?Exec(ExecArguments arguments, TimeSpan timeout)
        {
            var args = string.Join(" ", arguments.Args ?? new string[] {});
            var info = new ProcessStartInfo(arguments.Binary, args)
            {
                UseShellExecute = false
            };

            var pwd = arguments.WorkingDirectory;

            if (!string.IsNullOrWhiteSpace(pwd))
            {
                info.WorkingDirectory = pwd;
            }
            if (arguments.Environment != null)
            {
                foreach (var kv in arguments.Environment)
                {
                #if NET45
                    info.EnvironmentVariables[kv.Key] = kv.Value;
                #else
                    info.Environment[kv.Key] = kv.Value;
                #endif
                }
            }

            var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
                                ? $"\"{arguments.Binary}\""
                                : $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";

            using (var process = new Process())
            {
                process.StartInfo = info;
                if (!process.Start())
                {
                    throw new ProcExecException($"Failed to start {printBinary}");
                }

                var completedBeforeTimeout = process.WaitForExit((int)timeout.TotalMilliseconds);
                if (!completedBeforeTimeout)
                {
                    HardWaitForExit(process, TimeSpan.FromSeconds(1));
                    throw new ProcExecException($"Timeout {timeout} occured while running {printBinary}");
                }

                var exitCode = process.ExitCode;
                if (!arguments.ValidExitCodeClassifier(exitCode))
                {
                    throw new ProcExecException($"Process exited with '{exitCode}' {printBinary}")
                          {
                              ExitCode = exitCode
                          }
                }
                ;

                return(exitCode);
            }
        }
예제 #2
0
 /// <summary>
 /// This simply executes a binary and returns the exit code or throws if the binary failed to start
 /// <para>This method shares the same console and does not capture the output</para>
 /// <para>Use <see cref="Start(string,string[])"/> or overloads if you want to capture output and write to console in realtime</para>
 /// </summary>
 /// <exception cref="Exception">If the application fails to start</exception>
 /// <returns>The exit code of the binary being run</returns>
 public static int?Exec(ExecArguments arguments) => Exec(arguments, DefaultTimeout);