Search in sources :

Example 21 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project android by JetBrains.

the class AndroidUtils method executeCommand.

@NotNull
public static ExecutionStatus executeCommand(@NotNull GeneralCommandLine commandLine, @Nullable final OutputProcessor processor, @Nullable WaitingStrategies.Strategy strategy) throws ExecutionException {
    LOG.info(commandLine.getCommandLineString());
    OSProcessHandler handler = new OSProcessHandler(commandLine);
    final ProcessAdapter listener = new ProcessAdapter() {

        @Override
        public void onTextAvailable(final ProcessEvent event, final Key outputType) {
            if (processor != null) {
                final String message = event.getText();
                processor.onTextAvailable(message);
            }
        }
    };
    if (!(strategy instanceof WaitingStrategies.DoNotWait)) {
        handler.addProcessListener(listener);
    }
    handler.startNotify();
    try {
        if (!(strategy instanceof WaitingStrategies.WaitForever)) {
            if (strategy instanceof WaitingStrategies.WaitForTime) {
                handler.waitFor(((WaitingStrategies.WaitForTime) strategy).getTimeMs());
            }
        } else {
            handler.waitFor();
        }
    } catch (ProcessCanceledException e) {
        return ExecutionStatus.ERROR;
    }
    if (!handler.isProcessTerminated()) {
        return ExecutionStatus.TIMEOUT;
    }
    if (!(strategy instanceof WaitingStrategies.DoNotWait)) {
        handler.removeProcessListener(listener);
    }
    int exitCode = handler.getProcess().exitValue();
    return exitCode == 0 ? ExecutionStatus.SUCCESS : ExecutionStatus.ERROR;
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) Key(com.intellij.openapi.util.Key) RelativePoint(com.intellij.ui.awt.RelativePoint) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project android by JetBrains.

the class Haxm method runInstaller.

private void runInstaller(InstallContext installContext, GeneralCommandLine commandLine) {
    try {
        ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
        if (progressIndicator != null) {
            progressIndicator.setIndeterminate(true);
            progressIndicator.setText(RUNNING_INTEL_HAXM_INSTALLER_MESSAGE);
        }
        installContext.print(RUNNING_INTEL_HAXM_INSTALLER_MESSAGE + "\n", ConsoleViewContentType.SYSTEM_OUTPUT);
        CapturingAnsiEscapesAwareProcessHandler process = new CapturingAnsiEscapesAwareProcessHandler(commandLine);
        final StringBuffer output = new StringBuffer();
        process.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                output.append(event.getText());
                super.onTextAvailable(event, outputType);
            }
        });
        myProgressStep.attachToProcess(process);
        int exitCode = process.runProcess().getExitCode();
        // More testing of bash scripts invocation with intellij process wrappers might be useful.
        if (exitCode != INTEL_HAXM_INSTALLER_EXIT_CODE_SUCCESS) {
            // According to the installer docs for Windows, installer may signify that a reboot is required
            if (SystemInfo.isWindows && exitCode == INTEL_HAXM_INSTALLER_EXIT_CODE_REBOOT_REQUIRED) {
                String rebootMessage = "Reboot required: HAXM installation succeeded, however the installer reported that a reboot is " + "required in order for the changes to take effect";
                installContext.print(rebootMessage, ConsoleViewContentType.NORMAL_OUTPUT);
                AccelerationErrorSolution.promptAndRebootAsync(rebootMessage, ModalityState.NON_MODAL);
                myHaxmInstallerSuccessfullyCompleted = true;
                return;
            }
            // HAXM is not required so we do not stop setup process if this install failed.
            if (myInstallationIntention == HaxmInstallationIntention.UNINSTALL) {
                installContext.print("HAXM uninstallation failed", ConsoleViewContentType.ERROR_OUTPUT);
            } else {
                installContext.print(String.format("HAXM installation failed. To install HAXM follow the instructions found at: %s", SystemInfo.isWindows ? FirstRunWizardDefaults.HAXM_WINDOWS_INSTALL_URL : FirstRunWizardDefaults.HAXM_MAC_INSTALL_URL), ConsoleViewContentType.ERROR_OUTPUT);
            }
            Matcher m = Pattern.compile("installation log:\\s*\"(.*)\"").matcher(output.toString());
            if (m.find()) {
                String file = m.group(1);
                installContext.print(String.format("Installer log is located at %s", file), ConsoleViewContentType.ERROR_OUTPUT);
                try {
                    installContext.print("Installer log contents:\n", ConsoleViewContentType.ERROR_OUTPUT);
                    installContext.print(FileUtil.loadFile(new File(file), "UTF-16"), ConsoleViewContentType.NORMAL_OUTPUT);
                } catch (IOException e) {
                    installContext.print("Failed to read installer output log.\n", ConsoleViewContentType.ERROR_OUTPUT);
                }
            }
            progressIndicator.setFraction(1);
            myHaxmInstallerSuccessfullyCompleted = false;
            return;
        }
        progressIndicator.setFraction(1);
        myHaxmInstallerSuccessfullyCompleted = true;
    } catch (ExecutionException e) {
        installContext.print("Unable to run Intel HAXM installer: " + e.getMessage() + "\n", ConsoleViewContentType.ERROR_OUTPUT);
        LOG.warn(e);
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) CapturingAnsiEscapesAwareProcessHandler(com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File) Key(com.intellij.openapi.util.Key)

