Search in sources :

Example 86 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project Perl5-IDEA by Camelcade.

the class PerlRunProfileState method startProcess.

@NotNull
@Override
protected ProcessHandler startProcess() throws ExecutionException {
    PerlRunConfiguration runProfile = (PerlRunConfiguration) getEnvironment().getRunProfile();
    VirtualFile scriptFile = runProfile.getScriptFile();
    if (scriptFile == null) {
        throw new ExecutionException("Script file: " + runProfile.getScriptPath() + " is not exists");
    }
    Project project = getEnvironment().getProject();
    String perlSdkPath = PerlProjectManager.getSdkPath(project, scriptFile);
    String alternativeSdkPath = runProfile.getAlternativeSdkPath();
    if (runProfile.isUseAlternativeSdk() && !StringUtil.isEmpty(alternativeSdkPath)) {
        Sdk sdk = PerlSdkTable.getInstance().findJdk(alternativeSdkPath);
        if (sdk != null) {
            perlSdkPath = sdk.getHomePath();
        } else {
            perlSdkPath = alternativeSdkPath;
        }
    }
    if (perlSdkPath == null) {
        throw new ExecutionException("Unable to locate Perl Interpreter");
    }
    String homePath = runProfile.getWorkingDirectory();
    if (StringUtil.isEmpty(homePath)) {
        Module moduleForFile = ModuleUtilCore.findModuleForFile(scriptFile, project);
        if (moduleForFile != null) {
            homePath = PathMacroUtil.getModuleDir(moduleForFile.getModuleFilePath());
        } else {
            homePath = project.getBasePath();
        }
    }
    assert homePath != null;
    GeneralCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, perlSdkPath, scriptFile, getPerlParameters(runProfile));
    String programParameters = runProfile.getProgramParameters();
    if (programParameters != null) {
        commandLine.addParameters(StringUtil.split(programParameters, " "));
    }
    String charsetName = runProfile.getConsoleCharset();
    Charset charset;
    if (!StringUtil.isEmpty(charsetName)) {
        try {
            charset = Charset.forName(charsetName);
        } catch (UnsupportedCharsetException e) {
            throw new ExecutionException("Unknown charset: " + charsetName);
        }
    } else {
        charset = scriptFile.getCharset();
    }
    commandLine.setCharset(charset);
    commandLine.withWorkDirectory(homePath);
    Map<String, String> environment = calcEnv(runProfile);
    commandLine.withEnvironment(environment);
    commandLine.withParentEnvironmentType(runProfile.isPassParentEnvs() ? CONSOLE : NONE);
    OSProcessHandler handler = new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(), charset) {

        @Override
        public void startNotify() {
            super.startNotify();
            String perl5Opt = environment.get(PerlRunUtil.PERL5OPT);
            if (StringUtil.isNotEmpty(perl5Opt)) {
                notifyTextAvailable(" - " + PerlRunUtil.PERL5OPT + "=" + perl5Opt + '\n', ProcessOutputTypes.SYSTEM);
            }
        }
    };
    ProcessTerminatedListener.attach(handler, project);
    return handler;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) Charset(java.nio.charset.Charset) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project Perl5-IDEA by Camelcade.

the class PerlCriticAnnotator method getPerlCriticExecutable.

protected GeneralCommandLine getPerlCriticExecutable(Project project) throws ExecutionException {
    PerlSharedSettings sharedSettings = PerlSharedSettings.getInstance(project);
    PerlLocalSettings localSettings = PerlLocalSettings.getInstance(project);
    String executable = localSettings.PERL_CRITIC_PATH;
    if (StringUtil.isEmpty(executable)) {
        throw new ExecutionException("Path to Perl::Critic executable must be configured in perl settings");
    }
    GeneralCommandLine commandLine = new GeneralCommandLine(executable).withWorkDirectory(project.getBasePath());
    if (StringUtil.isNotEmpty(sharedSettings.PERL_CRITIC_ARGS)) {
        commandLine.addParameters(StringUtil.split(sharedSettings.PERL_CRITIC_ARGS, " "));
    }
    return commandLine;
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) PerlLocalSettings(com.perl5.lang.perl.idea.configuration.settings.PerlLocalSettings) ExecutionException(com.intellij.execution.ExecutionException)

Example 88 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project Perl5-IDEA by Camelcade.

the class PerlCriticAnnotator method doAnnotate.

