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;
}
}