Example 23 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project intellij-plugins by JetBrains.

the class FlexCommonUtils method getVersionOfAirSdkIncludedInFlexSdk.

@Nullable
public static String getVersionOfAirSdkIncludedInFlexSdk(final String flexSdkHomePath) {
    final File adtFile = new File(flexSdkHomePath + "/lib/adt.jar");
    if (!adtFile.isFile()) {
        return null;
    }
    String version = ourAdtJarPathAndTimestampToVersion.get(Pair.create(adtFile.getPath(), adtFile.lastModified()));
    if (version != null) {
        return version;
    }
    try {
        final Ref<String> versionRef = Ref.create();
        final String javaExecutable = FileUtil.toSystemDependentName((SystemProperties.getJavaHome() + "/bin/java" + (SystemInfo.isWindows ? ".exe" : "")));
        String[] cmdarray = { javaExecutable, "-jar", adtFile.getPath(), "-version" };
        final Process process = Runtime.getRuntime().exec(cmdarray);
        final BaseOSProcessHandler handler = new BaseOSProcessHandler(process, StringUtil.join(cmdarray, " "), Charset.defaultCharset());
        handler.addProcessListener(new ProcessAdapter() {

            public void onTextAvailable(ProcessEvent event, Key outputType) {
                if (outputType != ProcessOutputTypes.SYSTEM) {
                    parseAirVersionFromAdtOutput(event.getText().trim(), versionRef);
                }
            }
        });
        handler.startNotify();
        handler.waitFor(3000);
        if (!handler.isProcessTerminated()) {
            handler.destroyProcess();
        }
        version = versionRef.get();
        ourAdtJarPathAndTimestampToVersion.put(Pair.create(adtFile.getPath(), adtFile.lastModified()), version);
        return version;
    } catch (IOException e) {
    /*ignore*/
    }
    return null;
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) BaseOSProcessHandler(com.intellij.execution.process.BaseOSProcessHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project android by JetBrains.

the class AndroidCommonUtils method executeZipAlign.

@Nullable
public static String executeZipAlign(@NotNull String zipAlignPath, @NotNull File source, @NotNull File destination) {
    List<String> commandLine = Arrays.asList(zipAlignPath, "-f", "4", source.getAbsolutePath(), destination.getAbsolutePath());
    final ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
    BaseOSProcessHandler handler;
    try {
        handler = new BaseOSProcessHandler(processBuilder.start(), StringUtil.join(commandLine, " "), null);
    } catch (IOException e) {
        return e.getMessage();
    }
    final StringBuilder builder = new StringBuilder();
    handler.addProcessListener(new ProcessAdapter() {

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            builder.append(event.getText());
        }
    });
    handler.startNotify();
    handler.waitFor();
    int exitCode = handler.getProcess().exitValue();
    return exitCode != 0 ? builder.toString() : null;
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) BaseOSProcessHandler(com.intellij.execution.process.BaseOSProcessHandler) Key(com.intellij.openapi.util.Key) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project intellij-plugins by JetBrains.

