/**
  * Wait for process termination
  *
  * @param timeout
  */
 public boolean waitFor(int timeout) {
   checkStarted();
   final OSProcessHandler handler;
   synchronized (myLock) {
     // TODO: This line seems to cause situation when exitCode is not set before
     // SvnLineCommand.runCommand() is finished.
     // TODO: Carefully analyze behavior (on all operating systems) and fix.
     if (myIsDestroyed) return true;
     handler = myHandler;
   }
   if (timeout == -1) {
     return handler.waitFor();
   } else {
     return handler.waitFor(timeout);
   }
 }
  public void launch(final MPSMakeCallback callback) {
    if (!isValid()) {
      throw new IllegalStateException("Unable to launch without validation");
    }

    GeneralCommandLine gcl = new GeneralCommandLine(myCommandLine);
    gcl.setWorkDirectory(myProject.getBaseDir().getPath());
    final TextEventProcessor tep =
        new TextEventProcessor(myProject, "MPS") {
          @Override
          public void reportWrittenFile(String file) {
            LOG.debug("written file: " + file);
            callback.fileWritten(file);
          }

          @Override
          public void reportDeletedFile(String file) {
            LOG.debug("deleted file: " + file);
            callback.fileDeleted(file);
          }

          @Override
          public void error(String text) {
            LOG.debug("error: " + text);
            callback.error(text);
          }

          @Override
          public void info(String text) {
            LOG.info("info: " + text);
            callback.info(text);
          }
        };
    try {
      OSProcessHandler processHandler =
          new OSProcessHandler(gcl.createProcess(), myCommandLine.get(0));
      processHandler.addProcessListener(
          new ProcessAdapter() {
            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
              if (outputType == ProcessOutputTypes.STDERR) {
                tep.processStderr(event.getText());
              } else if (outputType == ProcessOutputTypes.STDOUT) {
                tep.processStdout(event.getText());
              }
            }
          });
      processHandler.startNotify();

      processHandler.waitFor();
      if (processHandler.getProcess().exitValue() != 0) {
        callback.error("External process returned non-zero");
      }

    } catch (ExecutionException e) {
      LOG.debug(e);
      callback.error("Error running process: " + e.getMessage());
    }
  }
