Search in sources :

Example 56 with GeneralCommandLine

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

the class Tool method execute.

public void execute(AnActionEvent event, DataContext dataContext, long executionId, @Nullable final ProcessListener processListener) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        return;
    }
    FileDocumentManager.getInstance().saveAllDocuments();
    try {
        if (isUseConsole()) {
            ExecutionEnvironment environment = ExecutionEnvironmentBuilder.create(project, DefaultRunExecutor.getRunExecutorInstance(), new ToolRunProfile(this, dataContext)).build();
            environment.setExecutionId(executionId);
            environment.getRunner().execute(environment, new ProgramRunner.Callback() {

                @Override
                public void processStarted(RunContentDescriptor descriptor) {
                    ProcessHandler processHandler = descriptor.getProcessHandler();
                    if (processHandler != null && processListener != null) {
                        LOG.assertTrue(!processHandler.isStartNotified(), "ProcessHandler is already startNotified, the listener won't be correctly notified");
                        processHandler.addProcessListener(processListener);
                    }
                }
            });
        } else {
            GeneralCommandLine commandLine = createCommandLine(dataContext);
            if (commandLine == null) {
                return;
            }
            OSProcessHandler handler = new OSProcessHandler(commandLine);
            handler.addProcessListener(new ToolProcessAdapter(project, synchronizeAfterExecution(), getName()));
            if (processListener != null) {
                handler.addProcessListener(processListener);
            }
            handler.startNotify();
        }
    } catch (ExecutionException ex) {
        ExecutionErrorDialog.show(ex, ToolsBundle.message("tools.process.start.error"), project);
    }
}
Also used : Project(com.intellij.openapi.project.Project) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) ProcessHandler(com.intellij.execution.process.ProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner) ExecutionException(com.intellij.execution.ExecutionException)

Example 57 with GeneralCommandLine

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

the class ToolRunProfile method getState.

@Override
public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) {
    final Project project = env.getProject();
    if (myCommandLine == null) {
        // can return null if creation of cmd line has been cancelled
        return null;
    }
    final CommandLineState commandLineState = new CommandLineState(env) {

        GeneralCommandLine createCommandLine() {
            return myCommandLine;
        }

        @Override
        @NotNull
        protected OSProcessHandler startProcess() throws ExecutionException {
            final GeneralCommandLine commandLine = createCommandLine();
            final OSProcessHandler processHandler = new ColoredProcessHandler(commandLine);
            ProcessTerminatedListener.attach(processHandler);
            return processHandler;
        }

        @Override
        @NotNull
        public ExecutionResult execute(@NotNull final Executor executor, @NotNull ProgramRunner runner) throws ExecutionException {
            final ExecutionResult result = super.execute(executor, runner);
            final ProcessHandler processHandler = result.getProcessHandler();
            if (processHandler != null) {
                processHandler.addProcessListener(new ToolProcessAdapter(project, myTool.synchronizeAfterExecution(), getName()));
                processHandler.addProcessListener(new ProcessAdapter() {

                    @Override
                    public void onTextAvailable(ProcessEvent event, Key outputType) {
                        if ((outputType == ProcessOutputTypes.STDOUT && myTool.isShowConsoleOnStdOut()) || (outputType == ProcessOutputTypes.STDERR && myTool.isShowConsoleOnStdErr())) {
                            ExecutionManager.getInstance(project).getContentManager().toFrontRunContent(executor, processHandler);
                        }
                    }
                });
            }
            return result;
        }
    };
    TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
    final FilterInfo[] outputFilters = myTool.getOutputFilters();
    for (FilterInfo outputFilter : outputFilters) {
        builder.addFilter(new RegexpFilter(project, outputFilter.getRegExp()));
    }
    commandLineState.setConsoleBuilder(builder);
    return commandLineState;
}
Also used : RegexpFilter(com.intellij.execution.filters.RegexpFilter) ExecutionResult(com.intellij.execution.ExecutionResult) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Executor(com.intellij.execution.Executor) TextConsoleBuilder(com.intellij.execution.filters.TextConsoleBuilder) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) CommandLineState(com.intellij.execution.configurations.CommandLineState) ProgramRunner(com.intellij.execution.runners.ProgramRunner) Key(com.intellij.openapi.util.Key)

Example 58 with GeneralCommandLine

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

the class BrowserLauncherAppless method doLaunch.