the class DartPubActionBase method showPubOutputConsole.

private static void showPubOutputConsole(@NotNull final Module module, @NotNull final GeneralCommandLine command, @NotNull final OSProcessHandler processHandler, @NotNull final VirtualFile pubspecYamlFile, @NotNull final String actionTitle) {
    final ConsoleView console;
    PubToolWindowContentInfo info = findExistingInfoForCommand(module.getProject(), command);
    if (info != null) {
        // rerunning the same pub command in the same tool window tab (corresponding tool window action invoked)
        console = info.console;
        console.clear();
    } else {
        console = createConsole(module.getProject(), pubspecYamlFile);
        info = new PubToolWindowContentInfo(module, pubspecYamlFile, command, actionTitle, console);
        final ActionToolbar actionToolbar = createToolWindowActionsBar(info);
        final SimpleToolWindowPanel toolWindowPanel = new SimpleToolWindowPanel(false, true);
        toolWindowPanel.setContent(console.getComponent());
        toolWindowPanel.setToolbar(actionToolbar.getComponent());
        final Content content = ContentFactory.SERVICE.getInstance().createContent(toolWindowPanel.getComponent(), actionTitle, true);
        content.putUserData(PUB_TOOL_WINDOW_CONTENT_INFO_KEY, info);
        Disposer.register(content, console);
        final ContentManager contentManager = MessageView.SERVICE.getInstance(module.getProject()).getContentManager();
        removeOldTabs(contentManager);
        contentManager.addContent(content);
        contentManager.setSelectedContent(content);
        final ToolWindow toolWindow = ToolWindowManager.getInstance(module.getProject()).getToolWindow(ToolWindowId.MESSAGES_WINDOW);
        toolWindow.activate(null, true);
    }
    info.rerunPubCommandAction.setProcessHandler(processHandler);
    info.stopProcessAction.setProcessHandler(processHandler);
    processHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(final ProcessEvent event) {
            console.print(IdeBundle.message("finished.with.exit.code.text.message", event.getExitCode()), ConsoleViewContentType.SYSTEM_OUTPUT);
        }
    });
    console.print(DartBundle.message("working.dir.0", FileUtil.toSystemDependentName(pubspecYamlFile.getParent().getPath())) + "\n", ConsoleViewContentType.SYSTEM_OUTPUT);
    console.attachToProcess(processHandler);
    processHandler.startNotify();
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ConsoleView(com.intellij.execution.ui.ConsoleView) ProcessEvent(com.intellij.execution.process.ProcessEvent) Content(com.intellij.ui.content.Content) ContentManager(com.intellij.ui.content.ContentManager) SimpleToolWindowPanel(com.intellij.openapi.ui.SimpleToolWindowPanel)

Aggregations

ProcessAdapter (com.intellij.execution.process.ProcessAdapter)62 ProcessEvent (com.intellij.execution.process.ProcessEvent)59 Key (com.intellij.openapi.util.Key)19 NotNull (org.jetbrains.annotations.NotNull)15 ProcessHandler (com.intellij.execution.process.ProcessHandler)12 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)10 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)10 ExecutionException (com.intellij.execution.ExecutionException)9 IOException (java.io.IOException)8 BaseOSProcessHandler (com.intellij.execution.process.BaseOSProcessHandler)7 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)5 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)4 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 File (java.io.File)4 ExecutionResult (com.intellij.execution.ExecutionResult)3 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)3 Disposable (com.intellij.openapi.Disposable)3 Semaphore (com.intellij.util.concurrency.Semaphore)3 RunProfile (com.intellij.execution.configurations.RunProfile)2