Search in sources :

Example 1 with ProcessListener

use of com.intellij.execution.process.ProcessListener in project intellij-community by JetBrains.

the class ConfigurationBasedProcessRunner method runProcess.

@Override
final void runProcess(@NotNull final String sdkPath, @NotNull final Project project, @NotNull final ProcessListener processListener, @NotNull final String tempWorkingPath) throws ExecutionException {
    ensureConsoleOk(myConsole);
    // Do not create new environment from factory, if child provided environment to rerun
    final ExecutionEnvironment executionEnvironment = // TODO: RENAME
    (myRerunExecutionEnvironment != null ? myRerunExecutionEnvironment : createExecutionEnvironment(sdkPath, project, tempWorkingPath));
    // Engine to be run after process end to post process console
    final ProcessListener consolePostprocessor = new ProcessAdapter() {

        @Override
        public void processTerminated(final ProcessEvent event) {
            super.processTerminated(event);
            ApplicationManager.getApplication().invokeAndWait(() -> prepareConsoleAfterProcessEnd(), ModalityState.NON_MODAL);
        }
    };
    /// Find all available runners to report them to the test
    myAvailableRunnersForLastRun.clear();
    for (final ProgramRunner<?> runner : ProgramRunner.PROGRAM_RUNNER_EP.getExtensions()) {
        for (final Executor executor : Executor.EXECUTOR_EXTENSION_NAME.getExtensions()) {
            if (runner.canRun(executor.getId(), executionEnvironment.getRunProfile())) {
                myAvailableRunnersForLastRun.add(runner);
            }
        }
    }
    executionEnvironment.getRunner().execute(executionEnvironment, new ProgramRunner.Callback() {

        @Override
        public void processStarted(final RunContentDescriptor descriptor) {
            final ProcessHandler handler = descriptor.getProcessHandler();
            assert handler != null : "No process handler";
            handler.addProcessListener(consolePostprocessor);
            handler.addProcessListener(processListener);
            myConsole = null;
            fetchConsoleAndSetToField(descriptor);
            assert myConsole != null : "fetchConsoleAndSetToField did not set console!";
            // Console does not work with out of this method
            final JComponent component = myConsole.getComponent();
            assert component != null;
            myLastProcessDescriptor = descriptor;
        }
    });
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) ProcessHandler(com.intellij.execution.process.ProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner)

Example 2 with ProcessListener

use of com.intellij.execution.process.ProcessListener in project intellij-community by JetBrains.

the class StudyRunAction method executeFile.

private void executeFile(@NotNull final Project project, @NotNull final VirtualFile openedFile, @NotNull final String filePath) {
    GeneralCommandLine cmd = new GeneralCommandLine();
    cmd.withWorkDirectory(openedFile.getParent().getCanonicalPath());
    TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
    assert selectedTaskFile != null;
    final Task currentTask = selectedTaskFile.getTask();
    final Sdk sdk = StudyUtils.findSdk(currentTask, project);
    if (sdk == null) {
        StudyUtils.showNoSdkNotification(currentTask, project);
        return;
    }
    String sdkHomePath = sdk.getHomePath();
    if (sdkHomePath != null) {
        cmd.setExePath(sdkHomePath);
        StudyUtils.setCommandLineParameters(cmd, project, filePath, sdkHomePath, currentTask);
        try {
            myHandler = new OSProcessHandler(cmd);
        } catch (ExecutionException e) {
            LOG.error(e);
            return;
        }
        for (ProcessListener processListener : myProcessListeners) {
            myHandler.addProcessListener(processListener);
        }
        final RunContentExecutor executor = StudyUtils.getExecutor(project, currentTask, myHandler);
        if (executor != null) {
            Disposer.register(project, executor);
            executor.run();
        }
        EduUtils.synchronize();
    }
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Task(com.jetbrains.edu.learning.courseFormat.Task) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) ProcessListener(com.intellij.execution.process.ProcessListener) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunContentExecutor(com.intellij.execution.RunContentExecutor) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException)

Example 3 with ProcessListener

use of com.intellij.execution.process.ProcessListener in project intellij-community by JetBrains.

the class RemoteProcessSupport method getProcessListener.

