Пример #1
0
        /// <summary>
        /// Starts a process using the specified information.
        /// </summary>
        /// <param name="filePath">The file name such as an application or document with which to start the process.</param>
        /// <param name="settings">The information about the process to start.</param>
        /// <returns>A process handle.</returns>
        public IProcess Start(FilePath filePath, ProcessSettings settings)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // Get the arguments.
            var arguments = settings.Arguments ?? new ProcessArgumentBuilder();

            // Log the filename and arguments.
            var message = string.Concat(filePath, " ", arguments.RenderSafe().TrimEnd());

            _log.Verbose(Verbosity.Diagnostic, "Executing: {0}", message);

            // Get the working directory.
            var workingDirectory = settings.WorkingDirectory ?? _environment.WorkingDirectory;

            settings.WorkingDirectory = workingDirectory.MakeAbsolute(_environment);

            // Create the process start info.
            var info = new ProcessStartInfo(filePath.FullPath)
            {
                Arguments              = arguments.Render(),
                WorkingDirectory       = workingDirectory.FullPath,
                UseShellExecute        = false,
                RedirectStandardOutput = settings.RedirectStandardOutput
            };

            // Start and return the process.
            var process = Process.Start(info);

            return(process == null ? null : new ProcessWrapper(process, _log, arguments.FilterUnsafe));
        }
Пример #2
0
        /// <summary>
        /// Starts a process using the specified information.
        /// </summary>
        /// <param name="filePath">The file name such as an application or document with which to start the process.</param>
        /// <param name="settings">The information about the process to start.</param>
        /// <returns>A process handle.</returns>
        public IProcess Start(FilePath filePath, ProcessSettings settings)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            // Get the fileName
            var fileName = _environment.Platform.IsUnix() ? filePath.FullPath : filePath.FullPath.Quote();

            // Get the arguments.
            var arguments = settings.Arguments ?? new ProcessArgumentBuilder();

            if (!settings.Silent)
            {
                // Log the filename and arguments.
                var message = string.Concat(fileName, " ", arguments.RenderSafe().TrimEnd());
                _log.Verbose(Verbosity.Diagnostic, "Executing: {0}", message);
            }

            // Get the working directory.
            var workingDirectory = settings.WorkingDirectory ?? _environment.WorkingDirectory;

            settings.WorkingDirectory = workingDirectory.MakeAbsolute(_environment);

            // Create the process start info.
            var info = new ProcessStartInfo(fileName)
            {
                Arguments              = arguments.Render(),
                UseShellExecute        = false,
                RedirectStandardError  = settings.RedirectStandardError,
                RedirectStandardOutput = settings.RedirectStandardOutput
            };

            if (settings.WorkingDirectory != null)
            {
                info.WorkingDirectory = settings.WorkingDirectory.FullPath;
            }

            // Add environment variables
            ProcessHelper.SetEnvironmentVariable(info, "CAKE", "True");
            ProcessHelper.SetEnvironmentVariable(info, "CAKE_VERSION", _environment.Runtime.CakeVersion.ToString(3));
            if (settings.EnvironmentVariables != null)
            {
                foreach (var environmentVariable in settings.EnvironmentVariables)
                {
                    ProcessHelper.SetEnvironmentVariable(info, environmentVariable.Key, environmentVariable.Value);
                }
            }

            // Start and return the process.
            var process = Process.Start(info);

            if (process == null)
            {
                return(null);
            }

            var consoleOutputQueue = settings.RedirectStandardOutput
                ? SubscribeStandardConsoleOutputQueue(process)
                : null;

            var consoleErrorQueue = settings.RedirectStandardError
                ? SubscribeStandardConsoleErrorQueue(process)
                : null;

            return(new ProcessWrapper(process, _log, arguments.FilterUnsafe, consoleOutputQueue, arguments.FilterUnsafe, consoleErrorQueue));
        }
Пример #3
0
        internal ProcessStartInfo GetProcessStartInfo(FilePath filePath, ProcessSettings settings, out Func <string, string> filterUnsafe)
        {
            // Get the fileName
            var fileName = _environment.Platform.IsUnix() ? filePath.FullPath : filePath.FullPath.Quote();

            // Get the arguments.
            var arguments = settings.Arguments ?? new ProcessArgumentBuilder();

            filterUnsafe = arguments.FilterUnsafe;

            if (!_noMonoCoersion &&
                _environment.Platform.IsUnix() &&
                _environment.Runtime.IsCoreClr &&
                fileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) &&
                _fileSystem.GetFile(fileName).IsClrAssembly())
            {
                FilePath monoPath = _tools.Resolve("mono");
                if (monoPath != null)
                {
                    if (!settings.Silent)
                    {
                        _log.Verbose(Verbosity.Diagnostic, "{0} is a .NET Framework executable, will try execute using Mono.", fileName);
                    }
                    arguments.PrependQuoted(fileName);
                    fileName = monoPath.FullPath;
                }
                else
                {
                    if (!settings.Silent)
                    {
                        _log.Verbose(Verbosity.Diagnostic, "{0} is a .NET Framework executable, you might need to install Mono for it to execute successfully.", fileName);
                    }
                }
            }

            if (!settings.Silent)
            {
                // Log the filename and arguments.
                var message = string.Concat(fileName, " ", arguments.RenderSafe().TrimEnd());
                _log.Verbose(_showCommandLine ? _log.Verbosity : Verbosity.Diagnostic, "Executing: {0}", message);
            }

            // Create the process start info.
            var info = new ProcessStartInfo(fileName)
            {
                Arguments              = arguments.Render(),
                UseShellExecute        = false,
                RedirectStandardError  = settings.RedirectStandardError,
                RedirectStandardOutput = settings.RedirectStandardOutput
            };

            // Allow working directory?
            if (!settings.NoWorkingDirectory)
            {
                var workingDirectory = settings.WorkingDirectory ?? _environment.WorkingDirectory;
                info.WorkingDirectory = workingDirectory.MakeAbsolute(_environment).FullPath;
            }

            // Add environment variables
            ProcessHelper.SetEnvironmentVariable(info, "CAKE", "True");
            ProcessHelper.SetEnvironmentVariable(info, "CAKE_VERSION", _environment.Runtime.CakeVersion.ToString(3));
            if (settings.EnvironmentVariables != null)
            {
                foreach (var environmentVariable in settings.EnvironmentVariables)
                {
                    ProcessHelper.SetEnvironmentVariable(info, environmentVariable.Key, environmentVariable.Value);
                }
            }

            return(info);
        }