Search in sources :

Example 66 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project Perl5-IDEA by Camelcade.

the class PerlXSubsState method reparseXSubs.

public void reparseXSubs() {
    if (myProject.isDisposed()) {
        return;
    }
    if (myParserTask != null) {
        Messages.showErrorDialog(myProject, PerlBundle.message("perl.deparsing.in.progress.message"), PerlBundle.message("perl.deparsing.in.progress.title"));
        return;
    }
    GeneralCommandLine commandLine = PerlPluginUtil.getPluginScriptCommandLine(myProject, "xs_parser_simple.pl");
    if (commandLine == null) {
        return;
    }
    try {
        LOG.info("Deparsing: " + commandLine.getCommandLineString());
        final CapturingProcessHandler processHandler = new CapturingProcessHandler(commandLine.createProcess(), CharsetToolkit.UTF8_CHARSET, commandLine.getCommandLineString());
        myParserTask = new Task.Backgroundable(myProject, PerlBundle.message("perl.deparsing.xsubs"), false) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                Map<String, Long> newFilesMap = ReadAction.compute(() -> {
                    if (myProject.isDisposed()) {
                        return null;
                    }
                    final Map<String, Long> result = new THashMap<>();
                    for (VirtualFile virtualFile : getAllXSFiles(myProject)) {
                        if (virtualFile.isValid()) {
                            String filePath = virtualFile.getCanonicalPath();
                            if (filePath != null) {
                                result.put(filePath, VfsUtilCore.virtualToIoFile(virtualFile).lastModified());
                            }
                        }
                    }
                    return result;
                });
                if (newFilesMap == null) {
                    myParserTask = null;
                    return;
                }
                ProcessOutput processOutput = processHandler.runProcess();
                final String stdout = processOutput.getStdout();
                String stderr = processOutput.getStderr();
                int exitCode = processOutput.getExitCode();
                LOG.info("Deparsing finished with exit code: " + exitCode + (StringUtil.isEmpty(stderr) ? "" : ". STDERR:\n" + stderr));
                if (exitCode != 0) {
                    showNotification(PerlBundle.message("perl.deparsing.error.execution"), stderr, NotificationType.ERROR);
                } else if (!stdout.isEmpty()) {
                    Application application = ApplicationManager.getApplication();
                    application.invokeAndWait(() -> WriteAction.run(() -> {
                        if (myProject.isDisposed()) {
                            return;
                        }
                        try {
                            VirtualFile newFile = myProject.getBaseDir().findOrCreateChildData(this, DEPARSED_FILE_NAME);
                            newFile.setWritable(true);
                            OutputStream outputStream = newFile.getOutputStream(null);
                            outputStream.write(stdout.getBytes());
                            outputStream.close();
                            newFile.setWritable(false);
                            FileContentUtil.reparseFiles(newFile);
                            myFilesMap = newFilesMap;
                            isActual = true;
                            showNotification(PerlBundle.message("perl.deparsing.finished"), "", NotificationType.INFORMATION);
                        } catch (IOException e) {
                            LOG.warn("Error creating deparsed file", e);
                            showNotification(PerlBundle.message("perl.deparsing.error.creating.file"), e.getMessage(), NotificationType.ERROR);
                        }
                    // fixme fix modality state
                    }));
                }
                myParserTask = null;
            }
        };
        myParserTask.queue();
    } catch (ExecutionException e) {
        LOG.warn("Error deparsing", e);
        showNotification(PerlBundle.message("perl.deparsing.error.execution"), e.getMessage(), NotificationType.ERROR);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) ReadTask(com.intellij.openapi.progress.util.ReadTask) Task(com.intellij.openapi.progress.Task) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) ExecutionException(com.intellij.execution.ExecutionException) THashMap(gnu.trove.THashMap) Map(java.util.Map) Application(com.intellij.openapi.application.Application) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Aggregations

ProcessOutput (com.intellij.execution.process.ProcessOutput)66 ExecutionException (com.intellij.execution.ExecutionException)25 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 File (java.io.File)23 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)16 Nullable (org.jetbrains.annotations.Nullable)13 NotNull (org.jetbrains.annotations.NotNull)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 Project (com.intellij.openapi.project.Project)5 PsiFile (com.intellij.psi.PsiFile)5 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)4 CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)3 Notification (com.intellij.notification.Notification)3 Document (com.intellij.openapi.editor.Document)3 Task (com.intellij.openapi.progress.Task)3 Sdk (com.intellij.openapi.projectRoots.Sdk)3