Ejemplo n.º 1
0
        internal virtual int RunAndWaitWithInput(string cmd, string workingdir, string input,
                                                 out string output, out string error)
        {
            Process p = InternalRun(cmd, workingdir, true);

            try
            {
                if (input != null && input != string.Empty)
                {
                    if (!PlatformIdentifier.IsWindows())
                    {
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(input);
                        p.StandardInput.BaseStream.Write(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        p.StandardInput.Write(input);
                    }
                    p.StandardInput.Flush();
                    p.StandardInput.Close();
                }

                output = p.StandardOutput.ReadToEnd();
                error  = p.StandardError.ReadToEnd();
                p.WaitForExit();

                return(p.ExitCode);
            }
            finally
            {
                p.Close();
            }
        }
        internal override int RunAndWait(string cmd, string workingdir, out string output,
                                         out string error, bool bShell)
        {
            if (!bShell || !cmd.StartsWith("cm"))
            {
                return(base.RunAndWait(cmd, workingdir, out output, out error));
            }

            workingdir = Path.GetFullPath(workingdir);
            if (mCmdProc == null)
            {
                mCmdProc = InitCmdProc(workingdir);
            }

            string command    = cmd.Substring(3);
            string outputfile = string.Empty;

            if (USE_FILE_COMMUNICATION)
            {
                outputfile = Path.GetTempFileName();

                if (File.Exists(outputfile))
                {
                    File.Delete(outputfile);
                }

                string cmdText = string.Format(
                    "{0} -path=\"{1}\" --shelloutputfile=\"{2}\" --stack",
                    command, workingdir, outputfile);

                mCmdProc.StandardInput.WriteLine(cmdText);
            }
            else
            {
                string cmdText = string.Format("{0} -path=\"{1}\"",
                                               command, workingdir);

                mCmdProc.StandardInput.WriteLine(cmdText);
            }

            output = string.Empty;
            int result = 0;

            if (USE_FILE_COMMUNICATION)
            {
                if (PlatformIdentifier.IsWindows())
                {
                    result = ReadFileOutputWindows(mCmdProc, outputfile,
                                                   out output, out error);
                }
                else
                {
                    result = ReadFileOutputUnix(mCmdProc, outputfile,
                                                out output, out error);
                }
            }
            else
            {
                result = ReadStdOutput(mCmdProc, out output, out error);
            }

            return(result);
        }