Exemple #3
0
  private static void runOutOfProcess(
      CompileContext compileContext, VirtualFile outputDir, File kotlinHome, File scriptFile) {
    final SimpleJavaParameters params = new SimpleJavaParameters();
    params.setJdk(new SimpleJavaSdkType().createJdk("tmp", SystemProperties.getJavaHome()));
    params.setMainClass("org.jetbrains.jet.cli.KotlinCompiler");
    params.getProgramParametersList().add("-module", scriptFile.getAbsolutePath());
    params.getProgramParametersList().add("-output", path(outputDir));
    params.getProgramParametersList().add("-tags");

    for (File jar : kompilerClasspath(kotlinHome, compileContext)) {
      params.getClassPath().add(jar);
    }

    params.getVMParametersList().addParametersString("-Djava.awt.headless=true -Xmx512m");
    //        params.getVMParametersList().addParametersString("-agentlib:yjpagent=sampling");

    Sdk sdk = params.getJdk();

    final GeneralCommandLine commandLine =
        JdkUtil.setupJVMCommandLine(
            ((JavaSdkType) sdk.getSdkType()).getVMExecutablePath(sdk), params, false);

    compileContext.addMessage(
        INFORMATION, "Invoking out-of-process compiler with arguments: " + commandLine, "", -1, -1);

    try {
      final OSProcessHandler processHandler =
          new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString()) {
            @Override
            public Charset getCharset() {
              return commandLine.getCharset();
            }
          };

      ProcessAdapter processListener = createProcessListener(compileContext);
      processHandler.addProcessListener(processListener);

      processHandler.startNotify();
      processHandler.waitFor();
    } catch (Exception e) {
      compileContext.addMessage(ERROR, "[Internal Error] " + e.getLocalizedMessage(), "", -1, -1);
      return;
    }
  }
  private JsonObject parseDubConfiguration() {
    try {
      String baseDir = project.getBaseDir().getCanonicalPath();
      GeneralCommandLine commandLine = new GeneralCommandLine();
      commandLine.setWorkDirectory(new File(baseDir));
      commandLine.setExePath(dubBinaryPath);
      ParametersList parametersList = commandLine.getParametersList();
      parametersList.addParametersString("describe");

      OSProcessHandler process =
          new OSProcessHandler(
              commandLine.createProcess(), commandLine.getPreparedCommandLine(Platform.current()));

      final StringBuilder builder = new StringBuilder();
      process.addProcessListener(
          new ProcessAdapter() {
            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
              builder.append(event.getText());
            }
          });

      process.startNotify();
      process.waitFor();

      // remove the warning line at the top if it exists
      String json =
          builder
              .toString()
              .replaceAll("WARNING.+", "")
              .trim()
              .replaceFirst(commandLine.getPreparedCommandLine(Platform.current()), "");

      // process output of dub describe into jsonObject
      return new JsonParser().parse(json).getAsJsonObject();

    } catch (ExecutionException ex) {
      ex.printStackTrace();
    }
    return null;
  }
  private void shutdownProcess() {
    final OSProcessHandler processHandler = myProcessHandler;
    if (processHandler != null) {
      if (!processHandler.isProcessTerminated()) {
        boolean forceQuite = true;
        try {
          writeLine(EXIT_COMMAND);
          forceQuite = !processHandler.waitFor(500);
          if (forceQuite) {
            LOG.warn("File watcher is still alive. Doing a force quit.");
          }
        } catch (IOException ignore) {
        }
        if (forceQuite) {
          processHandler.destroyProcess();
        }
      }

      myProcessHandler = null;
    }
  }
 @Nullable
 private static String executeZipAlign(String zipAlignPath, File source, File destination) {
   GeneralCommandLine commandLine = new GeneralCommandLine();
   commandLine.setExePath(zipAlignPath);
   commandLine.addParameters("-f", "4", source.getAbsolutePath(), destination.getAbsolutePath());
   OSProcessHandler handler;
   try {
     handler = new OSProcessHandler(commandLine.createProcess(), "");
   } catch (ExecutionException e) {
     return e.getMessage();
   }
   final StringBuilder builder = new StringBuilder();
   handler.addProcessListener(
       new ProcessAdapter() {
         @Override
         public void onTextAvailable(ProcessEvent event, Key outputType) {
           builder.append(event.getText());
         }
       });
   handler.startNotify();
   handler.waitFor();
   int exitCode = handler.getProcess().exitValue();
   return exitCode != 0 ? builder.toString() : null;
 }
  private void executeTest(String className, String testMethodName, String dunitPath) {
    String testPath = className + "." + testMethodName;
    final String workingDirectory = project.getBasePath();
    //            final String testFile = configuration.getDFile().getCanonicalPath();

    final String dubPath = ToolKey.DUB_KEY.getPath(project);
    if (dubPath == null || dubPath.isEmpty()) {
      Notifications.Bus.notify(
          new Notification(
              "Dunit Test Runner",
              "Dub path must be specified",
              "Dub executable path is empty"
                  + "<br/><a href='configureDLanguageTools'>Configure</a>",
              NotificationType.WARNING,
              new DToolsNotificationListener(project)),
          project);
      return;
    }

    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(workingDirectory);
    commandLine.setExePath(dubPath);
    ParametersList parametersList = commandLine.getParametersList();
    parametersList.addParametersString("--");
    parametersList.addParametersString("-v");
    parametersList.addParametersString("--filter");
    parametersList.addParametersString(testPath + "$"); // regex to locate exact test

    final StringBuilder builder = new StringBuilder();
    try {
      OSProcessHandler process = new OSProcessHandler(commandLine.createProcess());
      process.addProcessListener(
          new ProcessAdapter() {
            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
              builder.append(event.getText());
            }
          });

      process.startNotify();
      process.waitFor();

    } catch (ExecutionException e) {
      e.printStackTrace();
    }

    String result = builder.toString();
    // call either finished(success) or failed
    String successPatternString = ".*OK:.*";
    Pattern successPattern = Pattern.compile(successPatternString);
    Matcher successMatcher = successPattern.matcher(result);

    if (successMatcher.find()) {
      testFinished(className, testMethodName, 0);
      testStdOut(className, testMethodName, result);
    } else if (result.contains("FAILURE:")) {
      testFailed(className, testMethodName, 0, "Failed", result);
    } else if (result.contains("SKIP:")) {
      testIgnored(className, testMethodName);
      testStdOut(className, testMethodName, result);
    } else {
      testFailed(className, testMethodName, 0, "Failed for unknown reasons", result);
    }
  }