public void testBuildModuleBeforeArtifactIfSomeDirectoryInsideModuleOutputIsCopiedToArtifact() {
   String src =
       PathUtil.getParentPath(
           PathUtil.getParentPath(createFile("src/x/A.java", "package x; class A{}")));
   JpsModule module = addModule("m", src);
   File output = JpsJavaExtensionService.getInstance().getOutputDirectory(module, false);
   JpsArtifact artifact = addArtifact(root().dirCopy(new File(output, "x").getAbsolutePath()));
   rebuildAll();
   assertOutput(module, fs().dir("x").file("A.class"));
   assertOutput(artifact, fs().file("A.class"));
 }
 public void testExtractDirectoryFromExcludedJar() throws IOException {
   String jarPath = createFile("dir/lib/j.jar");
   FileUtil.copy(new File(getJUnitJarPath()), new File(jarPath));
   JpsModule module = addModule("m");
   String libDir = PathUtil.getParentPath(jarPath);
   module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(PathUtil.getParentPath(libDir)));
   module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(libDir));
   final JpsArtifact a = addArtifact("a", root().extractedDir(jarPath, "/junit/textui/"));
   buildAll();
   assertOutput(a, fs().file("ResultPrinter.class").file("TestRunner.class"));
 }
 public void testTwoDirsInArchive() {
   final String dir1 = PathUtil.getParentPath(PathUtil.getParentPath(createFile("dir1/a/x.txt")));
   final String dir2 = PathUtil.getParentPath(PathUtil.getParentPath(createFile("dir2/a/y.txt")));
   final JpsArtifact a =
       addArtifact(
           root()
               .archive("a.jar")
               .dirCopy(dir1)
               .dirCopy(dir2)
               .dir("a")
               .fileCopy(createFile("z.txt")));
   buildAll();
   assertOutput(a, fs().archive("a.jar").dir("a").file("x.txt").file("y.txt").file("z.txt"));
 }
  public void testCopyExcludedFile() {
    // excluded files under non-excluded directory should not be copied
    final String file = createFile("xxx/excluded/a.txt");
    createFile("xxx/b.txt");
    createFile("xxx/CVS");
    final String dir = PathUtil.getParentPath(PathUtil.getParentPath(file));

    JpsModule module = addModule("myModule");
    module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(dir));
    module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(PathUtil.getParentPath(file)));

    final JpsArtifact a = addArtifact(root().dirCopy(dir));
    buildAll();
    assertOutput(a, fs().file("b.txt"));
  }
  public void testCopyExcludedFolder() {
    // explicitly added excluded files should be copied (e.g. compile output)
    final String file = createFile("xxx/excluded/a.txt");
    createFile("xxx/excluded/CVS");
    final String excluded = PathUtil.getParentPath(file);
    final String dir = PathUtil.getParentPath(excluded);

    final JpsModule module = addModule("myModule");
    module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(dir));
    module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(excluded));

    final JpsArtifact a = addArtifact(root().dirCopy(excluded));
    buildAll();
    assertOutput(a, fs().file("a.txt"));
  }
 public void testIgnoredFileInArchive() {
   final String file = createFile("a/.svn/a.txt");
   createFile("a/svn/b.txt");
   final JpsArtifact a = addArtifact(archive("a.jar").parentDirCopy(PathUtil.getParentPath(file)));
   buildAll();
   assertOutput(a, fs().archive("a.jar").dir("svn").file("b.txt"));
 }
  public void testModuleOutput() {
    final String file = createFile("src/A.java", "public class A {}");
    final JpsModule module = addModule("a", PathUtil.getParentPath(file));
    final JpsArtifact artifact = addArtifact(root().module(module));

    buildArtifacts(artifact);
    assertOutput(artifact, fs().file("A.class"));
  }
 public void testCopyResourcesFromModuleOutput() {
   String file = createFile("src/a.xml", "");
   JpsJavaExtensionService.getInstance()
       .getOrCreateCompilerConfiguration(myProject)
       .addResourcePattern("*.xml");
   JpsModule module = addModule("a", PathUtil.getParentPath(file));
   JpsArtifact artifact = addArtifact(root().module(module));
   buildArtifacts(artifact);
   assertOutput(artifact, fs().file("a.xml"));
 }
 private static GradleProject populateProject(
     @NotNull IdeaProject project, @NotNull String projectPath) {
   String projectDirPath = GradleUtil.toCanonicalPath(PathUtil.getParentPath(projectPath));
   // Gradle API doesn't expose project compile output path yet.
   GradleProject result = new GradleProject(projectDirPath, projectDirPath + "/out");
   result.setName(project.getName());
   result.setJdkVersion(project.getJdkName());
   result.setLanguageLevel(project.getLanguageLevel().getLevel());
   return result;
 }
