Search in sources :

Example 36 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-community by JetBrains.

the class EditExternallyAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
    Options options = OptionsManager.getInstance().getOptions();
    String executablePath = options.getExternalEditorOptions().getExecutablePath();
    if (StringUtil.isEmpty(executablePath)) {
        Messages.showErrorDialog(project, ImagesBundle.message("error.empty.external.editor.path"), ImagesBundle.message("error.title.empty.external.editor.path"));
        ImagesConfigurable.show(project);
    } else {
        if (files != null) {
            Map<String, String> env = EnvironmentUtil.getEnvironmentMap();
            for (String varName : env.keySet()) {
                if (SystemInfo.isWindows) {
                    executablePath = StringUtil.replace(executablePath, "%" + varName + "%", env.get(varName), true);
                } else {
                    executablePath = StringUtil.replace(executablePath, "${" + varName + "}", env.get(varName), false);
                }
            }
            executablePath = FileUtil.toSystemDependentName(executablePath);
            File executable = new File(executablePath);
            GeneralCommandLine commandLine = new GeneralCommandLine();
            final String path = executable.exists() ? executable.getAbsolutePath() : executablePath;
            if (SystemInfo.isMac) {
                commandLine.setExePath(ExecUtil.getOpenCommandPath());
                commandLine.addParameter("-a");
                commandLine.addParameter(path);
            } else {
                commandLine.setExePath(path);
            }
            ImageFileTypeManager typeManager = ImageFileTypeManager.getInstance();
            for (VirtualFile file : files) {
                if (file.isInLocalFileSystem() && typeManager.isImage(file)) {
                    commandLine.addParameter(VfsUtilCore.virtualToIoFile(file).getAbsolutePath());
                }
            }
            commandLine.setWorkDirectory(new File(executablePath).getParentFile());
            try {
                commandLine.createProcess();
            } catch (ExecutionException ex) {
                Messages.showErrorDialog(project, ex.getLocalizedMessage(), ImagesBundle.message("error.title.launching.external.editor"));
                ImagesConfigurable.show(project);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Options(org.intellij.images.options.Options) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ImageFileTypeManager(org.intellij.images.fileTypes.ImageFileTypeManager) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 37 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-elixir by KronicDeth.

the class ElixirSystemUtil method getProcessOutput.

@NotNull
public static ProcessOutput getProcessOutput(int timeout, @Nullable String workDir, @NotNull String exePath, @NotNull String... arguments) throws ExecutionException {
    if (workDir == null || !new File(workDir).isDirectory() || !new File(exePath).canExecute()) {
        return new ProcessOutput();
    }
    GeneralCommandLine cmd = new GeneralCommandLine();
    cmd.withWorkDirectory(workDir);
    cmd.setExePath(exePath);
    cmd.addParameters(arguments);
    return execute(cmd, timeout);
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-elixir by KronicDeth.

the class MixProjectRootStep method getMixPath.

/**
   * private methods
   * */
@NotNull
private static String getMixPath(@Nullable String directory) {
    if (directory != null) {
        File mix = new File(directory, "mix");
        if (mix.exists() && mix.canExecute()) {
            return mix.getPath();
        }
    }
    String output = "";
    GeneralCommandLine generalCommandLine = null;
    if (SystemInfo.isWindows) {
        generalCommandLine = new GeneralCommandLine("where");
        generalCommandLine.addParameter("mix.bat");
    } else if (SystemInfo.isMac || SystemInfo.isLinux || SystemInfo.isUnix) {
        generalCommandLine = new GeneralCommandLine("which");
        generalCommandLine.addParameter("mix");
    }
    if (generalCommandLine != null) {
        try {
            output = ScriptRunnerUtil.getProcessOutput(generalCommandLine);
        } catch (Exception ignored) {
            LOG.warn(ignored);
        }
    }
    return output.trim();
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ExecutionException(com.intellij.execution.ExecutionException) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-elixir by KronicDeth.

the class MixProjectRootStep method fetchDependencies.

private static void fetchDependencies(@NotNull final VirtualFile projectRoot, @NotNull final String mixPath) {
    final Project project = ProjectImportBuilder.getCurrentProject();
    String sdkPath = project != null ? ElixirSdkType.getSdkPath(project) : null;
    final String elixirPath = sdkPath != null ? JpsElixirSdkType.getScriptInterpreterExecutable(sdkPath).getAbsolutePath() : JpsElixirSdkType.getExecutableFileName(JpsElixirSdkType.SCRIPT_INTERPRETER);
    ProgressManager.getInstance().run(new Task.Modal(project, "Fetching dependencies", true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            GeneralCommandLine commandLine = new GeneralCommandLine();
            commandLine.withWorkDirectory(projectRoot.getCanonicalPath());
            commandLine.setExePath(elixirPath);
            commandLine.addParameter(mixPath);
            commandLine.addParameter("deps.get");
            try {
                OSProcessHandler handler = new OSProcessHandler(commandLine.createProcess(), commandLine.getPreparedCommandLine(Platform.current()));
                handler.addProcessListener(new ProcessAdapter() {

                    @Override
                    public void onTextAvailable(ProcessEvent event, Key outputType) {
                        String text = event.getText();
                        indicator.setText2(text);
                    }
                });
                ProcessTerminatedListener.attach(handler);
                handler.startNotify();
                handler.waitFor();
                indicator.setText2("Refreshing");
            } catch (ExecutionException e) {
                LOG.warn(e);
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 40 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-elixir by KronicDeth.

the class ExtProcessUtil method execAndGetFirstLine.

@NotNull
public static ExtProcessOutput execAndGetFirstLine(long timeout, @NotNull String... command) {
    try {
        final Process cmdRunner = new GeneralCommandLine(command).createProcess();
        ExecutorService singleTreadExecutor = Executors.newSingleThreadExecutor();
        try {
            Future<ExtProcessOutput> cmdRunnerFuture = singleTreadExecutor.submit(new Callable<ExtProcessOutput>() {

                @Override
                public ExtProcessOutput call() throws Exception {
                    cmdRunner.waitFor();
                    String stdOut = readLine(cmdRunner.getInputStream());
                    String stdErr = readLine(cmdRunner.getErrorStream());
                    return new ExtProcessOutput(stdOut, stdErr);
                }
            });
            try {
                return cmdRunnerFuture.get(timeout, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
            // Suppress
            }
            cmdRunnerFuture.cancel(true);
        } finally {
            singleTreadExecutor.shutdown();
        }
    } catch (ExecutionException e) {
    // Suppress
    }
    return new ExtProcessOutput("", "");
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException) ExecutionException(com.intellij.execution.ExecutionException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)137 ExecutionException (com.intellij.execution.ExecutionException)42 File (java.io.File)35 NotNull (org.jetbrains.annotations.NotNull)35 ProcessOutput (com.intellij.execution.process.ProcessOutput)20 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)10 Project (com.intellij.openapi.project.Project)10 Key (com.intellij.openapi.util.Key)10 IOException (java.io.IOException)10 Nullable (org.jetbrains.annotations.Nullable)10 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)9 ProcessHandler (com.intellij.execution.process.ProcessHandler)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)7 Sdk (com.intellij.openapi.projectRoots.Sdk)7 PsiFile (com.intellij.psi.PsiFile)7 Test (org.junit.Test)6 CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)5 ArrayList (java.util.ArrayList)5 RunResult (org.jetbrains.kotlin.android.tests.run.RunResult)5