Esempio n. 1
0
  @NotNull
  public GeneralCommandLine createCommand(
      @NotNull Module module,
      @Nullable String jvmParams,
      boolean forCreation,
      @NotNull MvcCommand command)
      throws ExecutionException {
    final JavaParameters params =
        createJavaParameters(module, forCreation, false, true, jvmParams, command);
    addJavaHome(params, module);

    final GeneralCommandLine commandLine = createCommandLine(params);

    final VirtualFile griffonHome = getSdkRoot(module);
    if (griffonHome != null) {
      commandLine
          .getEnvironment()
          .put(getSdkHomePropertyName(), FileUtil.toSystemDependentName(griffonHome.getPath()));
    }

    final VirtualFile root = findAppRoot(module);
    final File ioRoot =
        root != null ? VfsUtilCore.virtualToIoFile(root) : new File(module.getModuleDirPath());
    commandLine.setWorkDirectory(forCreation ? ioRoot.getParentFile() : ioRoot);

    return commandLine;
  }
  @Nullable
  protected FileCoverageInfo collectBaseFileCoverage(
      @NotNull final VirtualFile file,
      @NotNull final Annotator annotator,
      @NotNull final ProjectData projectData,
      @NotNull final Map<String, String> normalizedFiles2Files) {
    final String filePath = normalizeFilePath(file.getPath());

    // process file
    final FileCoverageInfo info;

    final ClassData classData = getClassData(filePath, projectData, normalizedFiles2Files);
    if (classData != null) {
      // fill info from coverage data
      info = fileInfoForCoveredFile(classData);
    } else {
      // file wasn't mentioned in coverage information
      info = fillInfoForUncoveredFile(VfsUtilCore.virtualToIoFile(file));
    }

    if (info != null) {
      annotator.annotateFile(filePath, info);
    }
    return info;
  }
