use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.
the class ExternalSystemUtil method runTask.
public static void runTask(@NotNull final ExternalSystemTaskExecutionSettings taskSettings, @NotNull final String executorId, @NotNull final Project project, @NotNull final ProjectSystemId externalSystemId, @Nullable final TaskCallback callback, @NotNull final ProgressExecutionMode progressExecutionMode, boolean activateToolWindowBeforeRun) {
ExecutionEnvironment environment = createExecutionEnvironment(project, externalSystemId, taskSettings, executorId);
if (environment == null)
return;
RunnerAndConfigurationSettings runnerAndConfigurationSettings = environment.getRunnerAndConfigurationSettings();
assert runnerAndConfigurationSettings != null;
runnerAndConfigurationSettings.setActivateToolWindowBeforeRun(activateToolWindowBeforeRun);
final TaskUnderProgress task = new TaskUnderProgress() {
@Override
public void execute(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
final Semaphore targetDone = new Semaphore();
final Ref<Boolean> result = new Ref<>(false);
final Disposable disposable = Disposer.newDisposable();
project.getMessageBus().connect(disposable).subscribe(ExecutionManager.EXECUTION_TOPIC, new ExecutionListener() {
public void processStartScheduled(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
targetDone.down();
}
}
public void processNotStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
targetDone.up();
}
}
public void processStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal, @NotNull final ProcessHandler handler) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
handler.addProcessListener(new ProcessAdapter() {
public void processTerminated(ProcessEvent event) {
result.set(event.getExitCode() == 0);
targetDone.up();
}
});
}
}
});
try {
ApplicationManager.getApplication().invokeAndWait(() -> {
try {
environment.getRunner().execute(environment);
} catch (ExecutionException e) {
targetDone.up();
LOG.error(e);
}
}, ModalityState.defaultModalityState());
} catch (Exception e) {
LOG.error(e);
Disposer.dispose(disposable);
return;
}
targetDone.waitFor();
Disposer.dispose(disposable);
if (callback != null) {
if (result.get()) {
callback.onSuccess();
} else {
callback.onFailure();
}
}
}
};
final String title = AbstractExternalSystemTaskConfigurationType.generateName(project, taskSettings);
switch(progressExecutionMode) {
case NO_PROGRESS_SYNC:
task.execute(new EmptyProgressIndicator());
break;
case MODAL_SYNC:
new Task.Modal(project, title, true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
break;
case NO_PROGRESS_ASYNC:
ApplicationManager.getApplication().executeOnPooledThread(() -> task.execute(new EmptyProgressIndicator()));
break;
case IN_BACKGROUND_ASYNC:
new Task.Backgroundable(project, title) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
break;
case START_IN_FOREGROUND_ASYNC:
new Task.Backgroundable(project, title, true, PerformInBackgroundOption.DEAF) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
}
}
use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.
the class Tool method execute.
public void execute(AnActionEvent event, DataContext dataContext, long executionId, @Nullable final ProcessListener processListener) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null) {
return;
}
FileDocumentManager.getInstance().saveAllDocuments();
try {
if (isUseConsole()) {
ExecutionEnvironment environment = ExecutionEnvironmentBuilder.create(project, DefaultRunExecutor.getRunExecutorInstance(), new ToolRunProfile(this, dataContext)).build();
environment.setExecutionId(executionId);
environment.getRunner().execute(environment, new ProgramRunner.Callback() {
@Override
public void processStarted(RunContentDescriptor descriptor) {
ProcessHandler processHandler = descriptor.getProcessHandler();
if (processHandler != null && processListener != null) {
LOG.assertTrue(!processHandler.isStartNotified(), "ProcessHandler is already startNotified, the listener won't be correctly notified");
processHandler.addProcessListener(processListener);
}
}
});
} else {
GeneralCommandLine commandLine = createCommandLine(dataContext);
if (commandLine == null) {
return;
}
OSProcessHandler handler = new OSProcessHandler(commandLine);
handler.addProcessListener(new ToolProcessAdapter(project, synchronizeAfterExecution(), getName()));
if (processListener != null) {
handler.addProcessListener(processListener);
}
handler.startNotify();
}
} catch (ExecutionException ex) {
ExecutionErrorDialog.show(ex, ToolsBundle.message("tools.process.start.error"), project);
}
}
use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.
the class ExecutorRegistryImpl method initComponent.
@Override
public void initComponent() {
ProjectManager.getInstance().addProjectManagerListener(new ProjectManagerAdapter() {
@Override
public void projectOpened(final Project project) {
final MessageBusConnection connect = project.getMessageBus().connect(project);
connect.subscribe(ExecutionManager.EXECUTION_TOPIC, new ExecutionListener() {
@Override
public void processStartScheduled(@NotNull String executorId, @NotNull ExecutionEnvironment environment) {
myInProgress.add(createExecutionId(executorId, environment));
}
@Override
public void processNotStarted(@NotNull String executorId, @NotNull ExecutionEnvironment environment) {
myInProgress.remove(createExecutionId(executorId, environment));
}
@Override
public void processStarted(@NotNull String executorId, @NotNull ExecutionEnvironment environment, @NotNull ProcessHandler handler) {
myInProgress.remove(createExecutionId(executorId, environment));
}
});
}
@Override
public void projectClosed(final Project project) {
// perform cleanup
synchronized (myInProgress) {
for (Iterator<Trinity<Project, String, String>> it = myInProgress.iterator(); it.hasNext(); ) {
final Trinity<Project, String, String> trinity = it.next();
if (project.equals(trinity.first)) {
it.remove();
}
}
}
}
});
final Executor[] executors = Extensions.getExtensions(Executor.EXECUTOR_EXTENSION_NAME);
for (Executor executor : executors) {
initExecutor(executor);
}
}
use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.
the class EOFAction method update.
@Override
public void update(AnActionEvent e) {
RunContentDescriptor descriptor = StopAction.getRecentlyStartedContentDescriptor(e.getDataContext());
ProcessHandler handler = descriptor != null ? descriptor.getProcessHandler() : null;
e.getPresentation().setEnabledAndVisible(e.getData(LangDataKeys.CONSOLE_VIEW) != null && e.getData(CommonDataKeys.EDITOR) != null && handler != null && !handler.isProcessTerminated());
}
use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.
the class ExecutionManagerTest method testRerunSingleton.
public void testRerunSingleton() throws Exception {
Project project = getProject();
ExecutionManagerImpl executionManager = ExecutionManagerImpl.getInstance(project);
FakeRunConfiguration rc = new FakeRunConfiguration(project, true);
RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(project), rc, false);
settings.setSingleton(true);
ExecutionEnvironment env1 = createEnv(project, settings);
executionManager.restartRunProfile(env1);
UIUtil.dispatchAllInvocationEvents();
ProcessHandler processHandler1 = getProcessHandler(executionManager);
ExecutionEnvironment env2 = createEnv(project, settings);
executionManager.restartRunProfile(env2);
// Dispatching all events at this point will run into an endless cycle, because
// runContentDescriptors of the same type are asked to terminate and then are awaited for termination:
// com.intellij.execution.impl.ExecutionManagerImpl.awaitTermination
//
// However, the created processHandler is not willing to terminate on the first request (surviveSoftKill=true).
// It will be terminated on the second request: executionManager.restartRunProfile(env3)
ProcessHandler processHandler2 = getProcessHandler(executionManager);
assertTrue(processHandler1 == processHandler2);
assertTrue(processHandler1.isProcessTerminating());
ExecutionEnvironment env3 = createEnv(project, settings);
executionManager.restartRunProfile(env3);
UIUtil.dispatchAllInvocationEvents();
FakeProcessHandler processHandler3 = getProcessHandler(executionManager);
assertTrue(processHandler1 != processHandler3);
assertTrue(!processHandler3.isProcessTerminating() && !processHandler3.isProcessTerminated());
processHandler3.killProcess();
}
Aggregations