@NotNull public static com.intellij.execution.process.ProcessOutput execute(@NotNull GeneralCommandLine cmd, int timeout) throws ExecutionException { CapturingProcessHandler processHandler = new CapturingProcessHandler(cmd.createProcess()); return timeout < 0 ? processHandler.runProcess() : processHandler.runProcess(timeout); }
@NotNull public static ExtProcessOutput execAndGetFirstLine(long timeout, @NotNull String... command) { try { final Process cmdRunner = new GeneralCommandLine(command).withCharset(Charsets.UTF_8).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("", ""); }
generalCommandLine.createProcess(), generalCommandLine.getPreparedCommandLine(Platform.current()) );
@Nullable @Override protected Process createProcess() throws ExecutionException { String shellPath = MongoConfiguration.getInstance(getProject()).getShellPath(); GeneralCommandLine commandLine = MongoUtils.buildCommandLine(shellPath, serverConfiguration, database); notifier.notifyInfo("Running " + commandLine.getCommandLineString()); return commandLine.createProcess(); }
public static boolean checkMongoShellPath(String mongoShellPath) throws ExecutionException { if (isBlank(mongoShellPath)) { return false; } GeneralCommandLine commandLine = new GeneralCommandLine(); commandLine.setExePath(mongoShellPath); commandLine.addParameter("--version"); CapturingProcessHandler handler = new CapturingProcessHandler(commandLine.createProcess(), CharsetToolkit.getDefaultSystemCharset(), commandLine.getCommandLineString()); ProcessOutput result = handler.runProcess(15 * 1000); return result.getExitCode() == 0; }
private int callShellCommand(String command, String fromPath, String toPath) throws ExecutionException, IOException { VirtualFile moduleOutputDir = CompilerPaths.getModuleOutputDirectory(module, false); String outputDir = moduleOutputDir.getPath() + File.separator; SimulatorHelper simulatorHelper = new SimulatorHelper(null, sdk, outputDir); Optional<Integer> simulatorPortNTimes = simulatorHelper.findSimulatorPortNTimes(2, 100); if (!simulatorPortNTimes.isPresent()) { throw new IOException("simulator not found"); } int simulatorPort = simulatorPortNTimes.get(); Process process = createShellCmd(simulatorPort, outputDir, command, fromPath, toPath).createProcess(); try { process.waitFor(10, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IOException("shell did not close in time"); } int i = process.exitValue(); return i; }
/** * Executes commandLine, optionally piping input to stdin, and return stdout. */ @Nullable public static String readCommandLine(@NotNull final GeneralCommandLine commandLine, @Nullable final String input) { String output = null; try { final Process process = commandLine.createProcess(); if (input != null) { final BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(process.getOutputStream())); writer.write(input); writer.flush(); writer.close(); } output = new CapturingProcessHandler(process, Charset.defaultCharset(), commandLine.getCommandLineString() ).runProcess().getStdout(); } catch (final ExecutionException e) { LOG.debug(e); } catch (final IOException e) { LOG.debug(e); } return output; }
private Optional<Integer> findSimulatorPort(int i) { for (int port = SIMULATOR_PORT_MIN; port < SIMULATOR_PORT_MAX; port++) { printToConsole("Attempt " + i + " to connect to simulator (port " + port + ")...\n", ConsoleViewContentType.NORMAL_OUTPUT); try { Process process = createShellCmd(port).createProcess(); if (hasProcessOutputLineContaining(process, SHELL_SUCCESS_CONNECTED_TO_SIMULATOR)) { return Optional.of(port); } } catch (ExecutionException e) { printToConsole(e.getMessage(), ConsoleViewContentType.ERROR_OUTPUT); } } return Optional.empty(); }
cmd.createProcess(), Charset.defaultCharset(), cmd.getCommandLineString()
private static void runBuildProcess(@NotNull CompileContext context, @NotNull GeneralCommandLine commandLine, @NotNull String path) throws ProjectBuildException { try { LOG.debug(commandLine.getCommandLineString()); final Process process = commandLine.createProcess(); BaseOSProcessHandler handler = new BaseOSProcessHandler(process, commandLine.getCommandLineString(), Charset.defaultCharset()); handler.addProcessListener(new ProcessAdapter() { @Override public void onTextAvailable(ProcessEvent event, Key outputType) { final String text = event.getText(); if (!StringUtil.isEmptyOrSpaces(text)) { CompilerMessage compilerMessage = compilerMessageFromOutputLine(text.trim()); context.processMessage(compilerMessage); } } }); handler.startNotify(); handler.waitFor(); } catch (ExecutionException e) { throw new ProjectBuildException(e.getMessage()); } }
@NotNull @Override protected ProcessHandler startProcess() throws ExecutionException { try { final GeneralCommandLine dubCommandLine = getExecutableCommandLine(config); return new OSProcessHandler(dubCommandLine.createProcess(), dubCommandLine.getCommandLineString()); } catch (final ExecutionException e) { final String message = e.getMessage(); final Project project = config.getProject(); final boolean isEmpty = message.equals("DUB executable is not specified"); final boolean notCorrect = message.startsWith("Cannot run program"); if (isEmpty || notCorrect) { Notifications.Bus.notify( new Notification("DUB run configuration", "DUB settings", "DUB executable is " + (isEmpty ? "not specified" : "not specified correctly") + "<br/><a href='configureDLanguageTools'>Configure</a> executable", NotificationType.ERROR, new DToolsNotificationListener(project)), project); } throw e; } }
protected void runSimulator(ConsoleView console) throws ExecutionException { MonkeyParameters monkeyParameters = getMonkeyParameters(); Sdk sdk = monkeyParameters.getSdk(); String outputDir = monkeyParameters.getOutputPath().getPath() + File.separator; SimulatorHelper simulatorHelper = new SimulatorHelper(console, sdk, outputDir); GeneralCommandLine runSimulatorCmd = createRunSimulatorCmd(); runSimulatorCmd.createProcess(); if (!simulatorHelper.findSimulatorPortNTimes(5, 1000).isPresent()) { throw new ExecutionException("Could not connect to simulatorHelper"); } }
@NotNull @Override public ProcessHandler startProcess() throws ExecutionException { try { final GeneralCommandLine appCommandLine = getExecutableCommandLine(config); return new OSProcessHandler(appCommandLine.createProcess(), appCommandLine.getCommandLineString()); } catch (final NoValidDlangSdkFound e) { throw new ExecutionException("No valid DMD SDK found!"); } catch (final ModuleNotFoundException e) { throw new ExecutionException("Run configuration has no module selected."); } catch (final ExecutionException e) { final String message = e.getMessage(); final boolean isEmpty = message.equals("Executable is not specified"); final boolean notCorrect = message.startsWith("Cannot run program"); if (isEmpty || notCorrect) { Notifications.Bus.notify( new Notification("D App run configuration", "D App settings", "D application executable path is " + (isEmpty ? "empty" : "not specified correctly") + "<br/><a href='configure'>Configure</a> output folder", NotificationType.ERROR), config.getProject()); } throw e; } }
public static Boolean isKnownGdb(String path) { try { GeneralCommandLine command = new GeneralCommandLine(); command.setExePath(path); command.addParameter("--version"); ProcessOutput output = new CapturingProcessHandler( command.createProcess(), Charset.defaultCharset(), command.getCommandLineString()).runProcess(); if (output.getExitCode() != 0) { LOG.error("gdb exited with invalid exit code: " + output.getExitCode()); return false; } String cmdOutput = output.getStdout(); return cmdOutput.contains("(GDB) 7.6") || cmdOutput.contains("(GDB) 7.4"); } catch (Exception e) { LOG.error("Exception while executing the process:", e); return false; } }
@Override protected Process createProcess() throws ExecutionException { String bashLocation = BashInterpreterDetection.instance().findBestLocation(); if (bashLocation == null) { throw new ExecutionException("Could not locate the bash executable"); } GeneralCommandLine commandLine = new GeneralCommandLine().withWorkDirectory(getWorkingDir()); commandLine.setExePath(bashLocation); return commandLine.createProcess(); }
public static Boolean isValidGdbPath(String path) { try { GeneralCommandLine command = new GeneralCommandLine(); command.setExePath(path); command.addParameter("--version"); ProcessOutput output = new CapturingProcessHandler( command.createProcess(), Charset.defaultCharset(), command.getCommandLineString()).runProcess(); if (output.getExitCode() != 0) { LOG.error("gdb exited with invalid exit code: " + output.getExitCode()); return false; } // TODO maybe we should warn the user that his GDB version is not the latest at time of writing (7.6.2) return output.getStdout().contains("GDB"); } catch (Exception e) { // LOG.error("Exception while executing the process:", e); return false; } } }
@NotNull @Override public Process createProcess() throws ExecutionException { if (!myUsePty && ApplicationManager.getApplication().isDispatchThread()) { throw new RuntimeException("Non-console executions should not be performed on EDT"); } LOG.info("Executing: " + getCommandLineString()); LOG.info(" environment: " + getEnvironment() + " (+" + getParentEnvironmentType() + ")"); LOG.info(" host = " + getEffectiveHostData() + "; vm = " + getEffectiveVersionManagerData() + "; pty = " + isUsePty() + "; charset: " + getCharset()); return myUsePty ? new PtyCommandLine(this) .withConsoleMode(true) .withInitialColumns(256) .withEnvironment("TERM", "xterm-256color") .createProcess() : super.createProcess(); } }
@NotNull @Override protected ProcessHandler startProcess() throws ExecutionException { try { final GeneralCommandLine cmd = getDmdCommandLine(config); final OSProcessHandler handler = new OSProcessHandler(cmd.createProcess(), cmd.getCommandLineString()); handler.addProcessListener(this); return handler; } catch (final NoValidDlangSdkFound e) { throw new ExecutionException("No valid DMD SDK found!"); } catch (final NoSourcesException e) { throw new ExecutionException("No D Language source files found in directory: " + e.getSourcesRoot()); } catch (final ModuleNotFoundException e) { throw new ExecutionException("Run configuration has no module selected."); } catch (final ExecutionException e) { final String message = e.getMessage(); final boolean isEmpty = message.equals("Executable is not specified"); final boolean notCorrect = message.startsWith("Cannot run program"); if (isEmpty || notCorrect) { Notifications.Bus.notify( new Notification("DMD run configuration", "DMD settings", "DMD executable path is " + (isEmpty ? "empty" : "not specified correctly") + "<br/><a href='configure'>Configure</a>", NotificationType.ERROR), config.getProject()); } throw e; } }
final OSProcessHandler process = new OSProcessHandler(cmd.createProcess(), cmd.getCommandLineString()); final DubInitListener listener = new DubInitListener(); process.addProcessListener(listener);
@NotNull private String processFile(final PsiFile file, final String dubPath) { final GeneralCommandLine cmd = new GeneralCommandLine() .withWorkDirectory(file.getProject().getBasePath()) .withExePath(dubPath) .withEnvironment("DFLAGS", "-o-") .withParameters("build", "--combined", "-q"); try { final String dubCommand = cmd.getCommandLineString(); final DubProcessListener listener = new DubProcessListener(); final OSProcessHandler process = new OSProcessHandler(cmd.createProcess(), dubCommand); process.addProcessListener(listener); process.startNotify(); process.waitFor(); return listener.getOutput(); } catch (final ExecutionException e) { LOG.warn("There was a problem running 'dub build --combined -q'", e); } return ""; }