Пример #10
0
  private static boolean isPathOutsideProjectContent(
      @NotNull final ProjectFileIndex fileIndex, @NotNull String path) {
    while (!path.isEmpty()) {
      final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
      if (file == null) {
        path = PathUtil.getParentPath(path);
      } else {
        return !fileIndex.isInContent(file);
      }
    }

    return false;
  }
  @Override
  public PsiFile addFileToProject(
      @NonNls String rootPath, @NonNls final String relativePath, @NonNls final String fileText)
      throws IOException {
    final VirtualFile dir =
        VfsUtil.createDirectories(rootPath + "/" + PathUtil.getParentPath(relativePath));

    final VirtualFile[] virtualFile = new VirtualFile[1];
    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        virtualFile[0] = dir.createChildData(this, StringUtil.getShortName(relativePath, '/'));
        VfsUtil.saveText(virtualFile[0], fileText);
      }
    }.execute();
    return ApplicationManager.getApplication()
        .runReadAction(
            new Computable<PsiFile>() {
              public PsiFile compute() {
                return PsiManager.getInstance(getProject()).findFile(virtualFile[0]);
              }
            });
  }
 /**
  * @param xmlFile
  * @return True if the file is contained within 'OSGI-INF/blueprint'
  */
 private boolean isInsideOsgiInfBlueprint(XmlFile xmlFile) {
   String localPath = PathUtil.getLocalPath(xmlFile.getVirtualFile());
   if (localPath == null) return false;
   String parentPath = PathUtil.getParentPath(localPath);
   return parentPath.endsWith("OSGI-INF\\blueprint") || parentPath.endsWith("OSGI-INF/blueprint");
 }
/** @author nik */
public class ManifestFileUtil {
  private static final Logger LOG =
      Logger.getInstance(
          "#com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorContextImpl");
  public static final String MANIFEST_PATH = JarFile.MANIFEST_NAME;
  public static final String MANIFEST_FILE_NAME = PathUtil.getFileName(MANIFEST_PATH);
  public static final String MANIFEST_DIR_NAME = PathUtil.getParentPath(MANIFEST_PATH);

  private ManifestFileUtil() {}

  @Nullable
  public static VirtualFile findManifestFile(
      @NotNull CompositePackagingElement<?> root,
      PackagingElementResolvingContext context,
      ArtifactType artifactType) {
    return ArtifactUtil.findSourceFileByOutputPath(root, MANIFEST_PATH, context, artifactType);
  }

  @Nullable
  public static VirtualFile suggestManifestFileDirectory(
      @NotNull CompositePackagingElement<?> root,
      PackagingElementResolvingContext context,
      ArtifactType artifactType) {
    final VirtualFile metaInfDir =
        ArtifactUtil.findSourceFileByOutputPath(root, MANIFEST_DIR_NAME, context, artifactType);
    if (metaInfDir != null) {
      return metaInfDir;
    }

    final Ref<VirtualFile> sourceDir = Ref.create(null);
    final Ref<VirtualFile> sourceFile = Ref.create(null);
    ArtifactUtil.processElementsWithSubstitutions(
        root.getChildren(),
        context,
        artifactType,
        PackagingElementPath.EMPTY,
        new PackagingElementProcessor<PackagingElement<?>>() {
          @Override
          public boolean process(
              @NotNull PackagingElement<?> element, @NotNull PackagingElementPath path) {
            if (element instanceof FileCopyPackagingElement) {
              final VirtualFile file = ((FileCopyPackagingElement) element).findFile();
              if (file != null) {
                sourceFile.set(file);
              }
            } else if (element instanceof DirectoryCopyPackagingElement) {
              final VirtualFile file = ((DirectoryCopyPackagingElement) element).findFile();
              if (file != null) {
                sourceDir.set(file);
                return false;
              }
            }
            return true;
          }
        });

    if (!sourceDir.isNull()) {
      return sourceDir.get();
    }

    final Project project = context.getProject();
    return suggestBaseDir(project, sourceFile.get());
  }