Esempio n. 3
0
    public void actionPerformed(final ActionEvent e) {
      final FileChooserDescriptor descriptor =
          new FilterFileChooserDescriptor(
              "Select",
              "Select a file to import",
              new FileFilter() {
                @Override
                public boolean accept(final File f) {
                  return f.isDirectory()
                      || "xml".equalsIgnoreCase(FileUtilRt.getExtension(f.getAbsolutePath()));
                }

                @Override
                public String getDescription() {
                  return "*.xml";
                }
              });

      final Component parent = SwingUtilities.getRoot(_importFile);
      final VirtualFile toSelect = LocalFileSystem.getInstance().findFileByPath(_importDir);
      final VirtualFile chosen = FileChooser.chooseFile(descriptor, parent, null, toSelect);
      if (chosen != null) {
        _selectedFile = VfsUtilCore.virtualToIoFile(chosen);
        final String newLocation = _selectedFile.getPath();
        _importFile.setText(newLocation);
        _dialogBuilder.setOkActionEnabled(true);
      }
    }
    public ImportRunProfile(VirtualFile file, Project project) {
      myFile = file;
      myProject = project;
      try {
        final Document document = JDOMUtil.loadDocument(VfsUtilCore.virtualToIoFile(myFile));
        final Element config = document.getRootElement().getChild("config");
        if (config != null) {
          String configTypeId = config.getAttributeValue("configId");
          if (configTypeId != null) {
            final ConfigurationType configurationType =
                ConfigurationTypeUtil.findConfigurationType(configTypeId);
            if (configurationType != null) {
              myConfiguration =
                  configurationType.getConfigurationFactories()[0].createTemplateConfiguration(
                      project);
              myConfiguration.setName(config.getAttributeValue("name"));
              myConfiguration.readExternal(config);

              final Executor executor =
                  ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID);
              if (executor != null) {
                if (myConfiguration instanceof SMRunnerConsolePropertiesProvider) {
                  myProperties =
                      ((SMRunnerConsolePropertiesProvider) myConfiguration)
                          .createTestConsoleProperties(executor);
                }
              }
            }
          }
        }
      } catch (Exception ignore) {
      }
    }
  public static void assertFilesEqual(VirtualFile fileAfter, VirtualFile fileBefore)
      throws IOException {
    try {
      assertJarFilesEqual(
          VfsUtilCore.virtualToIoFile(fileAfter), VfsUtilCore.virtualToIoFile(fileBefore));
    } catch (IOException e) {
      FileDocumentManager manager = FileDocumentManager.getInstance();

      Document docBefore = manager.getDocument(fileBefore);
      boolean canLoadBeforeText =
          !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == FileTypes.UNKNOWN;
      String textB =
          docBefore != null
              ? docBefore.getText()
              : !canLoadBeforeText
                  ? null
                  : LoadTextUtil.getTextByBinaryPresentation(
                          fileBefore.contentsToByteArray(false), fileBefore)
                      .toString();

      Document docAfter = manager.getDocument(fileAfter);
      boolean canLoadAfterText =
          !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == FileTypes.UNKNOWN;
      String textA =
          docAfter != null
              ? docAfter.getText()
              : !canLoadAfterText
                  ? null
                  : LoadTextUtil.getTextByBinaryPresentation(
                          fileAfter.contentsToByteArray(false), fileAfter)
                      .toString();

      if (textA != null && textB != null) {
        assertEquals(fileAfter.getPath(), textA, textB);
      } else {
        Assert.assertArrayEquals(
            fileAfter.getPath(), fileAfter.contentsToByteArray(), fileBefore.contentsToByteArray());
      }
    }
  }
  /** @return path to the project */
  private void createDirs(@NotNull Collection<String> mockRoots) throws IOException {
    File baseDir;
    if (mockRoots.isEmpty()) {
      return;
    }

    baseDir = VfsUtilCore.virtualToIoFile(myProject.getBaseDir());
    int maxDepth = findMaxDepthAboveProject(mockRoots);
    File projectDir = createChild(baseDir, maxDepth - 1);
    cd(projectDir.getPath());
    for (String path : mockRoots) {
      File file = new File(projectDir, path);
      file.mkdirs();
      File mockDir = new File(file, DOT_MOCK);
      mockDir.mkdirs();
      myFilesToDelete.add(mockDir);
      mockDir.deleteOnExit();
      LocalFileSystem.getInstance().refreshAndFindFileByIoFile(mockDir);
    }
  }
 @Nullable
 @Override
 public RunProfileState getState(
     @NotNull Executor executor, @NotNull ExecutionEnvironment environment)
     throws ExecutionException {
   if (!myImported) {
     myImported = true;
     return new ImportedTestRunnableState(this, VfsUtilCore.virtualToIoFile(myFile));
   }
   if (myConfiguration != null) {
     try {
       return myConfiguration.getState(executor, environment);
     } catch (Throwable e) {
       LOG.info(e);
       throw new ExecutionException("Unable to run the configuration: settings are corrupted");
     }
   }
   throw new ExecutionException(
       "Unable to run the configuration: failed to detect test framework");
 }
 private static void makeAndAddLibraryJar(
     final VirtualFile virtualFile,
     final File zipFile,
     final String pluginName,
     final ZipOutputStream zos,
     final Set<String> usedJarNames,
     final ProgressIndicator progressIndicator,
     final String preferredName)
     throws IOException {
   File libraryJar = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
   libraryJar.deleteOnExit();
   ZipOutputStream jar = null;
   try {
     jar = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(libraryJar)));
     ZipUtil.addFileOrDirRecursively(
         jar,
         libraryJar,
         VfsUtilCore.virtualToIoFile(virtualFile),
         "",
         createFilter(progressIndicator, FileTypeManager.getInstance()),
         null);
   } finally {
     if (jar != null) jar.close();
   }
   final String jarName =
       getLibraryJarName(
           virtualFile.getName() + JAR_EXTENSION,
           usedJarNames,
           preferredName == null ? null : preferredName + JAR_EXTENSION);
   ZipUtil.addFileOrDirRecursively(
       zos,
       zipFile,
       libraryJar,
       getZipPath(pluginName, jarName),
       createFilter(progressIndicator, null),
       null);
 }
  @Override
  public JavaParameters createJavaParameters(
      @NotNull Module module,
      boolean forCreation,
      boolean forTests,
      boolean classpathFromDependencies,
      @Nullable String jvmParams,
      @NotNull MvcCommand command)
      throws ExecutionException {
    JavaParameters params = new JavaParameters();

    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();

    params.setJdk(sdk);
    final VirtualFile sdkRoot = getSdkRoot(module);
    if (sdkRoot == null) {
      return params;
    }

    Map<String, String> env = params.getEnv();
    if (env == null) {
      env = new HashMap<String, String>();
      params.setEnv(env);
    }
    env.put(getSdkHomePropertyName(), FileUtil.toSystemDependentName(sdkRoot.getPath()));

    final VirtualFile lib = sdkRoot.findChild("lib");
    if (lib != null) {
      for (final VirtualFile child : lib.getChildren()) {
        final String name = child.getName();
        if (name.startsWith("groovy-all-") && name.endsWith(".jar")) {
          params.getClassPath().add(child);
        }
      }
    }
    final VirtualFile dist = sdkRoot.findChild("dist");
    if (dist != null) {
      for (final VirtualFile child : dist.getChildren()) {
        final String name = child.getName();
        if (name.endsWith(".jar")) {
          if (name.startsWith("griffon-cli-")
              || name.startsWith("griffon-rt-")
              || name.startsWith("griffon-resources-")) {
            params.getClassPath().add(child);
          }
        }
      }
    }

    /////////////////////////////////////////////////////////////

    params.setMainClass("org.codehaus.griffon.cli.support.GriffonStarter");

    final VirtualFile rootFile;

    if (forCreation) {
      VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
      if (roots.length != 1) {
        throw new ExecutionException(
            "Failed to initialize griffon module: module "
                + module.getName()
                + " contains more than one root");
      }

      command.getArgs().add(0, roots[0].getName());

      rootFile = roots[0].getParent();
    } else {
      rootFile = findAppRoot(module);
      if (rootFile == null) {
        throw new ExecutionException(
            "Failed to run griffon command: module "
                + module.getName()
                + " is not a Griffon module");
      }
    }

    String workDir = VfsUtilCore.virtualToIoFile(rootFile).getAbsolutePath();

    if (jvmParams != null) {
      params.getVMParametersList().addParametersString(jvmParams);
    }

    if (!params.getVMParametersList().getParametersString().contains(XMX_JVM_PARAMETER)) {
      params.getVMParametersList().add("-Xmx256M");
    }

    final String griffonHomePath = FileUtil.toSystemDependentName(sdkRoot.getPath());
    params.getVMParametersList().add("-Dgriffon.home=" + griffonHomePath);
    params.getVMParametersList().add("-Dbase.dir=" + workDir);

    assert sdk != null;
    params
        .getVMParametersList()
        .add("-Dtools.jar=" + ((JavaSdkType) sdk.getSdkType()).getToolsPath(sdk));

    final String confpath = griffonHomePath + GROOVY_STARTER_CONF;
    params.getVMParametersList().add("-Dgroovy.starter.conf=" + confpath);

    params
        .getVMParametersList()
        .add(
            "-Dgroovy.sanitized.stacktraces=\"groovy., org.codehaus.groovy., java., javax., sun., gjdk.groovy., gant., org.codehaus.gant.\"");

    params.getProgramParametersList().add("--main");
    params.getProgramParametersList().add("org.codehaus.griffon.cli.GriffonScriptRunner");
    params.getProgramParametersList().add("--conf");
    params.getProgramParametersList().add(confpath);
    if (!forCreation && classpathFromDependencies) {
      final String path = getApplicationClassPath(module).getPathsString();
      if (StringUtil.isNotEmpty(path)) {
        params.getProgramParametersList().add("--classpath");
        params.getProgramParametersList().add(path);
      }
    }

    params.setWorkingDirectory(workDir);

    ParametersList paramList = new ParametersList();
    command.addToParametersList(paramList);
    params.getProgramParametersList().add(paramList.getParametersString());

    params.setDefaultCharset(module.getProject());

    return params;
  }
 @Nullable
 private static String getVersionByJarManifest(@NotNull VirtualFile file) {
   return JarUtil.getJarAttribute(
       VfsUtilCore.virtualToIoFile(file), Attributes.Name.IMPLEMENTATION_VERSION);
 }