Search in sources :

Example 21 with PerlCommandLine

use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.

the class PerlXSubsState method reparseXSubs.

public void reparseXSubs() {
    if (!PerlProjectManager.isPerlEnabled(myProject)) {
        return;
    }
    if (myParserTask != null) {
        Messages.showErrorDialog(myProject, PerlBundle.message("perl.deparsing.in.progress.message"), PerlBundle.message("perl.deparsing.in.progress.title"));
        return;
    }
    PerlCommandLine commandLine = PerlRunUtil.getPerlCommandLine(myProject, PerlPluginUtil.getHelperPath("xs_parser_simple.pl"));
    if (commandLine == null) {
        LOG.warn("Unable to create deparser command line");
        return;
    }
    commandLine.withCharset(StandardCharsets.UTF_8).withMissingPackageListener(false);
    LOG.info("Deparsing: " + commandLine.getCommandLineString());
    myParserTask = new Task.Backgroundable(myProject, PerlBundle.message("perl.deparsing.xsubs"), false) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            var project = PerlXSubsState.this.myProject;
            Map<String, Long> newFilesMap = ReadAction.compute(() -> {
                if (project.isDisposed()) {
                    return null;
                }
                final Map<String, Long> result = new THashMap<>();
                for (VirtualFile virtualFile : getAllXSFiles(project)) {
                    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;
            try {
                processOutput = PerlHostData.execAndGetOutput(commandLine);
            } catch (ExecutionException e) {
                LOG.warn("Error deparsing", e);
                showNotification(PerlBundle.message("perl.deparsing.error.execution"), e.getMessage(), NotificationType.ERROR);
                myParserTask = null;
                return;
            }
            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 (project.isDisposed()) {
                        return;
                    }
                    try {
                        VirtualFile newFile = project.getBaseDir().findOrCreateChildData(this, DEPARSED_FILE_NAME);
                        newFile.setWritable(true);
                        OutputStream outputStream = newFile.getOutputStream(null);
                        outputStream.write(stdout.getBytes());
                        outputStream.close();
                        newFile.setWritable(false);
                        FileContentUtilCore.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();
}
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) ProcessOutput(com.intellij.execution.process.ProcessOutput) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) ExecutionException(com.intellij.execution.ExecutionException) THashMap(gnu.trove.THashMap) Map(java.util.Map) Application(com.intellij.openapi.application.Application)

Aggregations

PerlCommandLine (com.perl5.lang.perl.idea.execution.PerlCommandLine)19 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 ExecutionException (com.intellij.execution.ExecutionException)11 Project (com.intellij.openapi.project.Project)8 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 ProcessOutput (com.intellij.execution.process.ProcessOutput)5 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)3 ProcessEvent (com.intellij.execution.process.ProcessEvent)3 Notification (com.intellij.notification.Notification)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 PerlSharedSettings (com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings)3 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)2 Document (com.intellij.openapi.editor.Document)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 PsiFile (com.intellij.psi.PsiFile)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2