  @Nullable
  public static VirtualFile suggestManifestFileDirectory(
      @NotNull Project project, @Nullable Module module) {
    OrderEnumerator enumerator =
        module != null
            ? OrderEnumerator.orderEntries(module)
            : OrderEnumerator.orderEntries(project);
    final VirtualFile[] files =
        enumerator
            .withoutDepModules()
            .withoutLibraries()
            .withoutSdk()
            .productionOnly()
            .sources()
            .getRoots();
    if (files.length > 0) {
      return files[0];
    }
    return suggestBaseDir(project, null);
  }

  @Nullable
  private static VirtualFile suggestBaseDir(
      @NotNull Project project, final @Nullable VirtualFile file) {
    final VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
    if (file == null && contentRoots.length > 0) {
      return contentRoots[0];
    }

    if (file != null) {
      for (VirtualFile contentRoot : contentRoots) {
        if (VfsUtil.isAncestor(contentRoot, file, false)) {
          return contentRoot;
        }
      }
    }

    return project.getBaseDir();
  }

  public static Manifest readManifest(@NotNull VirtualFile manifestFile) {
    try {
      final InputStream inputStream = manifestFile.getInputStream();
      final Manifest manifest;
      try {
        manifest = new Manifest(inputStream);
      } finally {
        inputStream.close();
      }
      return manifest;
    } catch (IOException ignored) {
      return new Manifest();
    }
  }