private boolean doLaunch(@Nullable String url, @NotNull List<String> command, @Nullable WebBrowser browser, @Nullable Project project, @NotNull String[] additionalParameters, @Nullable Runnable launchTask) {
    GeneralCommandLine commandLine = new GeneralCommandLine(command);
    if (url != null && url.startsWith("jar:")) {
        return false;
    }
    if (url != null) {
        commandLine.addParameter(url);
    }
    final BrowserSpecificSettings browserSpecificSettings = browser == null ? null : browser.getSpecificSettings();
    if (browserSpecificSettings != null) {
        commandLine.getEnvironment().putAll(browserSpecificSettings.getEnvironmentVariables());
    }
    addArgs(commandLine, browserSpecificSettings, additionalParameters);
    try {
        Process process = commandLine.createProcess();
        checkCreatedProcess(browser, project, commandLine, process, launchTask);
        return true;
    } catch (ExecutionException e) {
        showError(e.getMessage(), browser, project, null, null);
        return false;
    }
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException)

Example 59 with GeneralCommandLine

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

the class EnvironmentVariablesTextFieldWithBrowseButton method showParentEnvironmentDialog.

public static void showParentEnvironmentDialog(@NotNull Component parent) {
    EnvVariablesTable table = new EnvVariablesTable();
    table.setValues(convertToVariables(new TreeMap<>(new GeneralCommandLine().getParentEnvironment()), true));
    table.getActionsPanel().setVisible(false);
    DialogBuilder builder = new DialogBuilder(parent);
    builder.setTitle(ExecutionBundle.message("environment.variables.system.dialog.title"));
    builder.centerPanel(table.getComponent());
    builder.addCloseButton();
    builder.show();
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) EnvVariablesTable(com.intellij.execution.util.EnvVariablesTable) TreeMap(java.util.TreeMap) DialogBuilder(com.intellij.openapi.ui.DialogBuilder)

Example 60 with GeneralCommandLine

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

the class ExecUtil method sudoCommand.

@NotNull
private static GeneralCommandLine sudoCommand(@NotNull GeneralCommandLine commandLine, @NotNull String prompt) throws ExecutionException, IOException {
    if (SystemInfo.isUnix && "root".equals(System.getenv("USER"))) {
        return commandLine;
    }
    List<String> command = ContainerUtil.newArrayList();
    command.add(commandLine.getExePath());
    command.addAll(commandLine.getParametersList().getList());
    GeneralCommandLine sudoCommandLine;
    if (SystemInfo.isMac) {
        String escapedCommandLine = StringUtil.join(command, ExecUtil::escapeAppleScriptArgument, " & \" \" & ");
        String escapedScript = "tell current application\n" + "   activate\n" + "   do shell script " + escapedCommandLine + " with administrator privileges without altering line endings\n" + "end tell";
        sudoCommandLine = new GeneralCommandLine(getOsascriptPath(), "-e", escapedScript);
    } else if (hasGkSudo.getValue()) {
        List<String> sudoCommand = ContainerUtil.newArrayList();
        sudoCommand.addAll(Arrays.asList("gksudo", "--message", prompt, "--"));
        sudoCommand.addAll(command);
        sudoCommandLine = new GeneralCommandLine(sudoCommand);
    } else if (hasKdeSudo.getValue()) {
        List<String> sudoCommand = ContainerUtil.newArrayList();
        sudoCommand.addAll(Arrays.asList("kdesudo", "--comment", prompt, "--"));
        sudoCommand.addAll(command);
        sudoCommandLine = new GeneralCommandLine(sudoCommand);
    } else if (hasPkExec.getValue()) {
        command.add(0, "pkexec");
        sudoCommandLine = new GeneralCommandLine(command);
    } else if (SystemInfo.isUnix && hasTerminalApp()) {
        String escapedCommandLine = StringUtil.join(command, ExecUtil::escapeUnixShellArgument, " ");
        File script = createTempExecutableScript("sudo", ".sh", "#!/bin/sh\n" + "echo " + escapeUnixShellArgument(prompt) + "\n" + "echo\n" + "sudo -- " + escapedCommandLine + "\n" + "STATUS=$?\n" + "echo\n" + "read -p \"Press Enter to close this window...\" TEMP\n" + "exit $STATUS\n");
        sudoCommandLine = new GeneralCommandLine(getTerminalCommand("Install", script.getAbsolutePath()));
    } else {
        throw new UnsupportedOperationException("Unsupported OS/desktop: " + SystemInfo.OS_NAME + '/' + SystemInfo.SUN_DESKTOP);
    }
    return sudoCommandLine.withWorkDirectory(commandLine.getWorkDirectory()).withEnvironment(commandLine.getEnvironment()).withParentEnvironmentType(commandLine.getParentEnvironmentType()).withRedirectErrorStream(commandLine.isRedirectErrorStream());
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) List(java.util.List) 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