Beispiel #1
0
        /// <summary>
        /// Executes the given <paramref name="executable"/> with the given <paramref name="arguments"/> and
        /// <paramref name="options"/>
        /// </summary>
        public Command Run(string executable, IEnumerable <object> arguments = null, Action <Options> options = null)
        {
            Throw.If(string.IsNullOrEmpty(executable), "executable is required");

            var finalOptions = this.GetOptions(options);

            var processStartInfo = new ProcessStartInfo
            {
                Arguments = arguments != null
                    ? finalOptions.CommandLineSyntax.CreateArgumentString(arguments.Select(arg => Convert.ToString(arg, CultureInfo.InvariantCulture)))
                    : string.Empty,
                CreateNoWindow         = true,
                FileName               = executable,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
            };

            finalOptions.StartInfoInitializers.ForEach(a => a(processStartInfo));

            var command = new ProcessCommand(
                processStartInfo,
                throwOnError: finalOptions.ThrowExceptionOnError,
                disposeOnExit: finalOptions.DisposeProcessOnExit,
                timeout: finalOptions.ProcessTimeout
                );

            finalOptions.CommandInitializers.ForEach(a => a(command));

            return(command);
        }
Beispiel #2
0
        /// <summary>
        /// Executes the given <paramref name="executable"/> with the given <paramref name="arguments"/> and
        /// <paramref name="options"/>
        /// </summary>
        public Command Run(string executable, IEnumerable<object> arguments = null, Action<Options> options = null)
        {
            Throw.If(string.IsNullOrEmpty(executable), "executable is required");

            var finalOptions = this.GetOptions(options);

            var processStartInfo = new ProcessStartInfo
            {
                Arguments = arguments != null 
                    ? finalOptions.CommandLineSyntax.CreateArgumentString(arguments.Select(arg => Convert.ToString(arg, CultureInfo.InvariantCulture)))
                    : string.Empty,
                CreateNoWindow = true,
                FileName = executable,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
            };
            finalOptions.StartInfoInitializers.ForEach(a => a(processStartInfo));

            var command = new ProcessCommand(
                processStartInfo, 
                throwOnError: finalOptions.ThrowExceptionOnError,
                disposeOnExit: finalOptions.DisposeProcessOnExit,
                timeout: finalOptions.ProcessTimeout
            );
            finalOptions.CommandInitializers.ForEach(a => a(command));

            return command;
        }
Beispiel #3
0
        /// <summary>
        /// Executes the given <paramref name="executable"/> with the given <paramref name="arguments"/> and
        /// <paramref name="options"/>
        /// </summary>
        public Command Run(string executable, IEnumerable <object> arguments = null, Action <Options> options = null)
        {
            Throw.If(string.IsNullOrEmpty(executable), "executable is required");

            var finalOptions = this.GetOptions(options);

            var processStartInfo = new ProcessStartInfo
            {
                Arguments = arguments != null
                    ? finalOptions.CommandLineSyntax.CreateArgumentString(arguments.Select(arg => Convert.ToString(arg, CultureInfo.InvariantCulture)))
                    : string.Empty,
                CreateNoWindow         = true,
                FileName               = executable,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
            };

            if (finalOptions.ProcessStreamEncoding != null)
            {
                processStartInfo.StandardOutputEncoding = processStartInfo.StandardErrorEncoding = finalOptions.ProcessStreamEncoding;
            }
            finalOptions.StartInfoInitializers.ForEach(a => a(processStartInfo));

            Command command = new ProcessCommand(
                processStartInfo,
                throwOnError: finalOptions.ThrowExceptionOnError,
                disposeOnExit: finalOptions.DisposeProcessOnExit,
                timeout: finalOptions.ProcessTimeout,
                cancellationToken: finalOptions.ProcessCancellationToken,
                standardInputEncoding: finalOptions.ProcessStreamEncoding
                );

            foreach (var initializer in finalOptions.CommandInitializers)
            {
                command = initializer(command);
                if (command == null)
                {
                    throw new InvalidOperationException($"{nameof(Command)} initializer passed to {nameof(Options)}.{nameof(Options.Command)} must not return null!");
                }
            }

            return(command);
        }