MonoDevelop.Projects.CustomCommand.Execute C# (CSharp) Method

Execute() public method

public Execute ( MonoDevelop.Core.ProgressMonitor monitor, WorkspaceObject entry, MonoDevelop.Projects.ExecutionContext context, MonoDevelop.Projects.ConfigurationSelector configuration ) : Task
monitor MonoDevelop.Core.ProgressMonitor
entry WorkspaceObject
context MonoDevelop.Projects.ExecutionContext
configuration MonoDevelop.Projects.ConfigurationSelector
return Task
		public async Task<bool> Execute (ProgressMonitor monitor, WorkspaceObject entry, ExecutionContext context,
			ConfigurationSelector configuration)
		{
			ProcessExecutionCommand cmd = CreateExecutionCommand (entry, configuration);
			
			monitor.Log.WriteLine (GettextCatalog.GetString ("Executing: {0} {1}", cmd.Command, cmd.Arguments));
			
			if (!Directory.Exists (cmd.WorkingDirectory)) {
				monitor.ReportError (GettextCatalog.GetString ("Custom command working directory does not exist"), null);
				return false;
			}
			
			ProcessAsyncOperation oper = null;
			OperationConsole console = null;
			var result = true;
			
			try {
				if (context != null) {
					if (externalConsole)
						console = context.ExternalConsoleFactory.CreateConsole (!pauseExternalConsole, monitor.CancellationToken);
					else
						console = context.ConsoleFactory.CreateConsole (monitor.CancellationToken);
					oper = context.ExecutionHandler.Execute (cmd, console);
				} else {
					if (externalConsole) {
						console = context.ExternalConsoleFactory.CreateConsole (!pauseExternalConsole, monitor.CancellationToken);
						oper = Runtime.ProcessService.StartConsoleProcess (cmd.Command, cmd.Arguments,
							cmd.WorkingDirectory, console, null);
					} else {
						oper = Runtime.ProcessService.StartProcess (cmd.Command, cmd.Arguments,
							cmd.WorkingDirectory, monitor.Log, monitor.Log, null, false).ProcessAsyncOperation;
					}
				}

				var stopper = monitor.CancellationToken.Register (oper.Cancel);

				await oper.Task;

				stopper.Dispose ();

				if (oper.ExitCode != 0) {
					monitor.ReportError ("Custom command failed (exit code: " + oper.ExitCode + ")", null);
				}
			} catch (Win32Exception w32ex) {
				monitor.ReportError (GettextCatalog.GetString ("Failed to execute custom command '{0}': {1}",
					cmd.Command, w32ex.Message), null);
				return false;
			} catch (Exception ex) {
				LoggingService.LogError ("Command execution failed", ex);
				throw new UserException (GettextCatalog.GetString ("Command execution failed: {0}", ex.Message));
			} finally {
				result = oper != null && oper.ExitCode == 0;
				if (console != null) {
					console.Dispose ();
				}
			}
			return result;
		}
	}

Same methods

CustomCommand::Execute ( MonoDevelop.Core.ProgressMonitor monitor, WorkspaceObject entry, MonoDevelop.Projects.ConfigurationSelector configuration ) : Task

Usage Example

        protected override void Run(object dataItem)
        {
            IWorkspaceObject ce      = IdeApp.ProjectOperations.CurrentSelectedBuildTarget;
            CustomCommand    cmd     = (CustomCommand)dataItem;
            IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetRunProgressMonitor();

            Thread t = new Thread(
                delegate()
            {
                using (monitor)
                {
                    try
                    {
                        cmd.Execute(monitor, ce, IdeApp.Workspace.ActiveConfiguration);
                    }
                    catch (Exception ex)
                    {
                        monitor.ReportError(GettextCatalog.GetString("Command execution failed"), ex);
                    }
                }
            }
                );

            t.IsBackground = true;
            t.Start();
        }
All Usage Examples Of MonoDevelop.Projects.CustomCommand::Execute