예제 #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,
            };

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

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

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

            return(command);
        }
예제 #2
0
        /// <summary>
        /// Provides <see cref="CommandLineSyntax"/> functionality for windows
        /// </summary>
        public override string CreateArgumentString(IEnumerable <string> arguments)
        {
            Throw.IfNull(arguments, nameof(arguments));

            var builder         = new StringBuilder();
            var isFirstArgument = true;

            foreach (var argument in arguments)
            {
                Throw.If(argument == null, nameof(arguments) + ": must not contain null");

                if (isFirstArgument)
                {
                    isFirstArgument = false;
                }
                else
                {
                    builder.Append(' ');
                }
                AddArgument(argument, builder);
            }

            return(builder.ToString());
        }
예제 #3
0
 /// <summary>
 /// Throws <see cref="ObjectDisposedException"/> if the <see cref="Command"/> has been disposed
 /// </summary>
 protected void ThrowIfDisposed()
 {
     Throw <ObjectDisposedException> .If(Volatile.Read(ref this._disposed) != 0, () => this.ToString());
 }
예제 #4
0
 /// <summary>
 /// Throws an <see cref="ArgumentException"/> if the given condition is true
 /// </summary>
 public static void If(bool condition, string parameterName)
 {
     Throw <ArgumentException> .If(condition, parameterName);
 }
예제 #5
0
 /// <summary>
 /// Throws an <see cref="ArgumentNullException"/> if the given value is null
 /// </summary>
 public static void IfNull <T>(T value, string parameterName)
 {
     Throw <ArgumentNullException> .If(value == null, parameterName);
 }
예제 #6
0
            /// <summary>
            /// Adds or overwrites an environment variable to be passed to the <see cref="Command"/>
            /// </summary>
            public Options EnvironmentVariable(string name, string value)
            {
                Throw.If(string.IsNullOrEmpty(name), "name is required");

                return(this.StartInfo(psi => psi.EnvironmentVariables[name] = value));
            }