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

The following examples show how to use com.intellij.execution.configurations.GeneralCommandLine#setWorkDirectory() . 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: HLint.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
/**
 * Runs hlintProg with parameters if hlintProg can be executed.
 */
@NotNull
public static Either<ExecError, String> runHlint(HaskellToolsConsole.Curried toolConsole,
                                                  @NotNull String workingDirectory,
                                                  @NotNull String hlintProg,
                                                  @NotNull String hlintFlags,
                                                  @Nullable String fileContents,
                                                  @NotNull String... params) {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(workingDirectory);
    commandLine.setExePath(hlintProg);
    ParametersList parametersList = commandLine.getParametersList();
    // Required so that hlint won't report a non-zero exit status for lint issues.
    // Otherwise, ExecUtil.readCommandLine will return an error.
    parametersList.add("--no-exit-code");
    parametersList.addParametersString(hlintFlags);
    parametersList.addAll(params);
    toolConsole.writeInput("Using working directory: " + workingDirectory);
    toolConsole.writeInput(commandLine.getCommandLineString());
    return ExecUtil.readCommandLine(commandLine, fileContents);
}
 
Example 2
Source File: RedisConsoleRunner.java    From nosql4idea with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
protected Process createProcess() throws ExecutionException {

    NoSqlConfiguration noSqlConfiguration = NoSqlConfiguration.getInstance(getProject());
    String shellPath = noSqlConfiguration.getShellPath(DatabaseVendor.REDIS);
    final GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(shellPath);

    commandLine.addParameter("-n");
    commandLine.addParameter(database.getName());

    String shellWorkingDir = serverConfiguration.getShellWorkingDir();
    if (StringUtils.isNotBlank(shellWorkingDir)) {
        commandLine.setWorkDirectory(shellWorkingDir);
    }

    String shellArgumentsLine = serverConfiguration.getShellArgumentsLine();
    if (StringUtils.isNotBlank(shellArgumentsLine)) {
        commandLine.addParameters(shellArgumentsLine.split(" "));
    }

    return commandLine.createProcess();
}
 
Example 3
Source File: GhcModi.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
private void spawnProcess() throws GhcModiError {
    GeneralCommandLine commandLine = new GeneralCommandLine(
        GhcModUtil.changedPathIfStack(module.getProject(), path)
    );
    GhcModUtil.updateEnvironment(module.getProject(), commandLine.getEnvironment());
    ParametersList parametersList = commandLine.getParametersList();
    parametersList.addParametersString(
        GhcModUtil.changedFlagsIfStack(module.getProject(), path, flags)
    );
    // setWorkDirectory is deprecated but is needed to work with IntelliJ 13 which does not have withWorkDirectory.
    commandLine.setWorkDirectory(workingDirectory);
    // Make sure we can actually see the errors.
    commandLine.setRedirectErrorStream(true);
    writeInputToConsole("Using working directory: " + workingDirectory);
    writeInputToConsole("Starting ghc-modi process: " + commandLine.getCommandLineString());
    try {
        process = commandLine.createProcess();
    } catch (ExecutionException e) {
        writeErrorToConsole("Failed to initialize process");
        throw new InitError(e.toString());
    }
    input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    output = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    startIdleKillerThread();
}
 
Example 4
Source File: FlutterCommand.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates the command line to run.
 * <p>
 * If a project is supplied, it will be used to determine the ANDROID_HOME variable for the subprocess.
 */
@NotNull
public GeneralCommandLine createGeneralCommandLine(@Nullable Project project) {
  final GeneralCommandLine line = new GeneralCommandLine();
  line.setCharset(CharsetToolkit.UTF8_CHARSET);
  line.withEnvironment(FlutterSdkUtil.FLUTTER_HOST_ENV, FlutterSdkUtil.getFlutterHostEnvValue());
  final String androidHome = IntelliJAndroidSdk.chooseAndroidHome(project, false);
  if (androidHome != null) {
    line.withEnvironment("ANDROID_HOME", androidHome);
  }
  line.setExePath(FileUtil.toSystemDependentName(sdk.getHomePath() + "/bin/" + FlutterSdkUtil.flutterScriptName()));
  if (workDir != null) {
    line.setWorkDirectory(workDir.getPath());
  }
  if (!isDoctorCommand()) {
    line.addParameter("--no-color");
  }
  line.addParameters(type.subCommand);
  line.addParameters(args);
  return line;
}
 
