Search in sources :

Example 11 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment 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();
}
Also used : Project(com.intellij.openapi.project.Project) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessHandler(com.intellij.execution.process.ProcessHandler)

Example 12 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class ExecutionManagerImpl method compileAndRun.

@Override
public void compileAndRun(@NotNull final Runnable startRunnable, @NotNull final ExecutionEnvironment environment, @Nullable final RunProfileState state, @Nullable final Runnable onCancelRunnable) {
    long id = environment.getExecutionId();
    if (id == 0) {
        id = environment.assignNewExecutionId();
    }
    RunProfile profile = environment.getRunProfile();
    if (!(profile instanceof RunConfiguration)) {
        startRunnable.run();
        return;
    }
    final RunConfiguration runConfiguration = (RunConfiguration) profile;
    final List<BeforeRunTask> beforeRunTasks = RunManagerEx.getInstanceEx(myProject).getBeforeRunTasks(runConfiguration);
    if (beforeRunTasks.isEmpty()) {
        startRunnable.run();
    } else {
        DataContext context = environment.getDataContext();
        final DataContext projectContext = context != null ? context : SimpleDataContext.getProjectContext(myProject);
        final long finalId = id;
        final Long executionSessionId = new Long(id);
        ApplicationManager.getApplication().executeOnPooledThread(() -> {
            for (BeforeRunTask task : beforeRunTasks) {
                if (myProject.isDisposed()) {
                    return;
                }
                @SuppressWarnings("unchecked") BeforeRunTaskProvider<BeforeRunTask> provider = BeforeRunTaskProvider.getProvider(myProject, task.getProviderId());
                if (provider == null) {
                    LOG.warn("Cannot find BeforeRunTaskProvider for id='" + task.getProviderId() + "'");
                    continue;
                }
                ExecutionEnvironment taskEnvironment = new ExecutionEnvironmentBuilder(environment).contentToReuse(null).build();
                taskEnvironment.setExecutionId(finalId);
                EXECUTION_SESSION_ID_KEY.set(taskEnvironment, executionSessionId);
                if (!provider.executeTask(projectContext, runConfiguration, taskEnvironment, task)) {
                    if (onCancelRunnable != null) {
                        SwingUtilities.invokeLater(onCancelRunnable);
                    }
                    return;
                }
            }
            doRun(environment, startRunnable);
        });
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) RunProfile(com.intellij.execution.configurations.RunProfile) CompatibilityAwareRunProfile(com.intellij.execution.configuration.CompatibilityAwareRunProfile) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder)

Example 13 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class ExecutionManagerImpl method startRunProfile.

