Search in sources :

Example 11 with PerlCommandLine

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

the class PerlRunUtil method getPerlCommandLine.

/**
 * Builds non-patched perl command line (without patching by version manager)
 *
 * @return new perl command line or null if sdk is missing or corrupted
 */
@Nullable
public static PerlCommandLine getPerlCommandLine(@NotNull Project project, @Nullable Sdk perlSdk, @Nullable String localScriptPath, @NotNull List<String> perlParameters, @NotNull List<String> scriptParameters) {
    if (perlSdk == null) {
        perlSdk = PerlProjectManager.getSdk(project);
    }
    if (perlSdk == null) {
        LOG.error("No sdk provided or available in project " + project);
        return null;
    }
    String interpreterPath = PerlProjectManager.getInterpreterPath(perlSdk);
    if (StringUtil.isEmpty(interpreterPath)) {
        LOG.warn("Empty interpreter path in " + perlSdk + " while building command line for " + localScriptPath);
        return null;
    }
    PerlCommandLine commandLine = new PerlCommandLine(perlSdk).withProject(project);
    commandLine.setExePath(interpreterPath);
    PerlHostData<?, ?> hostData = PerlHostData.notNullFrom(perlSdk);
    for (VirtualFile libRoot : PerlProjectManager.getInstance(project).getModulesLibraryRoots()) {
        commandLine.addParameter(PERL_I + hostData.getRemotePath(libRoot.getCanonicalPath()));
    }
    commandLine.addParameters(perlParameters);
    if (StringUtil.isNotEmpty(localScriptPath)) {
        String remoteScriptPath = hostData.getRemotePath(localScriptPath);
        if (remoteScriptPath != null) {
            commandLine.addParameter(remoteScriptPath);
        }
    }
    commandLine.addParameters(scriptParameters);
    return commandLine;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with PerlCommandLine

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

the class PerlDockerAdapter method createProcess.

/**
 * Wrapping a {@code commandLine} to a script and runs it using {@code docker} command, returning it's process
 */
public Process createProcess(@NotNull PerlCommandLine commandLine) throws ExecutionException {
    PerlCommandLine dockerCommandLine = buildBaseProcessCommandLine(commandLine);
    // mounting helpers
    dockerCommandLine.withParameters(WITH_VOLUME, PerlPluginUtil.getPluginHelpersRoot() + ':' + myData.getHelpersRootPath());
    if (!commandLine.isUserCommandLine()) {
        dockerCommandLine.withParameters(WITH_ENTRYPOINT, "");
    }
    Project project = commandLine.getEffectiveProject();
    if (project != null) {
        // mounting modules roots
        Set<VirtualFile> roots = new HashSet<>();
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            roots.addAll(Arrays.asList(ModuleRootManager.getInstance(module).getContentRoots()));
        }
        roots.addAll(PerlProjectManager.getInstance(project).getExternalLibraryRoots());
        for (VirtualFile rootToMount : VfsUtil.getCommonAncestors(roots.toArray(VirtualFile.EMPTY_ARRAY))) {
            String localPath = rootToMount.getPath();
            String remotePath = myData.getRemotePath(localPath);
            dockerCommandLine.withParameters(WITH_VOLUME, localPath + ':' + remotePath);
        }
        // adding project settings if possible
        dockerCommandLine.withParameters(StringUtil.split(PerlDockerProjectSettings.getInstance(project).getAdditionalDockerParameters(), " "));
    }
    // working directory
    File remoteWorkingDirectory = myData.getRemotePath(commandLine.getWorkDirectory());
    if (remoteWorkingDirectory != null) {
        dockerCommandLine.withParameters(WORKING_DIRECTORY + "=" + StringUtil.escapeChar(FileUtil.toSystemIndependentName(remoteWorkingDirectory.toString()), ' '));
    }
    // required by coverage, probably we should have a getter for this; Also contains a temp path
    String localSystemPath = PathManager.getSystemPath();
    dockerCommandLine.withParameters(WITH_VOLUME, localSystemPath + ':' + myData.getRemotePath(localSystemPath));
    // we sure that command script is under system dir
    File script = createCommandScript(commandLine);
    String dockerScriptPath = myData.getRemotePath(script.getPath());
    if (StringUtil.isEmpty(dockerScriptPath)) {
        throw new ExecutionException("Unable to map path for " + script.getPath() + " in " + myData);
    }
    return dockerCommandLine.withParameters(myData.getImageName(), "sh", dockerScriptPath).createProcess();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) PerlExecutionException(com.perl5.lang.perl.idea.sdk.host.PerlExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 13 with PerlCommandLine

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

the class PerlDockerProjectSettingsConfigurable method updatePreview.

private void updatePreview() {
    PerlCommandLine dockerCommandLine = PerlDockerAdapter.buildBaseProcessCommandLine(new PerlCommandLine());
    dockerCommandLine.addParameters(StringUtil.split(myArgumentsEditor.getText(), " "));
    dockerCommandLine.addParameters("<project_and_helpers_volumes>", "<image_name>", "sh", "<shell_script_with_commands>");
    myPreviewEditor.setText(dockerCommandLine.getCommandLineString());
}
Also used : PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine)

Example 14 with PerlCommandLine

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

the class PerlRunAnythingProvider method execute.

@Override
public void execute(@NotNull DataContext dataContext, @NotNull List<CommandElement> value) {
    LOG.info("Running " + getCommand(value));
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        LOG.warn("No project in context");
        return;
    }
    if (!PerlProjectManager.isPerlEnabled(project)) {
        LOG.warn("Perl is not configured for project " + project);
        return;
    }
    PerlRunUtil.runInConsole(new PerlCommandLine(getCommand(value)).withProject(project));
}
Also used : Project(com.intellij.openapi.project.Project) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine)

Example 15 with PerlCommandLine

use of com.perl5.lang.perl.idea.execution.PerlCommandLine 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();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) OutputStream(java.io.OutputStream) IOException(java.io.IOException) 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) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) ExecutionException(com.intellij.execution.ExecutionException) BaseProcessHandler(com.intellij.execution.process.BaseProcessHandler)

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