Example 5
Source File: NodeRunner.java    From vue-for-idea with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param cwd working directory
 * @param node node interpreter path
 * @param exe node executable to run
 * @return command line to sdk
 */
@NotNull
public static GeneralCommandLine createCommandLine(@NotNull String cwd, String node, String exe) {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    if (!new File(cwd).exists()) {
        throw new IllegalArgumentException("cwd: "+cwd +" doesn't exist");
    }
    if (!new File(exe).exists()) {
        throw new IllegalArgumentException("exe: "+exe +" doesn't exist");

    }
    commandLine.setWorkDirectory(cwd);
    if (SystemInfo.isWindows) {
        commandLine.setExePath(exe);
    } else {
        if (!new File(node).exists()) {
            throw new IllegalArgumentException("path doesn't exist");
        }
        commandLine.setExePath(node);
        commandLine.addParameter(exe);
    }
    return commandLine;
}
 
Example 6
Source File: JavaCommandBuilder.java    From intellij with Apache License 2.0 6 votes vote down vote up
GeneralCommandLine build() {
  checkArgument(javaBinary != null, "javaBinary was not set");
  checkArgument(mainClass != null, "mainClass was not set");
  GeneralCommandLine commandLine = new GeneralCommandLine(javaBinary.getPath());
  commandLine.addParameters("-cp", classpaths.stream().map(File::getPath).collect(joining(":")));
  commandLine.addParameters(jvmArgs);
  commandLine.addParameters(
      systemProperties.entrySet().stream()
          .map(e -> "-D" + e.getKey() + '=' + e.getValue())
          .collect(toList()));
  commandLine.addParameter(mainClass);
  commandLine.addParameters(programArgs);

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

  commandLine.withParentEnvironmentType(ParentEnvironmentType.NONE);
  environmentVariables.forEach(commandLine::withEnvironment);

  return commandLine;
}
 
Example 7
Source File: SrcToolExe.java    From teamcity-symbol-server with Apache License 2.0 6 votes vote down vote up
public ExecResult dumpSources(final File pdbFile, final BuildProgressLogger buildLogger){
  final GeneralCommandLine commandLine = new GeneralCommandLine();
  commandLine.setWorkDirectory(myPath.getParent());
  commandLine.setExePath(myPath.getPath());
  commandLine.addParameter(pdbFile.getAbsolutePath());
  commandLine.addParameter(DUMP_REFERENCES_SWITCH);
  commandLine.addParameter(ZERRO_ON_SUCCESS_SWITCH);

  buildLogger.message(String.format("Running command %s", commandLine.getCommandLineString()));

  final ExecResult execResult = SimpleCommandLineProcessRunner.runCommand(commandLine, null);
  if (execResult.getExitCode() != 0) {
    buildLogger.warning(String.format("%s completed with exit code %s.", SRCTOOL_EXE, execResult));
    buildLogger.warning("Stdout: " + execResult.getStdout());
    buildLogger.warning("Stderr: " + execResult.getStderr());
    final Throwable exception = execResult.getException();
    if(exception != null){
      buildLogger.exception(exception);
    }
  }
  return execResult;
}
 
