Java Code Examples for com.intellij.execution.configurations.GeneralCommandLine#getCommandLineString()

The following examples show how to use com.intellij.execution.configurations.GeneralCommandLine#getCommandLineString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PantsCompileOptionsExecutor.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
private String loadProjectStructureFromTargets(
  @NotNull Consumer<String> statusConsumer,
  @Nullable ProcessAdapter processAdapter
) throws IOException, ExecutionException {
  final File outputFile = FileUtil.createTempFile("pants_depmap_run", ".out");
  final GeneralCommandLine command = getPantsExportCommand(outputFile, statusConsumer);
  statusConsumer.consume("Resolving dependencies...");
  PantsMetrics.markExportStart();
  final ProcessOutput processOutput = getProcessOutput(command);
  PantsMetrics.markExportEnd();
  if (processOutput.getStdout().contains("no such option")) {
    throw new ExternalSystemException("Pants doesn't have necessary APIs. Please upgrade your pants!");
  }
  if (processOutput.checkSuccess(LOG)) {
    return FileUtil.loadFile(outputFile);
  }
  else {
    throw new PantsExecutionException("Failed to update the project!", command.getCommandLineString("pants"), processOutput);
  }
}
 
Example 2
Source File: OSSPantsJavaExamplesIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
private String[] getModulesNamesFromPantsDependencies(String targetName) throws ProjectBuildException {
  Optional<VirtualFile> pantsExe = PantsUtil.findPantsExecutable(myProject);
  assertTrue(pantsExe.isPresent());
  final GeneralCommandLine commandLine = PantsUtil.defaultCommandLine(pantsExe.get().getPath());
  commandLine.addParameters(PantsConstants.PANTS_CLI_OPTION_NO_COLORS);
  commandLine.addParameters("dependencies");
  commandLine.addParameters(targetName);
  final Process process;
  try {
    process = commandLine.createProcess();
  }
  catch (ExecutionException e) {
    throw new ProjectBuildException(e);
  }

  final CapturingProcessHandler processHandler = new CapturingAnsiEscapesAwareProcessHandler(process, commandLine.getCommandLineString());
  ProcessOutput output = processHandler.runProcess();
  String lines[] = output.getStdout().split("\\r?\\n");
  Set<String> modules = new HashSet<>();
  for (String l : lines) {
    modules.add(PantsUtil.getCanonicalModuleName(l));
  }
  return modules.toArray(new String[modules.size()]);
}
 
Example 3
Source File: SimpleExportResult.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
public static SimpleExportResult getExportResult(@NotNull String pantsExecutable) {
  File pantsExecutableFile = new File(pantsExecutable);
  SimpleExportResult cache = simpleExportCache.get(pantsExecutableFile);
  if (cache != null) {
    return cache;
  }
  final GeneralCommandLine commandline = PantsUtil.defaultCommandLine(pantsExecutable);
  commandline.addParameters("--no-quiet", "export", PantsConstants.PANTS_CLI_OPTION_NO_COLORS);
  try (TempFile tempFile = TempFile.create("pants_export_run", ".out")) {
    commandline.addParameter(
      String.format("%s=%s", PantsConstants.PANTS_CLI_OPTION_EXPORT_OUTPUT_FILE,
                    tempFile.getFile().getPath()));
    final ProcessOutput processOutput = PantsUtil.getCmdOutput(commandline, null);
    if (processOutput.checkSuccess(LOG)) {
      SimpleExportResult result = parse(FileUtil.loadFile(tempFile.getFile()));
      simpleExportCache.put(pantsExecutableFile, result);
      return result;
    }
  }
  catch (IOException | ExecutionException e) {
    // Fall-through to handle outside the block.
  }
  throw new PantsException("Failed:" + commandline.getCommandLineString());
}
 
Example 4
Source File: ExecUtil.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to get the absolute path for a command in the PATH.
 */