private ProcessListener getProcessListener(@NotNull final Pair<Target, Parameters> key) {
    return new ProcessListener() {

        @Override
        public void startNotified(ProcessEvent event) {
            ProcessHandler processHandler = event.getProcessHandler();
            processHandler.putUserData(ProcessHandler.SILENTLY_DESTROY_ON_CLOSE, Boolean.TRUE);
            Info o;
            synchronized (myProcMap) {
                o = myProcMap.get(key);
                if (o instanceof PendingInfo) {
                    myProcMap.put(key, new PendingInfo(((PendingInfo) o).ref, processHandler));
                }
            }
        }

        @Override
        public void processTerminated(ProcessEvent event) {
            if (dropProcessInfo(key, null, event.getProcessHandler())) {
                fireModificationCountChanged();
            }
        }

        @Override
        public void processWillTerminate(ProcessEvent event, boolean willBeDestroyed) {
            if (dropProcessInfo(key, null, event.getProcessHandler())) {
                fireModificationCountChanged();
            }
        }

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            String text = StringUtil.notNullize(event.getText());
            if (outputType == ProcessOutputTypes.STDERR) {
                LOG.warn(text.trim());
            } else {
                LOG.info(text.trim());
            }
            RunningInfo result = null;
            PendingInfo info;
            synchronized (myProcMap) {
                Info o = myProcMap.get(key);
                logText(key.second, event, outputType, o);
                if (o instanceof PendingInfo) {
                    info = (PendingInfo) o;
                    if (outputType == ProcessOutputTypes.STDOUT) {
                        String prefix = "Port/ID:";
                        if (text.startsWith(prefix)) {
                            String pair = text.substring(prefix.length()).trim();
                            int idx = pair.indexOf("/");
                            result = new RunningInfo(info.handler, Integer.parseInt(pair.substring(0, idx)), pair.substring(idx + 1));
                            myProcMap.put(key, result);
                            myProcMap.notifyAll();
                        }
                    } else if (outputType == ProcessOutputTypes.STDERR) {
                        info.stderr.append(text);
                    }
                } else {
                    info = null;
                }
            }
            if (result != null) {
                synchronized (info.ref) {
                    info.ref.set(result);
                    info.ref.notifyAll();
                }
                fireModificationCountChanged();
                try {
                    RemoteDeadHand.TwoMinutesTurkish.startCooking("localhost", result.port);
                } catch (Throwable e) {
                    LOG.warn("The cook failed to start due to " + ExceptionUtil.getRootCause(e));
                }
            }
        }
    };
}
Also used : ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) ProcessHandler(com.intellij.execution.process.ProcessHandler) Key(com.intellij.openapi.util.Key)

Example 4 with ProcessListener

use of com.intellij.execution.process.ProcessListener in project intellij-community by JetBrains.

the class AbstractAutoTestManager method clearRestarterListener.

private static void clearRestarterListener(@NotNull ProcessHandler processHandler) {
    ProcessListener restarterListener = ON_TERMINATION_RESTARTER_KEY.get(processHandler, null);
    if (restarterListener != null) {
        processHandler.removeProcessListener(restarterListener);
        ON_TERMINATION_RESTARTER_KEY.set(processHandler, null);
    }
}
Also used : ProcessListener(com.intellij.execution.process.ProcessListener)

Example 5 with ProcessListener

use of com.intellij.execution.process.ProcessListener in project intellij-community by JetBrains.

the class AbstractAutoTestManager method scheduleRestartOnTermination.

private void scheduleRestartOnTermination(@NotNull final RunContentDescriptor descriptor, @NotNull final ProcessHandler processHandler, final int modificationStamp, @NotNull final AutoTestWatcher watcher) {
    ProcessListener restarterListener = ON_TERMINATION_RESTARTER_KEY.get(processHandler);
    if (restarterListener != null) {
        clearRestarterListener(processHandler);
    }
    restarterListener = new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            clearRestarterListener(processHandler);
            ApplicationManager.getApplication().invokeLater(() -> {
                if (isAutoTestEnabledForDescriptor(descriptor) && watcher.isUpToDate(modificationStamp)) {
                    restart(descriptor);
                }
            }, ModalityState.any());
        }
    };
    ON_TERMINATION_RESTARTER_KEY.set(processHandler, restarterListener);
    processHandler.addProcessListener(restarterListener);
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener)

Aggregations

ProcessListener (com.intellij.execution.process.ProcessListener)6 ProcessEvent (com.intellij.execution.process.ProcessEvent)4 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)2 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)2 ProcessHandler (com.intellij.execution.process.ProcessHandler)2 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)2 ProgramRunner (com.intellij.execution.runners.ProgramRunner)2 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 Key (com.intellij.openapi.util.Key)2 CodeInsightTestCase (com.intellij.codeInsight.CodeInsightTestCase)1 ExecutionException (com.intellij.execution.ExecutionException)1 Executor (com.intellij.execution.Executor)1 RunContentExecutor (com.intellij.execution.RunContentExecutor)1 RunManager (com.intellij.execution.RunManager)1 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 DefaultDebugExecutor (com.intellij.execution.executors.DefaultDebugExecutor)1 HyperlinkInfo (com.intellij.execution.filters.HyperlinkInfo)1 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)1