Search in sources :

Example 6 with PerlCommandLine

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

the class MojoGenerateAction method actionPerformed.

@Override
public final void actionPerformed(@NotNull AnActionEvent e) {
    var project = getEventProject(e);
    var perlSdk = PerlProjectManager.getSdk(e);
    VirtualFile mojoScript = MojoUtil.getMojoScript(e);
    if (mojoScript == null) {
        LOG.warn("No mojo script; project: " + project + " sdk: " + perlSdk);
        return;
    }
    List<String> generationParameters = computeGenerationParameters(e, mojoScript);
    if (generationParameters == null) {
        return;
    }
    var localScriptPath = mojoScript.getPath();
    if (perlSdk == null) {
        LOG.warn("No perl sdk for " + project);
        return;
    }
    PerlHostData<?, ?> hostData = PerlHostData.notNullFrom(perlSdk);
    var remoteScriptPath = hostData.getRemotePath(localScriptPath);
    if (remoteScriptPath == null) {
        LOG.warn("No remote path for " + localScriptPath + " in " + perlSdk);
        return;
    }
    List<String> fullArguments = ContainerUtil.newArrayList(remoteScriptPath, GENERATE_COMMAND);
    fullArguments.addAll(generationParameters);
    VirtualFile targetDirectory = getTargetDirectory(e);
    var targetDirectoryPath = targetDirectory.getPath();
    var callbackTestSemaphore = myCallbackTestSemaphore;
    PerlRunUtil.runInConsole(new PerlCommandLine(fullArguments).withProject(project).withConsoleIcon(MojoIcons.MOJO_LOGO).withWorkDirectory(targetDirectoryPath).withProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(@NotNull ProcessEvent event) {
            targetDirectory.refresh(true, false);
            try {
                hostData.fixPermissionsRecursively(targetDirectoryPath, project);
            } catch (ExecutionException ex) {
                LOG.warn("Error fixing permissions for " + targetDirectoryPath + ": " + ex.getMessage());
            } finally {
                if (callbackTestSemaphore != null) {
                    callbackTestSemaphore.up();
                }
            }
        }
    }));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) ExecutionException(com.intellij.execution.ExecutionException) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PerlCommandLine

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

the class PackageManagerAdapter method doInstall.

/**
 * Installs a package with {@code packageName} with output in console
 */
private void doInstall(@NotNull Collection<String> packageNames) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (myProject != null && myProject.isDisposed()) {
        return;
    }
    VirtualFile script = PerlRunUtil.findLibraryScriptWithNotification(getSdk(), getProject(), getManagerScriptName(), getManagerPackageName());
    if (script == null) {
        return;
    }
    String remotePath = PerlHostData.notNullFrom(mySdk).getRemotePath(script.getPath());
    if (remotePath == null) {
        return;
    }
    PerlRunUtil.runInConsole(new PerlCommandLine(remotePath).withParameters(getInstallParameters(packageNames)).withSdk(getSdk()).withProject(getProject()).withConsoleTitle(PerlBundle.message("perl.cpan.console.installing", StringUtil.join(packageNames, ", "))).withProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(@NotNull ProcessEvent event) {
            PerlRunUtil.refreshSdkDirs(mySdk, myProject);
        }
    }));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PerlCommandLine

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

the class PerlCriticAnnotator method doAnnotate.