@Nullable
public static String locateExecutable(@NotNull final String exePath) {
    GeneralCommandLine cmdLine = new GeneralCommandLine(
        SystemInfo.isWindows ? "where" : "which"
    );
    cmdLine.addParameter(exePath);
    final ProcessOutput processOutput;
    try {
        processOutput = new CapturingProcessHandler(cmdLine).runProcess();
    } catch (ExecutionException e) {
        throw new RuntimeException(
            "Failed to execute command: " + cmdLine.getCommandLineString(),
            e
        );
    }
    final String stdout = processOutput.getStdout();
    final String[] lines = stdout.trim().split("\n");
    if (lines.length == 0) return null;
    return lines[0].trim();
}
 
Example 5
Source File: BuckCommandHandler.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
public MyOSProcessHandler(
    Process process,
    GeneralCommandLine commandLine,
    Charset charset) {
  super(process, commandLine.getCommandLineString());
  myCharset = charset;
}
 
Example 6
Source File: ScriptRunnerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static OSProcessHandler execute(@Nonnull String exePath,
                                       @Nullable String workingDirectory,
                                       @Nullable VirtualFile scriptFile,
                                       String[] parameters) throws ExecutionException {
  GeneralCommandLine commandLine = new GeneralCommandLine();
  commandLine.setExePath(exePath);
  commandLine.setPassParentEnvironment(true);
  if (scriptFile != null) {
    commandLine.addParameter(scriptFile.getPresentableUrl());
  }
  commandLine.addParameters(parameters);

  if (workingDirectory != null) {
    commandLine.setWorkDirectory(workingDirectory);
  }

  LOG.debug("Command line: " + commandLine.getCommandLineString());
  LOG.debug("Command line env: " + commandLine.getEnvironment());

  final OSProcessHandler processHandler = new ColoredProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString(),
                                                                    EncodingManager.getInstance().getDefaultCharset());
  if (LOG.isDebugEnabled()) {
    processHandler.addProcessListener(new ProcessAdapter() {
      @Override
      public void onTextAvailable(ProcessEvent event, Key outputType) {
        LOG.debug(outputType + ": " + event.getText());
      }
    });
  }

  //ProcessTerminatedListener.attach(processHandler, project);
  return processHandler;
}
 
Example 7
Source File: OSProcessHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Deprecated
@DeprecationInfo("Use com.intellij.execution.process.ProcessHandlerFactory")
public OSProcessHandler(@Nonnull GeneralCommandLine commandLine) throws ExecutionException {
  this(startProcess(commandLine), commandLine.getCommandLineString(), commandLine.getCharset());
  myHasErrorStream = !commandLine.isRedirectErrorStream();
  myFilesToDelete = commandLine.getUserData(DELETE_FILES_ON_TERMINATION);
}
 
Example 8
Source File: BuckWSServerPortUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Returns the port number of Buck's HTTP server, if it can be determined. */
public static int getPort(Project project, String path)
    throws NumberFormatException, IOException, ExecutionException {
  String exec = BuckExecutableSettingsProvider.getInstance(project).resolveBuckExecutable();

  if (Strings.isNullOrEmpty(exec)) {
    throw new RuntimeException("Buck executable is not defined in settings.");
  }

  GeneralCommandLine commandLine = new GeneralCommandLine();
  commandLine.setExePath(exec);
  commandLine.withWorkDirectory(path);
  commandLine.withEnvironment(EnvironmentUtil.getEnvironmentMap());
  commandLine.addParameter("server");
  commandLine.addParameter("status");
  commandLine.addParameter("--reuse-current-config");
  commandLine.addParameter("--http-port");
  commandLine.setRedirectErrorStream(true);

  Process p = commandLine.createProcess();
  BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

  String line;
  while ((line = reader.readLine()) != null) {
    if (line.startsWith(SEARCH_FOR)) {
      return Integer.parseInt(line.substring(SEARCH_FOR.length()));
    }
  }
  throw new RuntimeException(
      "Configured buck executable did not report a valid port string,"
          + " ensure "
          + commandLine.getCommandLineString()
          + " can be run from "
          + path);
}
 
