예제 #1
0
        static private bool RunCommand(OutputWindowPane output, string executable, string commandline, string workingdir, int timeout)
        {
            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName        = executable;
                if (0 == timeout)
                {
                    // We are not for these processes reading the stdout and thus they could if they wrote more
                    // data on the output line hang.
                    process.StartInfo.RedirectStandardOutput = false;
                    process.StartInfo.RedirectStandardError  = false;
                }
                else
                {
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                }
                process.StartInfo.CreateNoWindow   = true;
                process.StartInfo.WorkingDirectory = workingdir;
                process.StartInfo.Arguments        = commandline;

                Log.Debug("executableName : " + executable);
                Log.Debug("workingDirectory : " + workingdir);
                Log.Debug("command : " + commandline);

                if (!process.Start())
                {
                    Log.Error("{0}: {1} Failed to start. Is Perforce installed and in the path?\n", executable, commandline);
                    return(false);
                }

                if (0 == timeout)
                {
                    // Fire and forget task.
                    return(true);
                }

                bool   exited    = false;
                string alloutput = "";
                using (Process.Handler stderr = new Process.Handler(), stdout = new Process.Handler())
                {
                    process.OutputDataReceived += stdout.OnOutput;
                    process.BeginOutputReadLine();

                    process.ErrorDataReceived += stderr.OnOutput;
                    process.BeginErrorReadLine();

                    exited = process.WaitForExit(timeout);

                    /*
                     * This causes the plugin to unexpectedly crash, since it brings the entire thread down, and thus the entire environment?!?
                     *
                     *
                     * if (0 != process.ExitCode)
                     * {
                     *      throw new Process.Error("Failed to execute {0} {1}, exit code was {2}", executable, process.StartInfo.Arguments, process.ExitCode);
                     * }*/

                    stderr.sentinel.WaitOne();
                    stdout.sentinel.WaitOne();
                    alloutput = stdout.buffer + "\n" + stderr.buffer;
                }

                if (!exited)
                {
                    Log.Info("{0}: {1} timed out ({2} ms)", executable, commandline, timeout);
                    process.Kill();
                    return(false);
                }
                else
                {
                    if (null != output)
                    {
                        output.OutputString(executable + ": " + commandline + "\n");
                        output.OutputString(alloutput);
                    }

                    System.Diagnostics.Debug.WriteLine(commandline + "\n");
                    System.Diagnostics.Debug.WriteLine(alloutput);

                    if (0 != process.ExitCode)
                    {
                        Log.Debug("{0}: {1} exit code {2}", executable, commandline, process.ExitCode);
                        return(false);
                    }
                }

                return(true);
            }
            catch (System.ComponentModel.Win32Exception e)
            {
                Log.Error("{0}: {1} failed to spawn: {2}", executable, commandline, e.ToString());
                return(false);
            }
        }
		static private bool RunCommand(OutputWindowPane output, string executable, string commandline, string workingdir, int timeout)
		{
			try
			{
				System.Diagnostics.Process process = new System.Diagnostics.Process();
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.FileName = executable;
				if( 0 == timeout )
				{
					// We are not for these processes reading the stdout and thus they could if they wrote more
					// data on the output line hang.
					process.StartInfo.RedirectStandardOutput = false;
					process.StartInfo.RedirectStandardError = false;
				}
				else
				{
					process.StartInfo.RedirectStandardOutput = true;
					process.StartInfo.RedirectStandardError = true;
				}
				process.StartInfo.CreateNoWindow = true;
				process.StartInfo.WorkingDirectory = workingdir;
				process.StartInfo.Arguments = commandline;

				Log.Debug("executableName : " + executable);
				Log.Debug("workingDirectory : " + workingdir);
				Log.Debug("command : " + commandline);

				if(!process.Start())
				{
					Log.Error("{0}: {1} Failed to start. Is Perforce installed and in the path?\n", executable, commandline);
					return false;
				}

                if (0 == timeout)
                {
                    // Fire and forget task.
                    return true;
                }
				
				bool exited = false;
				string alloutput = "";
				using (Process.Handler stderr = new Process.Handler(), stdout = new Process.Handler())
				{
					process.OutputDataReceived += stdout.OnOutput;
					process.BeginOutputReadLine();

					process.ErrorDataReceived += stderr.OnOutput;
					process.BeginErrorReadLine();

					exited = process.WaitForExit(timeout);

					/*
                     * This causes the plugin to unexpectedly crash, since it brings the entire thread down, and thus the entire environment?!?
                     * 
                    
                    if (0 != process.ExitCode)
					{
						throw new Process.Error("Failed to execute {0} {1}, exit code was {2}", executable, process.StartInfo.Arguments, process.ExitCode);
					}*/

					stderr.sentinel.WaitOne();
					stdout.sentinel.WaitOne();
					alloutput = stdout.buffer + "\n" + stderr.buffer;
				}
				
				if(!exited)
				{
					Log.Info("{0}: {1} timed out ({2} ms)", executable, commandline, timeout);
                    process.Kill();
					return false;
				}
				else
				{
					if(null != output)
					{
						output.OutputString(executable + ": " + commandline + "\n");
						output.OutputString(alloutput);
					}

					System.Diagnostics.Debug.WriteLine(commandline + "\n");
					System.Diagnostics.Debug.WriteLine(alloutput);

					if(0 != process.ExitCode)
					{
						Log.Debug("{0}: {1} exit code {2}", executable, commandline, process.ExitCode);
						return false;
					}
				}

				return true;
			}
			catch(System.ComponentModel.Win32Exception e)
			{
				Log.Error("{0}: {1} failed to spawn: {2}", executable, commandline, e.ToString());
				return false;
			}
		}
