Search in sources :

Example 21 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project flutter-intellij by flutter.

the class OpenSimulatorAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent event) {
    try {
        final GeneralCommandLine cmd = new GeneralCommandLine().withExePath("open").withParameters("-a", "Simulator.app");
        final OSProcessHandler handler = new OSProcessHandler(cmd);
        handler.addProcessListener(new ProcessAdapter() {

            @Override
            public void processTerminated(final ProcessEvent event) {
                if (event.getExitCode() != 0) {
                    final String msg = event.getText() != null ? event.getText() : "Process error - exit code: (" + event.getExitCode() + ")";
                    FlutterMessages.showError("Error Opening Simulator", msg);
                }
            }
        });
        handler.startNotify();
    } catch (ExecutionException e) {
        FlutterMessages.showError("Error Opening Simulator", FlutterBundle.message("flutter.command.exception.message", e.getMessage()));
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException)

Example 22 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project flutter-intellij by flutter.

the class System method which.

/**
 * Locate a given command-line tool given its name.
 */
@Nullable
public static String which(String toolName) {
    final String whichCommandName = SystemInfo.isWindows ? "where" : "which";
    final GeneralCommandLine cmd = new GeneralCommandLine().withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE).withExePath(whichCommandName).withParameters(toolName);
    try {
        final StringBuilder stringBuilder = new StringBuilder();
        final OSProcessHandler process = new OSProcessHandler(cmd);
        process.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                if (outputType == ProcessOutputTypes.STDOUT) {
                    stringBuilder.append(event.getText());
                }
            }
        });
        process.startNotify();
        // We wait a maximum of 2000ms.
        if (!process.waitFor(2000)) {
            return null;
        }
        final Integer exitCode = process.getExitCode();
        if (exitCode == null || process.getExitCode() != 0) {
            return null;
        }
        final String[] results = stringBuilder.toString().split("\n");
        return results.length == 0 ? null : results[0].trim();
    } catch (ExecutionException | RuntimeException e) {
        return null;
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project evosuite by EvoSuite.

the class IntelliJNotifier method attachProcess.

@Override
public void attachProcess(Process process) {
    if (processHandler != null) {
        detachLastProcess();
    }
    processHandler = new OSProcessHandler(process, null);
    console.attachToProcess(processHandler);
    processHandler.startNotify();
}
Also used : OSProcessHandler(com.intellij.execution.process.OSProcessHandler)

Example 24 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler 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 25 with OSProcessHandler

use of com.intellij.execution.process.OSProcessHandler in project clion-embedded-arm by elmot.

the class OpenOcdComponent method startOpenOcd.

@SuppressWarnings("WeakerAccess")
public Future<STATUS> startOpenOcd(Project project, @Nullable File fileToLoad, @Nullable String additionalCommand) throws ConfigurationException {
    if (project == null)
        return new FutureResult<>(STATUS.FLASH_ERROR);
    GeneralCommandLine commandLine = createOcdCommandLine(project, fileToLoad, additionalCommand, false);
    if (process != null && !process.isProcessTerminated()) {
        LOG.info("openOcd is already run");
        return new FutureResult<>(STATUS.FLASH_ERROR);
    }
    try {
        process = new OSProcessHandler(commandLine) {

            @Override
            public boolean isSilentlyDestroyOnClose() {
                return true;
            }
        };
        DownloadFollower downloadFollower = new DownloadFollower();
        process.addProcessListener(downloadFollower);
        RunContentExecutor openOCDConsole = new RunContentExecutor(project, process).withTitle("OpenOCD console").withActivateToolWindow(true).withFilter(new ErrorFilter(project)).withStop(process::destroyProcess, () -> !process.isProcessTerminated() && !process.isProcessTerminating());
        openOCDConsole.run();
        return downloadFollower;
    } catch (ExecutionException e) {
        ExecutionErrorDialog.show(e, "OpenOCD start failed", project);
        return new FutureResult<>(STATUS.FLASH_ERROR);
    }
}
Also used : FutureResult(com.intellij.util.concurrency.FutureResult) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunContentExecutor(com.intellij.execution.RunContentExecutor) ExecutionException(com.intellij.execution.ExecutionException)

Aggregations

OSProcessHandler (com.intellij.execution.process.OSProcessHandler)55 ExecutionException (com.intellij.execution.ExecutionException)25 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)24 ProcessEvent (com.intellij.execution.process.ProcessEvent)24 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)23 NotNull (org.jetbrains.annotations.NotNull)18 Key (com.intellij.openapi.util.Key)14 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 File (java.io.File)6 Module (com.intellij.openapi.module.Module)5 Sdk (com.intellij.openapi.projectRoots.Sdk)5 IOException (java.io.IOException)5 ProgramRunner (com.intellij.execution.runners.ProgramRunner)4 Nullable (org.jetbrains.annotations.Nullable)4 Executor (com.intellij.execution.Executor)3 RunContentExecutor (com.intellij.execution.RunContentExecutor)3 KillableColoredProcessHandler (com.intellij.execution.process.KillableColoredProcessHandler)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3