Example 8
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 9
Source File: GhcMod.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String exec(@NotNull Project project, @NotNull String workingDirectory, @NotNull String ghcModPath,
                          @NotNull String command, @NotNull String ghcModFlags, String... params) {
    if (!validateGhcVersion(project, ghcModPath, ghcModFlags)) return null;
    GeneralCommandLine commandLine = new GeneralCommandLine(ghcModPath);
    GhcModUtil.updateEnvironment(project, commandLine.getEnvironment());
    ParametersList parametersList = commandLine.getParametersList();
    parametersList.addParametersString(ghcModFlags);
    parametersList.add(command);
    parametersList.addAll(params);
    // setWorkDirectory is deprecated but is needed to work with IntelliJ 13 which does not have withWorkDirectory.
    commandLine.setWorkDirectory(workingDirectory);
    // Make sure we can actually see the errors.
    commandLine.setRedirectErrorStream(true);
    HaskellToolsConsole toolConsole = HaskellToolsConsole.get(project);
    toolConsole.writeInput(ToolKey.GHC_MOD_KEY, "Using working directory: " + workingDirectory);
    toolConsole.writeInput(ToolKey.GHC_MOD_KEY, commandLine.getCommandLineString());
    Either<ExecUtil.ExecError, String> result = ExecUtil.readCommandLine(commandLine);
    if (result.isLeft()) {
        //noinspection ThrowableResultOfMethodCallIgnored
        ExecUtil.ExecError e = EitherUtil.unsafeGetLeft(result);
        toolConsole.writeError(ToolKey.GHC_MOD_KEY, e.getMessage());
        NotificationUtil.displayToolsNotification(
            NotificationType.ERROR, project, "ghc-mod", e.getMessage()
        );
        return null;
    }
    String out = EitherUtil.unsafeGetRight(result);
    toolConsole.writeOutput(ToolKey.GHC_MOD_KEY, out);
    return out;
}
 
Example 10
Source File: Tool.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public GeneralCommandLine createCommandLine(DataContext dataContext) {
  if (StringUtil.isEmpty(getWorkingDirectory())) {
    setWorkingDirectory(dataContext.getData(CommonDataKeys.PROJECT).getBasePath());
  }

  GeneralCommandLine commandLine = new GeneralCommandLine();
  try {
    String paramString = MacroManager.getInstance().expandMacrosInString(getParameters(), true, dataContext);
    String workingDir = MacroManager.getInstance().expandMacrosInString(getWorkingDirectory(), true, dataContext);
    String exePath = MacroManager.getInstance().expandMacrosInString(getProgram(), true, dataContext);

    commandLine.getParametersList().addParametersString(
      MacroManager.getInstance().expandMacrosInString(paramString, false, dataContext));
    final String workDirExpanded = MacroManager.getInstance().expandMacrosInString(workingDir, false, dataContext);
    if (!StringUtil.isEmpty(workDirExpanded)) {
      commandLine.setWorkDirectory(workDirExpanded);
    }
    exePath = MacroManager.getInstance().expandMacrosInString(exePath, false, dataContext);
    if (exePath == null) return null;

    File exeFile = new File(exePath);
    if (exeFile.isDirectory() && exeFile.getName().endsWith(".app")) {
      commandLine.setExePath("open");
      commandLine.getParametersList().prependAll("-a", exePath);
    }
    else {
      commandLine.setExePath(exePath);
    }
  }
  catch (Macro.ExecutionCancelledException e) {
    return null;
  }
  return commandLine;
}
 
Example 11
Source File: BlazeCLionGDBDriverConfiguration.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public GeneralCommandLine createDriverCommandLine(
    DebuggerDriver driver, ArchitectureType architectureType) throws ExecutionException {
  GeneralCommandLine cl = super.createDriverCommandLine(driver, architectureType);
  cl.setWorkDirectory(workspaceRootDirectory);
  return cl;
}
 
Example 12
Source File: ExecUtil.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@NotNull
public static Either<ExecError, String> readCommandLine(@Nullable String workingDirectory, @NotNull String command, @NotNull String[] params, @Nullable String input) {
    GeneralCommandLine commandLine = new GeneralCommandLine(command);
    if (workingDirectory != null) {
        commandLine.setWorkDirectory(workingDirectory);
    }
    commandLine.addParameters(params);
    return readCommandLine(commandLine, input);
}
 
Example 13
Source File: RNConsoleImpl.java    From react-native-console with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Execute some shell with giving working directory.
 *
 * @param shell
 */
public void executeShell(String shell, String workDirectory) {
    GeneralCommandLine commandLine =RNPathUtil.createFullPathCommandLine(shell, workDirectory);
    commandLine.setWorkDirectory(workDirectory);
    myGeneralCommandLine = commandLine;
    try {
        processCommandline(commandLine);
    } catch (ExecutionException e) {
        NotificationUtils.showNotification("Unable to run the commandline:" + e.getMessage(),
                NotificationType.WARNING);
    }
}
 
