Search in sources :

Example 41 with OSProcessHandler

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

the class MavenRunConfiguration method getState.

@Override
public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException {
    JavaCommandLineState state = new JavaCommandLineState(env) {

        @Override
        protected JavaParameters createJavaParameters() throws ExecutionException {
            return MavenRunConfiguration.this.createJavaParameters(env.getProject());
        }

        @NotNull
        @Override
        public ExecutionResult execute(@NotNull Executor executor, @NotNull ProgramRunner runner) throws ExecutionException {
            DefaultExecutionResult res = (DefaultExecutionResult) super.execute(executor, runner);
            if (executor.getId().equals(ToolWindowId.RUN) && MavenResumeAction.isApplicable(env.getProject(), getJavaParameters(), MavenRunConfiguration.this)) {
                MavenResumeAction resumeAction = new MavenResumeAction(res.getProcessHandler(), runner, env);
                res.setRestartActions(resumeAction);
            }
            return res;
        }

        @NotNull
        @Override
        protected OSProcessHandler startProcess() throws ExecutionException {
            OSProcessHandler result = super.startProcess();
            result.setShouldDestroyProcessRecursively(true);
            result.addProcessListener(new ProcessAdapter() {

                @Override
                public void processTerminated(ProcessEvent event) {
                    updateProjectsFolders();
                }
            });
            return result;
        }
    };
    state.setConsoleBuilder(MavenConsoleImpl.createConsoleBuilder(getProject()));
    return state;
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with OSProcessHandler

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

the class JavaScratchConfiguration method getState.

@Override
public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) throws ExecutionException {
    final JavaCommandLineState state = new JavaApplicationCommandLineState<JavaScratchConfiguration>(this, env) {

        @Override
        protected void setupJavaParameters(JavaParameters params) throws ExecutionException {
            super.setupJavaParameters(params);
            final File scrachesOutput = JavaScratchCompilationSupport.getScratchOutputDirectory(getProject());
            if (scrachesOutput != null) {
                params.getClassPath().addFirst(FileUtil.toCanonicalPath(scrachesOutput.getAbsolutePath()).replace('/', File.separatorChar));
            }
        }

        @NotNull
        @Override
        protected OSProcessHandler startProcess() throws ExecutionException {
            final OSProcessHandler handler = super.startProcess();
            if (getRunnerSettings() instanceof DebuggingRunnerData) {
                final VirtualFile vFile = getConfiguration().getScratchVirtualFile();
                if (vFile != null) {
                    DebuggerManager.getInstance(getProject()).addDebugProcessListener(handler, new DebugProcessListener() {

                        @Override
                        public void processAttached(DebugProcess process) {
                            if (vFile.isValid()) {
                                process.appendPositionManager(new JavaScratchPositionManager((DebugProcessImpl) process, vFile));
                            }
                            process.removeDebugProcessListener(this);
                        }
                    });
                }
            }
            return handler;
        }
    };
    state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject(), getConfigurationModule().getSearchScope()));
    return state;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DebugProcess(com.intellij.debugger.engine.DebugProcess) DebugProcessListener(com.intellij.debugger.engine.DebugProcessListener) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 43 with OSProcessHandler

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

the class JavaCommandLineStateUtil method startProcess.

@NotNull
public static OSProcessHandler startProcess(@NotNull GeneralCommandLine commandLine, boolean ansiColoring) throws ExecutionException {
    ProcessHandlerFactory factory = ProcessHandlerFactory.getInstance();
    OSProcessHandler processHandler = ansiColoring ? factory.createColoredProcessHandler(commandLine) : factory.createProcessHandler(commandLine);
    ProcessTerminatedListener.attach(processHandler);
    return processHandler;
}
Also used : ProcessHandlerFactory(com.intellij.execution.process.ProcessHandlerFactory) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project kotlin by JetBrains.

the class RunUtils method run.