  public static void updateManifest(
      @NotNull final VirtualFile file,
      final @Nullable String mainClass,
      final @Nullable List<String> classpath,
      final boolean replaceValues) {
    final Manifest manifest = readManifest(file);
    final Attributes mainAttributes = manifest.getMainAttributes();

    if (mainClass != null) {
      mainAttributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    } else if (replaceValues) {
      mainAttributes.remove(Attributes.Name.MAIN_CLASS);
    }

    if (classpath != null && !classpath.isEmpty()) {
      List<String> updatedClasspath;
      if (replaceValues) {
        updatedClasspath = classpath;
      } else {
        updatedClasspath = new ArrayList<String>();
        final String oldClasspath = (String) mainAttributes.get(Attributes.Name.CLASS_PATH);
        if (!StringUtil.isEmpty(oldClasspath)) {
          updatedClasspath.addAll(StringUtil.split(oldClasspath, " "));
        }
        for (String path : classpath) {
          if (!updatedClasspath.contains(path)) {
            updatedClasspath.add(path);
          }
        }
      }
      mainAttributes.put(Attributes.Name.CLASS_PATH, StringUtil.join(updatedClasspath, " "));
    } else if (replaceValues) {
      mainAttributes.remove(Attributes.Name.CLASS_PATH);
    }

    ManifestBuilder.setVersionAttribute(mainAttributes);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                try {
                  final OutputStream outputStream = file.getOutputStream(ManifestFileUtil.class);
                  try {
                    manifest.write(outputStream);
                  } finally {
                    outputStream.close();
                  }
                } catch (IOException e) {
                  LOG.info(e);
                }
              }
            });
  }

  @NotNull
  public static ManifestFileConfiguration createManifestFileConfiguration(
      @NotNull VirtualFile manifestFile) {
    final String path = manifestFile.getPath();
    Manifest manifest = readManifest(manifestFile);
    String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
    final String classpathText = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    final List<String> classpath = new ArrayList<String>();
    if (classpathText != null) {
      classpath.addAll(StringUtil.split(classpathText, " "));
    }
    return new ManifestFileConfiguration(path, classpath, mainClass, manifestFile.isWritable());
  }

  public static List<String> getClasspathForElements(
      List<? extends PackagingElement<?>> elements,
      PackagingElementResolvingContext context,
      final ArtifactType artifactType) {
    final List<String> classpath = new ArrayList<String>();
    final PackagingElementProcessor<PackagingElement<?>> processor =
        new PackagingElementProcessor<PackagingElement<?>>() {
          @Override
          public boolean process(
              @NotNull PackagingElement<?> element, @NotNull PackagingElementPath path) {
            if (element instanceof FileCopyPackagingElement) {
              final String fileName = ((FileCopyPackagingElement) element).getOutputFileName();
              classpath.add(DeploymentUtil.appendToPath(path.getPathString(), fileName));
            } else if (element instanceof DirectoryCopyPackagingElement) {
              classpath.add(path.getPathString());
            } else if (element instanceof ArchivePackagingElement) {
              final String archiveName = ((ArchivePackagingElement) element).getName();
              classpath.add(DeploymentUtil.appendToPath(path.getPathString(), archiveName));
            }
            return true;
          }
        };
    for (PackagingElement<?> element : elements) {
      ArtifactUtil.processPackagingElements(element, null, processor, context, true, artifactType);
    }
    return classpath;
  }

  @Nullable
  public static VirtualFile showDialogAndCreateManifest(
      final ArtifactEditorContext context, final CompositePackagingElement<?> element) {
    FileChooserDescriptor descriptor = createDescriptorForManifestDirectory();
    final VirtualFile directory =
        suggestManifestFileDirectory(element, context, context.getArtifactType());
    final VirtualFile file = FileChooser.chooseFile(descriptor, context.getProject(), directory);
    if (file == null) {
      return null;
    }

    return createManifestFile(file, context.getProject());
  }

  @Nullable
  public static VirtualFile createManifestFile(
      final @NotNull VirtualFile directory, final @NotNull Project project) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final Ref<IOException> exc = Ref.create(null);
    final VirtualFile file =
        new WriteAction<VirtualFile>() {
          protected void run(@NotNull final Result<VirtualFile> result) {
            VirtualFile dir = directory;
            try {
              if (!dir.getName().equals(MANIFEST_DIR_NAME)) {
                dir = VfsUtil.createDirectoryIfMissing(dir, MANIFEST_DIR_NAME);
              }
              final VirtualFile file = dir.createChildData(this, MANIFEST_FILE_NAME);
              final OutputStream output = file.getOutputStream(this);
              try {
                final Manifest manifest = new Manifest();
                ManifestBuilder.setVersionAttribute(manifest.getMainAttributes());
                manifest.write(output);
              } finally {
                output.close();
              }
              result.setResult(file);
            } catch (IOException e) {
              exc.set(e);
            }
          }
        }.execute().getResultObject();

    final IOException exception = exc.get();
    if (exception != null) {
      LOG.info(exception);
      Messages.showErrorDialog(project, exception.getMessage(), CommonBundle.getErrorTitle());
      return null;
    }
    return file;
  }

  public static FileChooserDescriptor createDescriptorForManifestDirectory() {
    FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
    descriptor.setTitle("Select Directory for META-INF/MANIFEST.MF file");
    return descriptor;
  }

  public static void addManifestFileToLayout(
      final @NotNull String path,
      final @NotNull ArtifactEditorContext context,
      final @NotNull CompositePackagingElement<?> element) {
    context.editLayout(
        context.getArtifact(),
        new Runnable() {
          public void run() {
            final VirtualFile file = findManifestFile(element, context, context.getArtifactType());
            if (file == null || !FileUtil.pathsEqual(file.getPath(), path)) {
              PackagingElementFactory.getInstance()
                  .addFileCopy(element, MANIFEST_DIR_NAME, path, MANIFEST_FILE_NAME);
            }
          }
        });
  }

  @Nullable
  public static PsiClass selectMainClass(Project project, final @Nullable String initialClassName) {
    final TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(project);
    final GlobalSearchScope searchScope = GlobalSearchScope.allScope(project);
    final PsiClass aClass =
        initialClassName != null
            ? JavaPsiFacade.getInstance(project).findClass(initialClassName, searchScope)
            : null;
    final TreeClassChooser chooser =
        chooserFactory.createWithInnerClassesScopeChooser(
            "Select Main Class", searchScope, new MainClassFilter(), aClass);
    chooser.showDialog();
    return chooser.getSelected();
  }

  public static void setupMainClassField(
      final Project project, final TextFieldWithBrowseButton field) {
    field.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            final PsiClass selected = selectMainClass(project, field.getText());
            if (selected != null) {
              field.setText(selected.getQualifiedName());
            }
          }
        });
  }

  private static class MainClassFilter implements ClassFilter {
    public boolean isAccepted(final PsiClass aClass) {
      return ApplicationManager.getApplication()
          .runReadAction(
              new Computable<Boolean>() {
                @Override
                public Boolean compute() {
                  return PsiMethodUtil.MAIN_CLASS.value(aClass)
                      && PsiMethodUtil.hasMainMethod(aClass);
                }
              });
    }
  }
}