Example 14
Source File: Runner.java    From phpstorm-plugin with MIT License 5 votes vote down vote up
protected OSProcessHandler prepareProcessHandler(@NotNull String exePath, @Nullable String workingDirectory, String[] parameters, @Nullable Map<String, String> environment) throws ExecutionException {
    GeneralCommandLine commandLine = new GeneralCommandLine(exePath);
    commandLine.addParameters(parameters);
    if (workingDirectory != null) {
        commandLine.setWorkDirectory(workingDirectory);
    }

    commandLine.withEnvironment(environment);

    return new ColoredProcessHandler(commandLine);
}
 
Example 15
Source File: FreelineUtil.java    From freeline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void build(Project project) {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(project.getBasePath());
    commandLine.setExePath("python");
    commandLine.addParameter("freeline.py");
    // debug
    commandLine.addParameter("-d");

    // commands process
    try {
        processCommandline(project, commandLine);
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: HaskellApplicationCommandLineState.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
protected OSProcessHandler startProcess() throws ExecutionException {
    ExecutionEnvironment env = getEnvironment();
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(env.getProject().getBasePath());
    // TODO: This should probably be a bit more generic than relying on `cabal run`.
    final HaskellBuildSettings buildSettings = HaskellBuildSettings.getInstance(myConfig.getProject());
    commandLine.setExePath(buildSettings.getCabalPath());
    ParametersList parametersList = commandLine.getParametersList();
    parametersList.add("run");
    parametersList.add("--with-ghc=" + buildSettings.getGhcPath());
    parametersList.addParametersString(myConfig.programArguments);
    return new OSProcessHandler(commandLine);
}
 
Example 17
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 18
Source File: HaskellTestCommandLineState.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
protected OSProcessHandler startProcess() throws ExecutionException {
    ExecutionEnvironment env = getEnvironment();
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(env.getProject().getBasePath());
    // TODO: This should probably be a bit more generic than relying on `cabal test`.
    final String cabalPath = HaskellBuildSettings.getInstance(myConfig.getProject()).getCabalPath();
    commandLine.setExePath(cabalPath);
    ParametersList parametersList = commandLine.getParametersList();
    parametersList.add("test");
    parametersList.addParametersString(myConfig.programArguments);
    return new OSProcessHandler(commandLine);
}
 
Example 19
Source File: RNConsoleImpl.java    From react-native-console with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Execute raw commands without any path or param modify.
 *
 * @param shell
 */
public void executeRawShell(String workDirectory, String[] shell) {
    GeneralCommandLine commandLine =new GeneralCommandLine(shell);
    commandLine.setWorkDirectory(workDirectory);
    myGeneralCommandLine = commandLine;
    try {
        processCommandline(commandLine);
    } catch (ExecutionException e) {
        NotificationUtils.showNotification("Unable to run the commandline:" + e.getMessage(),
                NotificationType.WARNING);
    }
}
 
Example 20
Source File: HaxeSdkUtil.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Nullable
public static HaxeSdkData testHaxeSdk(String path) {
  final String exePath = getCompilerPathByFolderPath(path);

  if (exePath == null) {
    return null;
  }

  final GeneralCommandLine command = new GeneralCommandLine();
  command.setExePath(exePath);
  command.addParameter("-help");
  command.setWorkDirectory(path);

  try {
    final ProcessOutput output = new CapturingProcessHandler(
      command.createProcess(),
      Charset.defaultCharset(),
      command.getCommandLineString()).runProcess();

    if (output.getExitCode() != 0) {
      LOG.error("Haxe compiler exited with invalid exit code: " + output.getExitCode());
      return null;
    }

    final String outputString = output.getStderr();

    String haxeVersion = "NA";
    final Matcher matcher = VERSION_MATCHER.matcher(outputString);
    if (matcher.find()) {
      haxeVersion = matcher.group(1);
    }
    final HaxeSdkData haxeSdkData = new HaxeSdkData(path, haxeVersion);
    haxeSdkData.setHaxelibPath(getHaxelibPathByFolderPath(path));
    haxeSdkData.setNekoBinPath(suggestNekoBinPath(path));
    return haxeSdkData;
  }
  catch (ExecutionException e) {
    LOG.info("Exception while executing the process:", e);
    return null;
  }
}