@Override
public void startRunProfile(@NotNull final RunProfileStarter starter, @NotNull final RunProfileState state, @NotNull final ExecutionEnvironment environment) {
    final Project project = environment.getProject();
    RunContentDescriptor reuseContent = getContentManager().getReuseContent(environment);
    if (reuseContent != null) {
        reuseContent.setExecutionId(environment.getExecutionId());
        environment.setContentToReuse(reuseContent);
    }
    final Executor executor = environment.getExecutor();
    project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStartScheduled(executor.getId(), environment);
    Runnable startRunnable;
    startRunnable = () -> {
        if (project.isDisposed()) {
            return;
        }
        RunProfile profile = environment.getRunProfile();
        project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStarting(executor.getId(), environment);
        starter.executeAsync(state, environment).done(descriptor -> {
            AppUIUtil.invokeOnEdt(() -> {
                if (descriptor != null) {
                    final Trinity<RunContentDescriptor, RunnerAndConfigurationSettings, Executor> trinity = Trinity.create(descriptor, environment.getRunnerAndConfigurationSettings(), executor);
                    myRunningConfigurations.add(trinity);
                    Disposer.register(descriptor, () -> myRunningConfigurations.remove(trinity));
                    getContentManager().showRunContent(executor, descriptor, environment.getContentToReuse());
                    final ProcessHandler processHandler = descriptor.getProcessHandler();
                    if (processHandler != null) {
                        if (!processHandler.isStartNotified()) {
                            processHandler.startNotify();
                        }
                        project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStarted(executor.getId(), environment, processHandler);
                        ProcessExecutionListener listener = new ProcessExecutionListener(project, executor.getId(), environment, processHandler, descriptor);
                        processHandler.addProcessListener(listener);
                        boolean terminating = processHandler.isProcessTerminating();
                        boolean terminated = processHandler.isProcessTerminated();
                        if (terminating || terminated) {
                            listener.processWillTerminate(new ProcessEvent(processHandler), false);
                            if (terminated) {
                                int exitCode = processHandler.isStartNotified() ? processHandler.getExitCode() : -1;
                                listener.processTerminated(new ProcessEvent(processHandler, exitCode));
                            }
                        }
                    }
                    environment.setContentToReuse(descriptor);
                } else {
                    project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
                }
            }, o -> project.isDisposed());
        }).rejected(e -> {
            if (!(e instanceof ProcessCanceledException)) {
                ExecutionException error = e instanceof ExecutionException ? (ExecutionException) e : new ExecutionException(e);
                ExecutionUtil.handleExecutionError(project, ExecutionManager.getInstance(project).getContentManager().getToolWindowIdByEnvironment(environment), profile, error);
            }
            LOG.info(e);
            project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
        });
    };
    if (ApplicationManager.getApplication().isUnitTestMode() && !myForceCompilationInTests) {
        startRunnable.run();
    } else {
        compileAndRun(() -> TransactionGuard.submitTransaction(project, startRunnable), environment, state, () -> {
            if (!project.isDisposed()) {
                project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
            }
        });
    }
}
Also used : Trinity(com.intellij.openapi.util.Trinity) ModalityState(com.intellij.openapi.application.ModalityState) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) RunProfileState(com.intellij.execution.configurations.RunProfileState) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) SmartList(com.intellij.util.SmartList) SaveAndSyncHandler(com.intellij.ide.SaveAndSyncHandler) Disposer(com.intellij.openapi.util.Disposer) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) RunnerLayoutUi(com.intellij.execution.ui.RunnerLayoutUi) DumbService(com.intellij.openapi.project.DumbService) AppUIUtil(com.intellij.ui.AppUIUtil) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) Nullable(org.jetbrains.annotations.Nullable) ServiceManager(com.intellij.openapi.components.ServiceManager) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ProcessEvent(com.intellij.execution.process.ProcessEvent) ExecutionUtil(com.intellij.execution.runners.ExecutionUtil) Registry(com.intellij.openapi.util.registry.Registry) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) DataContext(com.intellij.openapi.actionSystem.DataContext) RunContentManager(com.intellij.execution.ui.RunContentManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RunContentManagerImpl(com.intellij.execution.ui.RunContentManagerImpl) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) CommonBundle(com.intellij.CommonBundle) Project(com.intellij.openapi.project.Project) ProgramRunner(com.intellij.execution.runners.ProgramRunner) StringUtil(com.intellij.openapi.util.text.StringUtil) Key(com.intellij.openapi.util.Key) RunProfile(com.intellij.execution.configurations.RunProfile) Disposable(com.intellij.openapi.Disposable) ProcessHandler(com.intellij.execution.process.ProcessHandler) com.intellij.execution(com.intellij.execution) TestOnly(org.jetbrains.annotations.TestOnly) DockManager(com.intellij.ui.docking.DockManager) CompatibilityAwareRunProfile(com.intellij.execution.configuration.CompatibilityAwareRunProfile) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) TransactionGuard(com.intellij.openapi.application.TransactionGuard) Condition(com.intellij.openapi.util.Condition) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Project(com.intellij.openapi.project.Project) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) Trinity(com.intellij.openapi.util.Trinity) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessHandler(com.intellij.execution.process.ProcessHandler) RunProfile(com.intellij.execution.configurations.RunProfile) CompatibilityAwareRunProfile(com.intellij.execution.configuration.CompatibilityAwareRunProfile) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 14 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class RunConfigurationBeforeRunProvider method doExecuteTask.

