Ejemplo n.º 1
0
        /// <summary>
        /// Runs a custom tool.
        /// Returns a string with a tool output to be printed out.
        /// This string can be empty, in which case nothing should be printed.
        /// </summary>
        public string Run(List <string> files)
        {
            App.PrintLogMessage(ToString(), MessageType.Command);

            string stdout = string.Empty;
            string args   = DeMacroise(Args, files);

            // Add custom arguments if the checkbox to Prompt for Arguments was checked
            if (IsPromptForArgs)
            {
                // Description is used as a question for the arguments, shown in the window title bar
                string desc = Name;
                if (!string.IsNullOrEmpty(Desc))
                {
                    desc += ": " + Desc;
                }

                FormCustomToolArgs formCustomToolArgs = new FormCustomToolArgs(desc, args, IsAddBrowse);
                if (formCustomToolArgs.ShowDialog() == DialogResult.Cancel)
                {
                    return(string.Empty);
                }

                args = formCustomToolArgs.GetArgs();
            }

            App.StatusBusy(true);

            // Prepare the process to be run
            Process proc = new Process();

            proc.StartInfo.FileName         = "\"" + Cmd + "\"";
            proc.StartInfo.Arguments        = args;
            proc.StartInfo.WorkingDirectory = DeMacroise(Dir, new List <string>());
            proc.StartInfo.UseShellExecute  = false;

            try
            {
                // Run the custom tool in two ways (console app and GUI app)
                if (IsConsoleApp)
                {
                    // Start a console process
                    proc.StartInfo.CreateNoWindow = false;

                    // If we have to keep the window open (CMD/SHELL) after exit,
                    // we start the command line app in a different way, using a
                    // shell command (in which case we cannot redirect the stdout)
                    if (IsCloseWindowOnExit)
                    {
                        App.MainForm.SetTitle("Waiting for " + Cmd + " to finish...");

                        // Redirect standard output to our status pane if requested
                        if (IsWriteOutput)
                        {
                            proc.StartInfo.RedirectStandardOutput = true;
                            proc.StartInfo.RedirectStandardError  = true;
                            proc.OutputDataReceived += ProcOutputDataReceived;
                            proc.ErrorDataReceived  += ProcErrorDataReceived;
                            proc.Start();
                            proc.BeginOutputReadLine();
                            proc.WaitForExit();
                        }
                        else
                        {
                            proc.Start();
                            proc.WaitForExit();
                        }
                    }
                    else
                    {
                        // We need to keep the CMD/SHELL window open, so start the process using
                        // the CMD/SHELL as the root process and pass it our command to execute
                        proc.StartInfo.Arguments = string.Format("{0} {1} {2}",
                                                                 ClassUtils.GetShellExecFlags(), proc.StartInfo.FileName, proc.StartInfo.Arguments);
                        proc.StartInfo.FileName = ClassUtils.GetShellExecCmd();
                        App.PrintLogMessage(proc.StartInfo.Arguments, MessageType.Command);

                        proc.Start();
                    }
                }
                else
                {
                    // Start a GUI process
                    proc.StartInfo.CreateNoWindow = true;

                    // We can start the process and wait for it to finish only if we need to
                    // refresh the app after the process has exited.
                    proc.Start();
                }

                if (IsRefresh)
                {
                    App.MainForm.SetTitle("Waiting for " + Cmd + " to finish...");

                    proc.WaitForExit();

                    App.DoRefresh();
                }
            }
            catch (Exception ex)
            {
                App.PrintStatusMessage(ex.Message, MessageType.Error);
                MessageBox.Show(ex.Message, "Error executing custom tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            proc.Close();
            App.StatusBusy(false);

            return(stdout);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs a custom tool.
        /// Returns a string with a tool output to be printed out.
        /// This string can be empty, in which case nothing should be printed.
        /// </summary>
        public string Run(List<string> files)
        {
            App.PrintLogMessage(ToString(), MessageType.Command);

            string stdout = string.Empty;
            string args = DeMacroise(Args, files);

            // Add custom arguments if the checkbox to Prompt for Arguments was checked
            if (IsPromptForArgs)
            {
                // Description is used as a question for the arguments, shown in the window title bar
                string desc = Name;
                if (!string.IsNullOrEmpty(Desc)) desc += ": " + Desc;

                FormCustomToolArgs formCustomToolArgs = new FormCustomToolArgs(desc, args, IsAddBrowse);
                if (formCustomToolArgs.ShowDialog() == DialogResult.Cancel)
                    return string.Empty;

                args = formCustomToolArgs.GetArgs();
            }

            App.StatusBusy(true);

            // Prepare the process to be run
            Process proc = new Process();
            proc.StartInfo.FileName = "\"" + Cmd + "\"";
            proc.StartInfo.Arguments = args;
            proc.StartInfo.WorkingDirectory = DeMacroise(Dir, new List<string>());
            proc.StartInfo.UseShellExecute = false;

            try
            {
                // Run the custom tool in two ways (console app and GUI app)
                if (IsConsoleApp)
                {
                    // Start a console process
                    proc.StartInfo.CreateNoWindow = false;

                    // If we have to keep the window open (CMD/SHELL) after exit,
                    // we start the command line app in a different way, using a
                    // shell command (in which case we cannot redirect the stdout)
                    if (IsCloseWindowOnExit)
                    {
                        App.MainForm.SetTitle("Waiting for " + Cmd + " to finish...");

                        // Redirect standard output to our status pane if requested
                        if (IsWriteOutput)
                        {
                            proc.StartInfo.RedirectStandardOutput = true;
                            proc.StartInfo.RedirectStandardError = true;
                            proc.OutputDataReceived += ProcOutputDataReceived;
                            proc.ErrorDataReceived += ProcErrorDataReceived;
                            proc.Start();
                            proc.BeginOutputReadLine();
                            proc.WaitForExit();
                        }
                        else
                        {
                            proc.Start();
                            proc.WaitForExit();
                        }
                    }
                    else
                    {
                        // We need to keep the CMD/SHELL window open, so start the process using
                        // the CMD/SHELL as the root process and pass it our command to execute
                        proc.StartInfo.Arguments = string.Format("{0} {1} {2}",
                            ClassUtils.GetShellExecFlags(), proc.StartInfo.FileName, proc.StartInfo.Arguments);
                        proc.StartInfo.FileName = ClassUtils.GetShellExecCmd();
                        App.PrintLogMessage(proc.StartInfo.Arguments, MessageType.Command);

                        proc.Start();
                    }
                }
                else
                {
                    // Start a GUI process
                    proc.StartInfo.CreateNoWindow = true;

                    // We can start the process and wait for it to finish only if we need to
                    // refresh the app after the process has exited.
                    proc.Start();
                }

                if (IsRefresh)
                {
                    App.MainForm.SetTitle("Waiting for " + Cmd + " to finish...");

                    proc.WaitForExit();

                    App.DoRefresh();
                }
            }
            catch (Exception ex)
            {
                App.PrintStatusMessage(ex.Message, MessageType.Error);
                MessageBox.Show(ex.Message, "Error executing custom tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            proc.Close();
            App.StatusBusy(false);

            return stdout;
        }