예제 #3
0
        private static bool RunCommand(string executable, string commandline, string workingdir, int timeout)
        {
            try
            {
                using (var process = new System.Diagnostics.Process())
                {
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.FileName        = executable;
                    if (0 == timeout)
                    {
                        // We are not for these processes reading the stdout and thus they could if they wrote more
                        // data on the output line hang.
                        process.StartInfo.RedirectStandardOutput = false;
                        process.StartInfo.RedirectStandardError  = false;
                    }
                    else
                    {
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError  = true;
                    }

                    process.StartInfo.CreateNoWindow   = true;
                    process.StartInfo.WorkingDirectory = workingdir;
                    process.StartInfo.Arguments        = commandline;

                    Log.Debug("executableName : " + executable);
                    Log.Debug("workingDirectory : " + workingdir);
                    Log.Debug("command : " + commandline);

                    if (!process.Start())
                    {
                        Log.Error("{0}: {1} Failed to start. Is Perforce installed and in the path?\n", executable, commandline);
                        return(false);
                    }

                    if (0 == timeout)
                    {
                        // Fire and forget task.
                        return(true);
                    }

                    bool   exited    = false;
                    string alloutput = "";
                    using (Process.Handler stderr = new Process.Handler(), stdout = new Process.Handler())
                    {
                        process.OutputDataReceived += stdout.OnOutput;
                        process.BeginOutputReadLine();

                        process.ErrorDataReceived += stderr.OnOutput;
                        process.BeginErrorReadLine();

                        exited = process.WaitForExit(timeout);

                        stderr.Sentinel.WaitOne();
                        stdout.Sentinel.WaitOne();
                        alloutput = stdout.Buffer + "\n" + stderr.Buffer;
                    }

                    if (!exited)
                    {
                        Log.Info("{0}: {1} timed out ({2} ms)", executable, commandline, timeout);
                        process.Kill();
                        return(false);
                    }
                    else
                    {
                        Log.Info(executable + ": " + commandline);
                        Log.Info(alloutput);

                        if (0 != process.ExitCode)
                        {
                            Log.Debug("{0}: {1} exit code {2}", executable, commandline, process.ExitCode);
                            return(false);
                        }
                    }
                }

                return(true);
            }
            catch (System.ComponentModel.Win32Exception e)
            {
                Log.Error("{0}: {1} failed to spawn: {2}", executable, commandline, e.ToString());
                return(false);
            }
        }