Example 9
Source File: HaxeTestsRunner.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected RunContentDescriptor doExecute(@NotNull final Project project,
                                         @NotNull RunProfileState state,
                                         final RunContentDescriptor descriptor,
                                         @NotNull final ExecutionEnvironment environment) throws ExecutionException {

  final HaxeTestsConfiguration profile = (HaxeTestsConfiguration)environment.getRunProfile();
  final Module module = profile.getConfigurationModule().getModule();

  ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  VirtualFile[] roots = rootManager.getContentRoots();

  return super.doExecute(project, new CommandLineState(environment) {
    @NotNull
    @Override
    protected ProcessHandler startProcess() throws ExecutionException {
      //actually only neko target is supported for tests
      HaxeTarget currentTarget = HaxeTarget.NEKO;
      final GeneralCommandLine commandLine = new GeneralCommandLine();
      commandLine.setWorkDirectory(PathUtil.getParentPath(module.getModuleFilePath()));
      commandLine.setExePath(currentTarget.getFlag());
      final HaxeModuleSettings settings = HaxeModuleSettings.getInstance(module);
      String folder = settings.getOutputFolder() != null ? (settings.getOutputFolder() + "/release/") : "";
      commandLine.addParameter(getFileNameWithCurrentExtension(currentTarget, folder + settings.getOutputFileName()));

      final TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(module.getProject());
      consoleBuilder.addFilter(new ErrorFilter(module));
      setConsoleBuilder(consoleBuilder);

      return new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString());
    }

    private String getFileNameWithCurrentExtension(HaxeTarget haxeTarget, String fileName) {
      if (haxeTarget != null) {
        return haxeTarget.getTargetFileNameWithExtension(FileUtil.getNameWithoutExtension(fileName));
      }
      return fileName;
    }
  }, descriptor, environment);
}
 
Example 10
Source File: GaugeRunProcessHandler.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
public static GaugeRunProcessHandler runCommandLine(final GeneralCommandLine commandLine, GaugeDebugInfo debugInfo, Project project) throws ExecutionException {
    LOG.info(String.format("Running Gauge tests with command : %s", commandLine.getCommandLineString()));
    final GaugeRunProcessHandler gaugeRunProcess = new GaugeRunProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString());
    ProcessTerminatedListener.attach(gaugeRunProcess);
    if (debugInfo.shouldDebug()) {
        launchDebugger(project, debugInfo);
    }
    return gaugeRunProcess;
}
 
Example 11
Source File: PantsUtil.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public static ProcessOutput getCmdOutput(
  @NotNull GeneralCommandLine command,
  @Nullable ProcessAdapter processAdapter
) throws ExecutionException {
  final CapturingProcessHandler processHandler =
    new CapturingProcessHandler(command.createProcess(), Charset.defaultCharset(), command.getCommandLineString());
  if (processAdapter != null) {
    processHandler.addProcessListener(processAdapter);
  }
  return processHandler.runProcess();
}
 
Example 12
Source File: PantsOptions.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static PantsOptions execPantsOptions(@NotNull String pantsExecutable) {
  GeneralCommandLine exportCommandline = PantsUtil.defaultCommandLine(pantsExecutable);
  exportCommandline.addParameters("options", PantsConstants.PANTS_CLI_OPTION_NO_COLORS);
  try {
    ProcessOutput processOutput = PantsUtil.getCmdOutput(exportCommandline, null);
    return new PantsOptions(processOutput.getStdout());
  }
  catch (ExecutionException e) {
    throw new PantsException("Failed:" + exportCommandline.getCommandLineString());
  }
}
 