private static RunResult run(final RunSettings settings) {
    System.out.println("RUN COMMAND: " + settings);
    final StringBuilder stdOut = new StringBuilder();
    final StringBuilder stdErr = new StringBuilder();
    OSProcessHandler handler;
    try {
        handler = new OSProcessHandler(settings.commandLine.createProcess(), settings.commandLine.getCommandLineString(), Charsets.UTF_8);
        if (settings.input != null) {
            handler.getProcessInput().write(settings.input.getBytes());
        }
        close(handler.getProcessInput());
    } catch (ExecutionException e) {
        return new RunResult(false, getStackTrace(e));
    } catch (IOException e) {
        return new RunResult(false, getStackTrace(e));
    }
    handler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            System.out.println("TERMINATED: " + settings.commandLine);
            super.processTerminated(event);
        }

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            String str = event.getText();
            if (outputType == ProcessOutputTypes.STDOUT || outputType == ProcessOutputTypes.SYSTEM) {
                appendToContent(stdOut, str);
            } else if (outputType == ProcessOutputTypes.STDERR) {
                appendToContent(stdErr, str);
            }
        }

        private synchronized void appendToContent(StringBuilder content, String line) {
            if (settings.printOutputAtAppearance) {
                System.out.println(getPrefixString() + StringUtil.trimTrailing(line));
                System.out.flush();
            } else {
                content.append(getPrefixString());
                content.append(StringUtil.trimTrailing(line));
                content.append("\n");
            }
        }

        private String getPrefixString() {
            return (settings.outputPrefix != null) ? settings.outputPrefix + " " : "";
        }
    });
    handler.startNotify();
    if (settings.waitForEnd) {
        String timeoutAsString = System.getenv("kotlin.tests.android.timeout");
        if (timeoutAsString == null) {
            timeoutAsString = "30";
            System.err.println("Default value for timeout used: timeout = 30 min. You can change it using 'kotlin.tests.android.timeout' environment variable");
        }
        int timeout;
        try {
            timeout = Integer.parseInt(timeoutAsString);
        } catch (NumberFormatException e) {
            timeout = 30;
            System.err.println("Timeout system property should be a number");
        }
        handler.waitFor(timeout * 60 * 1000);
        if (!handler.isProcessTerminated()) {
            System.out.println("Output before handler.isProcessTerminated() " + settings.commandLine);
            System.out.println(stdOut);
            System.err.println(stdErr);
            return new RunResult(false, "Timeout exception: execution was terminated after ~20 min.");
        }
    } else {
        handler.waitFor();
    }
    int exitCode = handler.getProcess().exitValue();
    if (exitCode != 0) {
        return new RunResult(false, builderToString(stdOut) + builderToString(stdErr));
    } else {
        String output = builderToString(stdOut) + builderToString(stdErr);
        if (OutputUtils.isBuildFailed(output)) {
            return new RunResult(false, output);
        }
        if (!settings.commandLine.getCommandLineString().contains("install")) {
            System.out.print(output);
        }
        return new RunResult(true, output);
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 45 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project kotlin by JetBrains.

the class KotlinIntegrationTestBase method runProcess.

private static int runProcess(GeneralCommandLine commandLine, StringBuilder executionLog) throws ExecutionException {
    OSProcessHandler handler = new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(), commandLine.getCharset());
    StringBuilder outContent = new StringBuilder();
    StringBuilder errContent = new StringBuilder();
    handler.addProcessListener(new OutputListener(outContent, errContent));
    handler.startNotify();
    handler.waitFor();
    int exitCode = handler.getProcess().exitValue();
    appendIfNotEmpty(executionLog, "OUT:\n", outContent.toString());
    appendIfNotEmpty(executionLog, "\nERR:\n", errContent.toString());
    executionLog.append("\nReturn code: ").append(exitCode).append("\n");
    return exitCode;
}
Also used : OSProcessHandler(com.intellij.execution.process.OSProcessHandler)

Aggregations

OSProcessHandler (com.intellij.execution.process.OSProcessHandler)55 ExecutionException (com.intellij.execution.ExecutionException)25 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)24 ProcessEvent (com.intellij.execution.process.ProcessEvent)24 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)23 NotNull (org.jetbrains.annotations.NotNull)18 Key (com.intellij.openapi.util.Key)14 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 File (java.io.File)6 Module (com.intellij.openapi.module.Module)5 Sdk (com.intellij.openapi.projectRoots.Sdk)5 IOException (java.io.IOException)5 ProgramRunner (com.intellij.execution.runners.ProgramRunner)4 Nullable (org.jetbrains.annotations.Nullable)4 Executor (com.intellij.execution.Executor)3 RunContentExecutor (com.intellij.execution.RunContentExecutor)3 KillableColoredProcessHandler (com.intellij.execution.process.KillableColoredProcessHandler)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3