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;
}
}
Aggregations