Search in sources :

Example 1 with CapturingProcessAdapter

use of com.intellij.execution.process.CapturingProcessAdapter in project ballerina by ballerina-lang.

the class BallerinaExecutor method execute.

public boolean execute() {
    Logger.getInstance(getClass()).assertTrue(!ApplicationManager.getApplication().isDispatchThread(), "It's bad idea to run external tool on EDT");
    Logger.getInstance(getClass()).assertTrue(myProcessHandler == null, "Process has already run with this executor instance");
    Ref<Boolean> result = Ref.create(false);
    GeneralCommandLine commandLine = null;
    try {
        commandLine = createCommandLine();
        GeneralCommandLine finalCommandLine = commandLine;
        myProcessHandler = new KillableColoredProcessHandler(finalCommandLine, true) {

            @Override
            public void startNotify() {
                if (myShowBallerinaEnvVariables) {
                    BallerinaRunUtil.printBallerinaEnvVariables(finalCommandLine, this);
                }
                super.startNotify();
            }
        };
        BallerinaHistoryProcessListener historyProcessListener = new BallerinaHistoryProcessListener();
        myProcessHandler.addProcessListener(historyProcessListener);
        for (ProcessListener listener : myProcessListeners) {
            myProcessHandler.addProcessListener(listener);
        }
        CapturingProcessAdapter processAdapter = new CapturingProcessAdapter(myProcessOutput) {

            @Override
            public void processTerminated(@NotNull ProcessEvent event) {
                super.processTerminated(event);
                boolean success = event.getExitCode() == 0 && myProcessOutput.getStderr().isEmpty();
                boolean nothingToShow = myProcessOutput.getStdout().isEmpty() && myProcessOutput.getStderr().isEmpty();
                boolean cancelledByUser = (event.getExitCode() == -1 || event.getExitCode() == 2) && nothingToShow;
                result.set(success);
                if (success) {
                    if (myShowNotificationsOnSuccess) {
                        showNotification("Finished successfully", NotificationType.INFORMATION);
                    }
                } else if (cancelledByUser) {
                    if (myShowNotificationsOnError) {
                        showNotification("Interrupted", NotificationType.WARNING);
                    }
                } else if (myShowOutputOnError) {
                    ApplicationManager.getApplication().invokeLater(() -> showOutput(myProcessHandler, historyProcessListener));
                }
            }
        };
        myProcessHandler.addProcessListener(processAdapter);
        myProcessHandler.startNotify();
        ExecutionModes.SameThreadMode sameThreadMode = new ExecutionModes.SameThreadMode(getPresentableName());
        ExecutionHelper.executeExternalProcess(myProject, myProcessHandler, sameThreadMode, commandLine);
        LOGGER.debug("Finished `" + getPresentableName() + "` with result: " + result.get());
        return result.get();
    } catch (ExecutionException e) {
        if (myShowOutputOnError) {
            ExecutionHelper.showErrors(myProject, Collections.singletonList(e), getPresentableName(), null);
        }
        if (myShowNotificationsOnError) {
            showNotification(StringUtil.notNullize(e.getMessage(), "Unknown error, see logs for details"), NotificationType.ERROR);
        }
        String commandLineInfo = commandLine != null ? commandLine.getCommandLineString() : "not constructed";
        LOGGER.debug("Finished `" + getPresentableName() + "` with an exception. Commandline: " + commandLineInfo, e);
        return false;
    }
}
Also used : CapturingProcessAdapter(com.intellij.execution.process.CapturingProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) NotNull(org.jetbrains.annotations.NotNull) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionModes(com.intellij.execution.ExecutionModes) ExecutionException(com.intellij.execution.ExecutionException) KillableColoredProcessHandler(com.intellij.execution.process.KillableColoredProcessHandler)

Aggregations

ExecutionException (com.intellij.execution.ExecutionException)1 ExecutionModes (com.intellij.execution.ExecutionModes)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 CapturingProcessAdapter (com.intellij.execution.process.CapturingProcessAdapter)1 KillableColoredProcessHandler (com.intellij.execution.process.KillableColoredProcessHandler)1 ProcessEvent (com.intellij.execution.process.ProcessEvent)1 ProcessListener (com.intellij.execution.process.ProcessListener)1 NotNull (org.jetbrains.annotations.NotNull)1