Ejemplo n.º 1
0
        public void ExecuteCommand(string workingFolder, string command, string arguments)
        {
            ChildProcessParameters childProcessParameters = new ChildProcessParameters
                                                                {
                                                                    FileName = command,
                                                                    Arguments = arguments,
                                                                    WorkingDirectory = workingFolder
                                                                };

            ChildProcessExecutor executor = new ChildProcessExecutor();
            executor.ExecuteProcess(childProcessParameters);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs a child process.
        /// </summary>
        /// <param name="parameters">The parameters that are used to execute the child process.</param>
        public void ExecuteProcess(ChildProcessParameters parameters)
        {
            // Default to empty to prevent InvalidOperationException from ProcessOutput - if the process runs OK
            // then we will update
            this.processOutput = string.Empty;
            bool grabExternalLog = !string.IsNullOrEmpty(parameters.LogFileName);

            string hostName = GetHostName(parameters.FileName);
            var logFileMonitors = LogFileMonitor.CreateMonitors(
                parameters.ProcessTitle,
                hostName,
                parameters.Log,
                parameters.LogFilesToMonitor);

            try
            {
                var info = new ProcessStartInfo
                               {
                                   FileName = parameters.FileName,
                                   Arguments = parameters.Arguments,
                                   WorkingDirectory = parameters.WorkingDirectory,
                                   UseShellExecute = false,
                                   RedirectStandardOutput = grabExternalLog,
                                   RedirectStandardError = grabExternalLog,
                                   CreateNoWindow = true
                               };

                Process process = null;
                try
                {
                    process = Process.Start(info);
                }
                catch (Win32Exception ex)
                {
                    if (ex.NativeErrorCode == Win32FileNotFound)
                    {
                        throw new FileNotFoundException(
                            string.Format("The system could not find the file '{0}'", parameters.FileName),
                            parameters.FileName,
                            ex);
                    }
                }

                if (process == null)
                {
                    // TODO: determine the circumstances in which this situation could occur
                    string message = string.Format("Unable to start the {0} process", parameters.ProcessTitle);
                    throw new InvalidOperationException(message);
                }

                var reader = new ProcessOutputReader(process);
                if (grabExternalLog)
                {
                    reader.ReadProcessOutput();
                }

                if (!parameters.LogFilesToMonitor.Any())
                {
                    process.WaitForExit();
                }
                else
                {
                    do
                    {
                        foreach (var logFileMonitor in logFileMonitors)
                        {
                            logFileMonitor.PollFile();
                        }

                        Thread.Sleep(POLLINTERVAL);
                    }
                    while (!process.HasExited);
                }

                if (grabExternalLog)
                {
                    this.processOutput = reader.CombinedOutput;
                    WriteLogFile(parameters.LogFileName, reader.CombinedOutput);
                }

                if (process.ExitCode == 1 && hostName != "localhost")
                {
                    string message = string.Format(
                        "The {1} process exited with code {2}. " +
                        "This generally means that the MSBuild task started by the deployer has failed. " +
                        "Check the log file on the remote machine {3} for details",
                        Environment.NewLine,
                        parameters.ProcessTitle,
                        process.ExitCode,
                        hostName);

                    throw new InvalidOperationException(message);
                }
                else if (process.ExitCode == 1)
                {
                    string message = string.Format(
                        "The {1} process exited with code {2}. " +
                        "This generally means that the MSBuild task started by the deployer has failed. " +
                        "Check the log file on the local machine for details",
                        Environment.NewLine,
                        parameters.ProcessTitle,
                        process.ExitCode);

                    throw new InvalidOperationException(message);
                }
                else if (process.ExitCode != 0)
                {
                    string message = string.Format(
                        "The {0} process exited with code {1}",
                        parameters.ProcessTitle,
                        process.ExitCode);

                    throw new InvalidOperationException(message);
                }
            }
            catch (FileNotFoundException ex)
            {
                string message = string.Format(
                    "File '{0}' was not found for process {1}", parameters.FileName, parameters.ProcessTitle);
                throw new InvalidOperationException(message, ex);
            }
        }