@Override
@Nullable
public List<PerlCriticErrorDescriptor> doAnnotate(final PerlFile sourcePsiFile) {
    if (sourcePsiFile == null) {
        return null;
    }
    VirtualFile virtualFile = ReadAction.compute(() -> PsiUtilCore.getVirtualFile(sourcePsiFile));
    if (virtualFile == null) {
        return null;
    }
    byte[] sourceBytes = ReadAction.compute(sourcePsiFile::getPerlContentInBytes);
    if (sourceBytes == null) {
        return null;
    }
    try {
        PerlCommandLine criticCommandLine = getPerlCriticCommandLine(sourcePsiFile);
        if (criticCommandLine == null) {
            PerlSharedSettings.getInstance(sourcePsiFile.getProject()).PERL_CRITIC_ENABLED = false;
            return null;
        }
        BaseProcessHandler<?> processHandler = PerlHostData.createProcessHandler(criticCommandLine.withCharset(virtualFile.getCharset()));
        OutputStream outputStream = Objects.requireNonNull(processHandler.getProcessInput());
        outputStream.write(sourceBytes);
        outputStream.close();
        List<PerlCriticErrorDescriptor> errors = new ArrayList<>();
        PerlCriticErrorDescriptor lastDescriptor = null;
        for (String output : PerlHostData.getOutput(processHandler).getStdoutLines()) {
            PerlCriticErrorDescriptor fromString = PerlCriticErrorDescriptor.getFromString(output);
            if (fromString != null) {
                errors.add(lastDescriptor = fromString);
            } else if (lastDescriptor != null) {
                lastDescriptor.append(" " + output);
            } else if (!StringUtil.equals(output, "source OK")) {
                LOG.warn("Could not parse line: " + output);
            }
        }
        return errors;
    } catch (Exception e) {
        LOG.warn("Error running perlcritic", e);
        Notifications.Bus.notify(new Notification(PerlBundle.message("perl.critic.notification.group"), PerlBundle.message("perl.critic.execution.error.title"), PerlBundle.message("perl.critic.execution.error.message", e.getMessage()), NotificationType.ERROR));
        PerlSharedSettings.getInstance(sourcePsiFile.getProject()).PERL_CRITIC_ENABLED = false;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PerlCommandLine

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

the class PerlCriticAnnotator method getPerlCriticCommandLine.

@Nullable
protected PerlCommandLine getPerlCriticCommandLine(@NotNull PsiFile fileToLint) {
    var project = fileToLint.getProject();
    PerlSharedSettings sharedSettings = PerlSharedSettings.getInstance(project);
    VirtualFile perlCriticScript = ReadAction.compute(() -> PerlRunUtil.findLibraryScriptWithNotification(project, SCRIPT_NAME, PACKAGE_NAME));
    if (perlCriticScript == null) {
        return null;
    }
    PerlCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, perlCriticScript);
    if (commandLine == null) {
        return null;
    }
    var virtualFileToLint = PsiUtilCore.getVirtualFile(fileToLint);
    var contentRoot = PerlFileUtil.getContentRoot(project, virtualFileToLint);
    if (contentRoot != null) {
        commandLine.withWorkDirectory(contentRoot.getPath());
    } else {
        LOG.warn("No content root for perlcritic lint: " + virtualFileToLint + " - " + fileToLint + " in " + project);
    }
    if (StringUtil.isNotEmpty(sharedSettings.PERL_CRITIC_ARGS)) {
        commandLine.addParameters(StringUtil.split(sharedSettings.PERL_CRITIC_ARGS, " "));
    }
    return commandLine;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PerlCommandLine

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

the class PerlDeparseFileAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    PsiFile file = PerlActionUtil.getPsiFileFromEvent(event);
    if (file == null) {
        return;
    }
    final Document document = file.getViewProvider().getDocument();
    if (document == null) {
        return;
    }
    final Project project = file.getProject();
    String deparseArgument = "-MO=Deparse";
    PerlSharedSettings perl5Settings = PerlSharedSettings.getInstance(project);
    if (StringUtil.isNotEmpty(perl5Settings.PERL_DEPARSE_ARGUMENTS)) {
        deparseArgument += "," + perl5Settings.PERL_DEPARSE_ARGUMENTS;
    }
    PerlCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, file.getVirtualFile(), deparseArgument);
    if (commandLine == null) {
        return;
    }
    commandLine.withWorkDirectory(project.getBasePath());
    FileDocumentManager.getInstance().saveDocument(document);
    new Task.Backgroundable(file.getProject(), PerlBundle.message("perl.action.deparsing.progress", file.getName()), true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            try {
                ProcessOutput processOutput = PerlHostData.execAndGetOutput(commandLine);
                String deparsed = processOutput.getStdout();
                String error = processOutput.getStderr();
                if (StringUtil.isNotEmpty(error) && !StringUtil.contains(error, "syntax OK")) {
                    if (StringUtil.isEmpty(deparsed)) {
                        Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.action.error.notification.title"), error.replaceAll("\\n", "<br/>"), NotificationType.ERROR));
                    } else {
                        Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.action.success.notification.title"), PerlBundle.message("perl.action.success.notification.message", error.replaceAll("\\n", "<br/>")), NotificationType.INFORMATION));
                    }
                }
                if (StringUtil.isNotEmpty(deparsed)) {
                    ApplicationManager.getApplication().invokeLater(() -> WriteAction.run(() -> {
                        VirtualFile newFile = new LightVirtualFile("Deparsed " + file.getName(), file.getFileType(), deparsed);
                        OpenFileAction.openFile(newFile, project);
                    }));
                }
            } catch (ExecutionException e) {
                Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.execution.error.notification.title"), e.getMessage().replaceAll("\\n", "<br/>"), NotificationType.ERROR));
            }
        }
    }.queue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Task(com.intellij.openapi.progress.Task) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) Document(com.intellij.openapi.editor.Document) Notification(com.intellij.notification.Notification) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessOutput(com.intellij.execution.process.ProcessOutput) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) PsiFile(com.intellij.psi.PsiFile) ExecutionException(com.intellij.execution.ExecutionException)

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