Search in sources :

Example 6 with CapturingProcessHandler

use of com.intellij.execution.process.CapturingProcessHandler in project intellij-community by JetBrains.

the class ShowFilePathAction method doOpen.

private static void doOpen(@NotNull File _dir, @Nullable File _toSelect) throws IOException, ExecutionException {
    String dir = FileUtil.toSystemDependentName(FileUtil.toCanonicalPath(_dir.getPath()));
    String toSelect = _toSelect != null ? FileUtil.toSystemDependentName(FileUtil.toCanonicalPath(_toSelect.getPath())) : null;
    if (SystemInfo.isWindows) {
        String cmd = toSelect != null ? "explorer /select," + toSelect : "explorer /root," + dir;
        // no quoting/escaping is needed
        Process process = Runtime.getRuntime().exec(cmd);
        new CapturingProcessHandler(process, null, cmd).runProcess().checkSuccess(LOG);
    } else if (SystemInfo.isMac) {
        GeneralCommandLine cmd = toSelect != null ? new GeneralCommandLine("open", "-R", toSelect) : new GeneralCommandLine("open", dir);
        ExecUtil.execAndGetOutput(cmd).checkSuccess(LOG);
    } else if (fileManagerApp.getValue() != null) {
        schedule(new GeneralCommandLine(fileManagerApp.getValue(), toSelect != null ? toSelect : dir));
    } else if (SystemInfo.hasXdgOpen()) {
        schedule(new GeneralCommandLine("xdg-open", dir));
    } else if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
        Desktop.getDesktop().open(new File(dir));
    } else {
        Messages.showErrorDialog("This action isn't supported on the current platform", "Cannot Open File");
    }
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) VirtualFile(com.intellij.openapi.vfs.VirtualFile) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Example 7 with CapturingProcessHandler

use of com.intellij.execution.process.CapturingProcessHandler in project intellij-community by JetBrains.

the class Executor method run.

@NotNull
public static String run(@NotNull File workingDir, @NotNull List<String> params, boolean ignoreNonZeroExitCode) throws ExecutionException {
    final ProcessBuilder builder = new ProcessBuilder().command(params);
    builder.directory(workingDir);
    builder.redirectErrorStream(true);
    Process clientProcess;
    try {
        clientProcess = builder.start();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    String commandLine = StringUtil.join(params, " ");
    CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), commandLine);
    ProcessOutput result = handler.runProcess(30 * 1000);
    if (result.isTimeout()) {
        throw new RuntimeException("Timeout waiting for the command execution. Command: " + commandLine);
    }
    String stdout = result.getStdout().trim();
    if (result.getExitCode() != 0) {
        if (ignoreNonZeroExitCode) {
            debug("{" + result.getExitCode() + "}");
        }
        debug(stdout);
        if (!ignoreNonZeroExitCode) {
            throw new ExecutionException(result.getExitCode(), stdout);
        }
    } else {
        debug(stdout);
    }
    return stdout;
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) IOException(java.io.IOException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with CapturingProcessHandler

use of com.intellij.execution.process.CapturingProcessHandler in project intellij-community by JetBrains.

the class TestClientRunner method runClient.

public ProcessOutput runClient(@NotNull String exeName, @Nullable String stdin, @Nullable final File workingDir, String... commandLine) throws IOException {
    final List<String> arguments = new ArrayList<>();
    final File client = new File(myClientBinaryPath, SystemInfo.isWindows ? exeName + ".exe" : exeName);
    if (client.exists()) {
        arguments.add(client.toString());
    } else {
        // assume client is in path
        arguments.add(exeName);
    }
    Collections.addAll(arguments, commandLine);
    if (myTraceClient) {
        LOG.info("*** running:\n" + arguments);
        if (StringUtil.isNotEmpty(stdin)) {
            LOG.info("*** stdin:\n" + stdin);
        }
    }
    final ProcessBuilder builder = new ProcessBuilder().command(arguments);
    if (workingDir != null) {
        builder.directory(workingDir);
    }
    if (myClientEnvironment != null) {
        builder.environment().putAll(myClientEnvironment);
    }
    final Process clientProcess = builder.start();
    if (stdin != null) {
        final OutputStream outputStream = clientProcess.getOutputStream();
        try {
            final byte[] bytes = stdin.getBytes();
            outputStream.write(bytes);
        } finally {
            outputStream.close();
        }
    }
    final CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), StringUtil.join(arguments, " "));
    final ProcessOutput result = handler.runProcess(100 * 1000, false);
    if (myTraceClient || result.isTimeout()) {
        LOG.debug("*** result: " + result.getExitCode());
        final String out = result.getStdout().trim();
        if (out.length() > 0) {
            LOG.debug("*** output:\n" + out);
        }
        final String err = result.getStderr().trim();
        if (err.length() > 0) {
            LOG.debug("*** error:\n" + err);
        }
    }
    if (result.isTimeout()) {
        String processList = LogUtil.getProcessList();
        handler.destroyProcess();
        throw new RuntimeException("Timeout waiting for VCS client to finish execution:\n" + processList);
    }
    return result;
}
Also used : OutputStream(java.io.OutputStream) ProcessOutput(com.intellij.execution.process.ProcessOutput) ArrayList(java.util.ArrayList) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Example 9 with CapturingProcessHandler