public static boolean doExecuteTask(@NotNull final ExecutionEnvironment env, @NotNull final RunnerAndConfigurationSettings settings) {
    final Executor executor = DefaultRunExecutor.getRunExecutorInstance();
    final String executorId = executor.getId();
    ExecutionEnvironmentBuilder builder = ExecutionEnvironmentBuilder.createOrNull(executor, settings);
    if (builder == null) {
        return false;
    }
    ExecutionTarget compatibleTarget = getCompatibleTarget(env, settings);
    if (compatibleTarget == null) {
        return false;
    }
    final ExecutionEnvironment environment = builder.target(compatibleTarget).build();
    environment.setExecutionId(env.getExecutionId());
    if (!environment.getRunner().canRun(executorId, environment.getRunProfile())) {
        return false;
    } else {
        beforeRun(environment);
        return doRunTask(executorId, environment, environment.getRunner());
    }
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder)

Example 15 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class DebuggerTestCase method createLocalSession.

protected DebuggerSession createLocalSession(final JavaParameters javaParameters) throws ExecutionException, InterruptedException {
    createBreakpoints(javaParameters.getMainClass());
    DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT;
    GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings();
    debuggerRunnerSettings.LOCAL = true;
    final RemoteConnection debugParameters = DebuggerManagerImpl.createDebugParameters(javaParameters, debuggerRunnerSettings, false);
    ExecutionEnvironment environment = new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runnerSettings(debuggerRunnerSettings).runProfile(new MockConfiguration()).build();
    final JavaCommandLineState javaCommandLineState = new JavaCommandLineState(environment) {

        @Override
        protected JavaParameters createJavaParameters() {
            return javaParameters;
        }

        @Override
        protected GeneralCommandLine createCommandLine() throws ExecutionException {
            return getJavaParameters().toCommandLine();
        }
    };
    ApplicationManager.getApplication().invokeAndWait(() -> {
        try {
            myDebuggerSession = DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(new DefaultDebugEnvironment(new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runProfile(new MockConfiguration()).build(), javaCommandLineState, debugParameters, false));
            XDebuggerManager.getInstance(myProject).startSession(javaCommandLineState.getEnvironment(), new XDebugProcessStarter() {

                @Override
                @NotNull
                public XDebugProcess start(@NotNull XDebugSession session) {
                    return JavaDebugProcess.create(session, myDebuggerSession);
                }
            });
        } catch (ExecutionException e) {
            LOG.error(e);
        }
    });
    myDebugProcess = myDebuggerSession.getProcess();
    myDebugProcess.addProcessListener(new ProcessAdapter() {

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            print(event.getText(), outputType);
        }
    });
    assertNotNull(myDebuggerSession);
    assertNotNull(myDebugProcess);
    return myDebuggerSession;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) NotNull(org.jetbrains.annotations.NotNull) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Aggregations

ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)49 ProcessHandler (com.intellij.execution.process.ProcessHandler)13 ProcessEvent (com.intellij.execution.process.ProcessEvent)12 NotNull (org.jetbrains.annotations.NotNull)12 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)11 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)11 Project (com.intellij.openapi.project.Project)11 Nullable (org.jetbrains.annotations.Nullable)11 ProgramRunner (com.intellij.execution.runners.ProgramRunner)10 ExecutionException (com.intellij.execution.ExecutionException)9 Executor (com.intellij.execution.Executor)8 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)8 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)8 Key (com.intellij.openapi.util.Key)8 RunProfile (com.intellij.execution.configurations.RunProfile)7 Ref (com.intellij.openapi.util.Ref)7 DefaultDebugExecutor (com.intellij.execution.executors.DefaultDebugExecutor)6 Module (com.intellij.openapi.module.Module)6 IOException (java.io.IOException)5 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)4