@Nullable
@Override
public List<PerlCriticErrorDescriptor> doAnnotate(final PerlFile sourcePsiFile) {
    if (sourcePsiFile == null) {
        return null;
    }
    byte[] sourceBytes = ReadAction.compute(sourcePsiFile::getPerlContentInBytes);
    if (sourceBytes == null) {
        return null;
    }
    try {
        GeneralCommandLine criticCommandLine = getPerlCriticExecutable(sourcePsiFile.getProject());
        final Process process = criticCommandLine.createProcess();
        OutputStream outputStream = process.getOutputStream();
        outputStream.write(sourceBytes);
        outputStream.close();
        VirtualFile virtualFile = PsiUtilCore.getVirtualFile(sourcePsiFile);
        final CapturingProcessHandler processHandler = new CapturingProcessHandler(process, virtualFile == null ? null : virtualFile.getCharset(), criticCommandLine.getCommandLineString());
        List<PerlCriticErrorDescriptor> errors = new ArrayList<>();
        PerlCriticErrorDescriptor lastDescriptor = null;
        for (String output : processHandler.runProcess().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")) {
                // fixme we could make some popup here
                System.err.println("Could not parse line: " + output);
            }
        }
        return errors;
    } catch (ExecutionException e) {
        Notifications.Bus.notify(new Notification("PerlCritic error", "Perl::Critic failed to start and has been disabled", "<ul style=\"padding-left:10px;margin-left:0px;\">" + "<li>Make sure that Perl::Critic module is installed</li>" + "<li>Configure path to perlcritic executable in <a href=\"configure\">Perl5 settings</a> and re-enable it</li>" + "</ul>", NotificationType.ERROR, new NotificationListener.Adapter() {

            @Override
            protected void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent e) {
                Perl5SettingsConfigurable.open(sourcePsiFile);
                notification.expire();
            }
        }));
        PerlSharedSettings.getInstance(sourcePsiFile.getProject()).PERL_CRITIC_ENABLED = false;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HyperlinkEvent(javax.swing.event.HyperlinkEvent) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Notification(com.intellij.notification.Notification) ExecutionException(com.intellij.execution.ExecutionException) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) NotificationListener(com.intellij.notification.NotificationListener) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project Perl5-IDEA by Camelcade.

the class PerlDeparseFileAction method actionPerformed.

@Override
public void actionPerformed(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;
    }
    GeneralCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, file.getVirtualFile(), deparseArgument);
    if (commandLine == null) {
        return;
    }
    commandLine.withWorkDirectory(project.getBasePath());
    FileDocumentManager.getInstance().saveDocument(document);
    try {
        ProcessOutput processOutput = ExecUtil.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)) {
            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));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Project(com.intellij.openapi.project.Project) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFile(com.intellij.psi.PsiFile) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) Document(com.intellij.openapi.editor.Document) ExecutionException(com.intellij.execution.ExecutionException) Notification(com.intellij.notification.Notification)

Example 90 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project clion-embedded-arm by elmot.

the class OpenOcdComponent method createOcdCommandLine.

@SuppressWarnings("WeakerAccess")
@NotNull
public static GeneralCommandLine createOcdCommandLine(@NotNull Project project, @Nullable File fileToLoad, @Nullable String additionalCommand, boolean shutdown) throws ConfigurationException {
    OpenOcdSettingsState ocdSettings = project.getComponent(OpenOcdSettingsState.class);
    if (ocdSettings.boardConfigFile == null || "".equals(ocdSettings.boardConfigFile.trim())) {
        throw new ConfigurationException("Board Config file is not defined.\nPlease open OpenOCD settings and choose one.", "OpenOCD run error");
    }
    VirtualFile ocdHome = require(LocalFileSystem.getInstance().findFileByPath(ocdSettings.openOcdHome));
    VirtualFile ocdBinary = require(ocdHome.findFileByRelativePath(BIN_OPENOCD));
    File ocdBinaryIo = VfsUtil.virtualToIoFile(ocdBinary);
    GeneralCommandLine commandLine = new PtyCommandLine().withWorkDirectory(ocdBinaryIo.getParentFile()).withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE).withParameters("-c", "tcl_port disabled").withExePath(ocdBinaryIo.getAbsolutePath());
    VirtualFile ocdScripts = require(OpenOcdSettingsState.findOcdScripts(ocdHome));
    commandLine.addParameters("-s", VfsUtil.virtualToIoFile(ocdScripts).getAbsolutePath());
    if (ocdSettings.gdbPort != OpenOcdSettingsState.DEF_GDB_PORT) {
        commandLine.addParameters("-c", "gdb_port " + ocdSettings.gdbPort);
    }
    if (ocdSettings.telnetPort != OpenOcdSettingsState.DEF_TELNET_PORT) {
        commandLine.addParameters("-c", "telnet_port " + ocdSettings.telnetPort);
    }
    commandLine.addParameters("-f", ocdSettings.boardConfigFile);
    if (fileToLoad != null) {
        String command = "program \"" + fileToLoad.getAbsolutePath().replace(File.separatorChar, '/') + "\"";
        commandLine.addParameters("-c", command);
    }
    if (additionalCommand != null && !additionalCommand.isEmpty()) {
        commandLine.addParameters("-c", additionalCommand);
    }
    if (shutdown) {
        commandLine.addParameters("-c", "shutdown");
    }
    return commandLine;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConfigurationException(com.intellij.openapi.options.ConfigurationException) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) PtyCommandLine(com.intellij.execution.configurations.PtyCommandLine) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)179 ExecutionException (com.intellij.execution.ExecutionException)70 NotNull (org.jetbrains.annotations.NotNull)47 File (java.io.File)41 VirtualFile (com.intellij.openapi.vfs.VirtualFile)31 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)25 ProcessOutput (com.intellij.execution.process.ProcessOutput)25 Key (com.intellij.openapi.util.Key)18 ProcessEvent (com.intellij.execution.process.ProcessEvent)15 Project (com.intellij.openapi.project.Project)15 IOException (java.io.IOException)15 Nullable (org.jetbrains.annotations.Nullable)15 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)14 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)13 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)12 Sdk (com.intellij.openapi.projectRoots.Sdk)9 PsiFile (com.intellij.psi.PsiFile)9 ArrayList (java.util.ArrayList)9 Module (com.intellij.openapi.module.Module)8 ProcessHandler (com.intellij.execution.process.ProcessHandler)7