use of com.intellij.execution.process.CapturingProcessHandler in project intellij-community by JetBrains.

the class GitExecutableDetector method runs.

/**
   * Checks if it is possible to run the specified program.
   * Made protected for tests not to start a process there.
   */
protected boolean runs(@NotNull String exec) {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(exec);
    commandLine.setCharset(CharsetToolkit.getDefaultSystemCharset());
    try {
        CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
        ProcessOutput result = handler.runProcess((int) TimeUnit.SECONDS.toMillis(5));
        return !result.isTimeout();
    } catch (ExecutionException e) {
        return false;
    }
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) ExecutionException(com.intellij.execution.ExecutionException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Example 10 with CapturingProcessHandler

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

the class GradleImportTest method assertBuildsCleanly.

public static void assertBuildsCleanly(File base, boolean allowWarnings) throws Exception {
    File gradlew = new File(base, isWindows() ? FN_GRADLE_WRAPPER_WIN : FN_GRADLE_WRAPPER_UNIX);
    if (!gradlew.exists()) {
        // Not using a wrapper; can't easily test building (we don't have a gradle prebuilt)
        return;
    }
    File pwd = base.getAbsoluteFile();
    List<String> args = Lists.newArrayList();
    args.add(gradlew.getPath());
    args.add("assembleDebug");
    GradleInitScripts.getInstance().addLocalMavenRepoInitScriptCommandLineArgTo(args);
    GeneralCommandLine cmdLine = new GeneralCommandLine(args).withWorkDirectory(pwd);
    CapturingProcessHandler process = new CapturingProcessHandler(cmdLine);
    // Building currently takes about 30s, so a 5min timeout should give a safe margin.
    int timeoutInMilliseconds = 5 * 60 * 1000;
    ProcessOutput processOutput = process.runProcess(timeoutInMilliseconds, true);
    if (processOutput.isTimeout()) {
        throw new TimeoutException("\"gradlew assembleDebug\" did not terminate within test timeout value.\n" + "[stdout]\n" + processOutput.getStdout() + "\n" + "[stderr]\n" + processOutput.getStderr() + "\n");
    }
    String errors = processOutput.getStderr();
    String output = processOutput.getStdout();
    int exitCode = processOutput.getExitCode();
    int expectedExitCode = 0;
    if (output.contains("BUILD FAILED") && errors.contains("Could not find any version that matches com.android.tools.build:gradle:")) {
        // We ignore this assertion. We got here because we are using a version of the
        // Android Gradle plug-in that is not available in Maven Central yet.
        expectedExitCode = 1;
    } else {
        assertTrue(output + "\n" + errors, output.contains("BUILD SUCCESSFUL"));
        if (!allowWarnings) {
            assertEquals(output + "\n" + errors, "", errors);
        }
    }
    assertEquals(expectedExitCode, exitCode);
    System.out.println("Built project successfully; output was:\n" + output);
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)14 ProcessOutput (com.intellij.execution.process.ProcessOutput)13 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)10 ExecutionException (com.intellij.execution.ExecutionException)6 File (java.io.File)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 OutputStream (java.io.OutputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 RunCanceledByUserException (com.intellij.execution.RunCanceledByUserException)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)1 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)1