use of com.intellij.execution.process.BaseProcessHandler in project Perl5-IDEA by Camelcade.
the class PerlFormatWithPerlTidyAction method actionPerformed.
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
if (isEnabled(event)) {
final PsiFile file = PerlActionUtil.getPsiFileFromEvent(event);
if (file == null) {
return;
}
final Project project = file.getProject();
final Document document = file.getViewProvider().getDocument();
if (document == null) {
return;
}
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return;
}
FileDocumentManager.getInstance().saveDocument(document);
new Task.Backgroundable(project, PerlBundle.message("perl.tidy.formatting"), false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
try {
PerlCommandLine perlTidyCommandLine = getPerlTidyCommandLine(project);
if (perlTidyCommandLine == null) {
return;
}
BaseProcessHandler<?> processHandler = PerlHostData.createProcessHandler(perlTidyCommandLine.withCharset(virtualFile.getCharset()));
final OutputStream outputStream = Objects.requireNonNull(processHandler.getProcessInput());
try {
final byte[] sourceBytes = virtualFile.contentsToByteArray();
outputStream.write(sourceBytes);
outputStream.close();
} catch (IOException e) {
LOG.error(e);
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), e.getMessage(), NotificationType.ERROR));
return;
}
ProcessOutput processOutput = PerlHostData.getOutput(processHandler);
final List<String> stdoutLines = processOutput.getStdoutLines(false);
List<String> stderrLines = processOutput.getStderrLines();
if (stderrLines.isEmpty()) {
WriteCommandAction.runWriteCommandAction(project, () -> {
document.setText(StringUtil.join(stdoutLines, "\n"));
PsiDocumentManager.getInstance(project).commitDocument(document);
});
} else {
LOG.warn("Non-empty stderr: " + processOutput.getStderr());
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), StringUtil.join(stderrLines, "<br>"), NotificationType.ERROR));
}
} catch (ExecutionException e) {
LOG.error(e);
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.running.error.title"), e.getMessage(), NotificationType.ERROR));
}
}
}.queue();
}
}
use of com.intellij.execution.process.BaseProcessHandler in project consulo by consulo.
the class EnvironmentAwareHost method getProcessOutput.
/**
* @param commandLine commandLine to execute on this host
* @return output of the corresponding process
*/
@Nonnull
public ProcessOutput getProcessOutput(@Nonnull GeneralCommandLine commandLine) throws ExecutionException {
BaseProcessHandler handler = getProcessHandler(commandLine);
CapturingProcessRunner runner = new CapturingProcessRunner(handler);
return runner.runProcess();
}
Aggregations