Example 13
Source File: JscsRunner.java    From jscs-plugin with MIT License 5 votes vote down vote up
@NotNull
private static ProcessOutput execute(@NotNull GeneralCommandLine commandLine, int timeoutInMilliseconds) throws ExecutionException {
    LOG.info("Running jscs command: " + commandLine.getCommandLineString());
    Process process = commandLine.createProcess();
    OSProcessHandler processHandler = new ColoredProcessHandler(process, commandLine.getCommandLineString(), Charsets.UTF_8);
    final ProcessOutput output = new ProcessOutput();
    processHandler.addProcessListener(new ProcessAdapter() {
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            if (outputType.equals(ProcessOutputTypes.STDERR)) {
                output.appendStderr(event.getText());
            } else if (!outputType.equals(ProcessOutputTypes.SYSTEM)) {
                output.appendStdout(event.getText());
            }
        }
    });
    processHandler.startNotify();
    if (processHandler.waitFor(timeoutInMilliseconds)) {
        output.setExitCode(process.exitValue());
    } else {
        processHandler.destroyProcess();
        output.setTimeout();
    }
    if (output.isTimeout()) {
        throw new ExecutionException("Command '" + commandLine.getCommandLineString() + "' is timed out.");
    }
    return output;
}
 
Example 14
Source File: NodeRunner.java    From vue-for-idea with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param commandLine command line to sdk
 * @param timeoutInMilliseconds timeout
 * @return process output
 * @throws ExecutionException
 */
@NotNull
public static ProcessOutput execute(@NotNull GeneralCommandLine commandLine,ProcessListener listener, int timeoutInMilliseconds) throws ExecutionException {
    LOG.info("Running node command: " + commandLine.getCommandLineString());
    Process process = commandLine.createProcess();
    OSProcessHandler processHandler = new ColoredProcessHandler(process, commandLine.getCommandLineString(), Charsets.UTF_8);
    final ProcessOutput output = new ProcessOutput();
    processHandler.addProcessListener(new ProcessAdapter() {
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            if (outputType.equals(ProcessOutputTypes.STDERR)) {
                output.appendStderr(event.getText());
                if(listener!=null){
                    listener.onError(processHandler,event.getText());
                }
            } else if (!outputType.equals(ProcessOutputTypes.SYSTEM)) {
                output.appendStdout(event.getText());
                if(listener!=null){
                    listener.onOutput(processHandler,event.getText());
                }
            }else if(outputType.equals(ProcessOutputTypes.SYSTEM)){
                if(listener!=null){
                    listener.onCommand(processHandler,event.getText());
                }
            }
        }
    });
    processHandler.startNotify();
    if (processHandler.waitFor(timeoutInMilliseconds)) {
        output.setExitCode(process.exitValue());
    } else {
        processHandler.destroyProcess();
        output.setTimeout();
    }
    if (output.isTimeout()) {
        throw new ExecutionException("Command '" + commandLine.getCommandLineString() + "' is timed out.");
    }
    return output;
}
 
Example 15
Source File: BuckCommandHandler.java    From buck with Apache License 2.0 4 votes vote down vote up
public OSProcessHandler createProcess(GeneralCommandLine commandLine) throws ExecutionException {
  // TODO(t7984081): Use ProcessExecutor to start buck process.
  Process process = commandLine.createProcess();
  return new OSProcessHandler(process, commandLine.getCommandLineString(), charset);
}
 
Example 16
Source File: OCamlApplicationRunningState.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
protected ProcessHandler startProcess() throws ExecutionException {
    GeneralCommandLine commandLine = getCommand();
    return new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString());
}
 
Example 17
Source File: RunnerMediator.java    From consulo with Apache License 2.0 4 votes vote down vote up
/** @deprecated use CustomDestroyProcessHandler(GeneralCommandLine commandLine) (to remove in IDEA 16) */
public CustomDestroyProcessHandler(@Nonnull Process process, @Nonnull GeneralCommandLine commandLine) {
  super(process, commandLine.getCommandLineString());
  mySoftKill = false;
}
 
Example 18
Source File: RunnerMediator.java    From consulo with Apache License 2.0 4 votes vote down vote up
/** @deprecated use CustomDestroyProcessHandler(GeneralCommandLine commandLine, boolean softKill) (to remove in IDEA 16) */
public CustomDestroyProcessHandler(@Nonnull Process process, @Nonnull GeneralCommandLine commandLine, boolean softKill) {
  super(process, commandLine.getCommandLineString());
  mySoftKill = softKill;
}