private void attachLibraries(
      @NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) {
    ApplicationManager.getApplication().assertIsDispatchThread();

    if (!libraryRoots.isEmpty()) {
      ApplicationManager.getApplication()
          .runWriteAction(
              () -> {
                ModuleRootManager model = ModuleRootManager.getInstance(myModule);
                LibraryOrderEntry goLibraryEntry =
                    OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName());

                if (goLibraryEntry != null && goLibraryEntry.isValid()) {
                  Library library = goLibraryEntry.getLibrary();
                  if (library != null && !((LibraryEx) library).isDisposed()) {
                    fillLibrary(library, libraryRoots, exclusions);
                  }
                } else {
                  LibraryTable libraryTable =
                      LibraryTablesRegistrar.getInstance().getLibraryTable(myModule.getProject());
                  Library library = libraryTable.createLibrary(getLibraryName());
                  fillLibrary(library, libraryRoots, exclusions);
                  ModuleRootModificationUtil.addDependency(myModule, library);
                }
              });
      showNotification(myModule.getProject());
    } else {
      removeLibraryIfNeeded();
    }
  }
Exemplo n.º 2
0
 public static void removeExcludedRoot(Module module, VirtualFile root) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntryWithAssertion(model, root);
         entry.removeExcludeFolder(root.getUrl());
       });
 }
Exemplo n.º 3
0
 public static void removeAllRoots(Module module, Sdk jdk) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         model.clear();
         model.setSdk(jdk);
       });
 }
Exemplo n.º 4
0
 protected Sdk setupJdkForModule(final String moduleName) {
   final Sdk sdk =
       useJps()
           ? JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk()
           : createJdk("Java 1.5");
   ModuleRootModificationUtil.setModuleSdk(getModule(moduleName), sdk);
   return sdk;
 }
 protected void setUpJdk() {
   // final ProjectJdkEx jdk = ProjectJdkUtil.getDefaultJdk("java 1.4");
   final Sdk jdk = getTestProjectJdk();
   //    ProjectJdkImpl jdk = ProjectJdkTable.getInstance().addJdk(defaultJdk);
   Module[] modules = ModuleManager.getInstance(myProject).getModules();
   for (Module module : modules) {
     ModuleRootModificationUtil.setModuleSdk(module, jdk);
   }
 }
Exemplo n.º 6
0
 public static void addExcludedRoot(Module module, VirtualFile dir) {
   ModuleRootModificationUtil.updateModel(
       module,
       model ->
           ApplicationManager.getApplication()
               .runReadAction(
                   () -> {
                     findContentEntryWithAssertion(model, dir).addExcludeFolder(dir);
                   }));
 }
Exemplo n.º 7
0
 public static <P extends JpsElement> void addSourceRoot(
     Module module, VirtualFile vDir, @NotNull JpsModuleSourceRootType<P> rootType, P properties) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntry(model, vDir);
         if (entry == null) entry = model.addContentEntry(vDir);
         entry.addSourceFolder(vDir, rootType, properties);
       });
 }
Exemplo n.º 8
0
 public static Library addProjectLibrary(
     Module module,
     String libName,
     List<VirtualFile> classesRoots,
     List<VirtualFile> sourceRoots) {
   Ref<Library> result = Ref.create();
   ModuleRootModificationUtil.updateModel(
       module,
       model -> result.set(addProjectLibrary(module, model, libName, classesRoots, sourceRoots)));
   return result.get();
 }
  public void testContentWrite() throws Exception {
    File content = getTestRoot();
    File source = new File(content, "source");
    File testSource = new File(content, "testSource");
    File exclude = new File(content, "exclude");
    File classes = new File(content, "classes");
    File testClasses = new File(content, "testClasses");
    final VirtualFile contentFile = LocalFileSystem.getInstance().findFileByIoFile(content);
    assertNotNull(contentFile);
    final VirtualFile sourceFile = LocalFileSystem.getInstance().findFileByIoFile(source);
    assertNotNull(sourceFile);
    final VirtualFile testSourceFile = LocalFileSystem.getInstance().findFileByIoFile(testSource);
    assertNotNull(testSourceFile);
    final VirtualFile excludeFile = LocalFileSystem.getInstance().findFileByIoFile(exclude);

    assertNotNull(excludeFile);
    final VirtualFile classesFile = LocalFileSystem.getInstance().findFileByIoFile(classes);

    assertNotNull(classesFile);
    final VirtualFile testClassesFile = LocalFileSystem.getInstance().findFileByIoFile(testClasses);

    assertNotNull(testClassesFile);

    final File moduleFile = new File(content, "test.iml");
    final Module module = createModule(moduleFile);
    final ModuleRootManagerImpl moduleRootManager =
        (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);

    PsiTestUtil.addContentRoot(module, contentFile);
    PsiTestUtil.addSourceRoot(module, sourceFile);
    PsiTestUtil.addSourceRoot(module, testSourceFile, true);
    ModuleRootModificationUtil.setModuleSdk(module, JavaSdkImpl.getMockJdk17());
    PsiTestUtil.addExcludedRoot(module, excludeFile);
    PsiTestUtil.setCompilerOutputPath(module, classesFile.getUrl(), false);
    PsiTestUtil.setCompilerOutputPath(module, testClassesFile.getUrl(), true);

    final Element element = new Element("root");
    moduleRootManager.getState().writeExternal(element);
    assertElementEquals(
        element,
        "<root inherit-compiler-output=\"false\">"
            + "<output url=\"file://$MODULE_DIR$/classes\" />"
            + "<output-test url=\"file://$MODULE_DIR$/testClasses\" />"
            + "<exclude-output />"
            + "<content url=\"file://$MODULE_DIR$\">"
            + "<sourceFolder url=\"file://$MODULE_DIR$/source\" isTestSource=\"false\" />"
            + "<sourceFolder url=\"file://$MODULE_DIR$/testSource\" isTestSource=\"true\" />"
            + "<excludeFolder url=\"file://$MODULE_DIR$/exclude\" />"
            + "</content>"
            + "<orderEntry type=\"jdk\" jdkName=\"java 1.7\" jdkType=\"JavaSDK\" />"
            + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
            + "</root>",
        module);
  }
Exemplo n.º 10
0
  public static ContentEntry addContentRoot(Module module, VirtualFile vDir) {
    ModuleRootModificationUtil.updateModel(module, model -> model.addContentEntry(vDir));

    for (ContentEntry entry : ModuleRootManager.getInstance(module).getContentEntries()) {
      if (Comparing.equal(entry.getFile(), vDir)) {
        Assert.assertFalse(((ContentEntryImpl) entry).isDisposed());
        return entry;
      }
    }

    return null;
  }
 public static void setDirectoryProjectSdk(
     @NotNull final Project project, @Nullable final Sdk sdk) {
   ApplicationManager.getApplication()
       .runWriteAction(
           () -> {
             ProjectRootManager.getInstance(project).setProjectSdk(sdk);
             final Module[] modules = ModuleManager.getInstance(project).getModules();
             if (modules.length > 0) {
               ModuleRootModificationUtil.setSdkInherited(modules[0]);
             }
           });
 }
Exemplo n.º 12
0
 public static void removeSourceRoot(Module module, VirtualFile root) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntryWithAssertion(model, root);
         for (SourceFolder sourceFolder : entry.getSourceFolders()) {
           if (root.equals(sourceFolder.getFile())) {
             entry.removeSourceFolder(sourceFolder);
             break;
           }
         }
       });
 }
Exemplo n.º 13
0
 public static void setCompilerOutputPath(Module module, String url, boolean forTests) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         CompilerModuleExtension extension =
             model.getModuleExtension(CompilerModuleExtension.class);
         extension.inheritCompilerOutputPath(false);
         if (forTests) {
           extension.setCompilerOutputPathForTests(url);
         } else {
           extension.setCompilerOutputPath(url);
         }
       });
 }
Exemplo n.º 14
0
 public static void addLibrary(
     Module module, String libName, String libDir, String[] classRoots, String[] sourceRoots) {
   String proto =
       (classRoots.length > 0 ? classRoots[0] : sourceRoots[0]).endsWith(".jar!/")
           ? JarFileSystem.PROTOCOL
           : LocalFileSystem.PROTOCOL;
   String parentUrl = VirtualFileManager.constructUrl(proto, libDir);
   List<String> classesUrls = new ArrayList<>();
   List<String> sourceUrls = new ArrayList<>();
   for (String classRoot : classRoots) {
     classesUrls.add(parentUrl + classRoot);
   }
   for (String sourceRoot : sourceRoots) {
     sourceUrls.add(parentUrl + sourceRoot);
   }
   ModuleRootModificationUtil.addModuleLibrary(module, libName, classesUrls, sourceUrls);
 }
Exemplo n.º 15
0
  public static void addJarsToRoots(
      @NotNull final List<String> jarPaths,
      @Nullable final String libraryName,
      @NotNull final Module module,
      @Nullable final PsiElement location) {
    final Boolean isAdded =
        provideFix(
            new Function<MissingDependencyFixProvider, Boolean>() {
              @Override
              public Boolean fun(MissingDependencyFixProvider provider) {
                return provider.addJarsToRoots(jarPaths, libraryName, module, location);
              }
            });
    if (Boolean.TRUE.equals(isAdded)) return;

    List<String> urls =
        ContainerUtil.map(
            jarPaths,
            new Function<String, String>() {
              @Override
              public String fun(String path) {
                return refreshAndConvertToUrl(path);
              }
            });
    boolean inTests = false;
    if (location != null) {
      final VirtualFile vFile = location.getContainingFile().getVirtualFile();
      if (vFile != null
          && ModuleRootManager.getInstance(module).getFileIndex().isInTestSourceContent(vFile)) {
        inTests = true;
      }
    }
    ModuleRootModificationUtil.addModuleLibrary(
        module,
        libraryName,
        urls,
        Collections.<String>emptyList(),
        inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
  }
Exemplo n.º 16
0
 public static boolean checkTestNGInClasspath(PsiElement psiElement) {
   final Project project = psiElement.getProject();
   final PsiManager manager = PsiManager.getInstance(project);
   if (JavaPsiFacade.getInstance(manager.getProject())
           .findClass(TestNG.class.getName(), psiElement.getResolveScope())
       == null) {
     if (!ApplicationManager.getApplication().isUnitTestMode()) {
       if (Messages.showOkCancelDialog(
               psiElement.getProject(),
               "TestNG will be added to module classpath",
               "Unable to convert.",
               Messages.getWarningIcon())
           != Messages.OK) {
         return false;
       }
     }
     final Module module = ModuleUtilCore.findModuleForPsiElement(psiElement);
     if (module == null) return false;
     String url =
         VfsUtil.getUrlForLibraryRoot(new File(PathUtil.getJarPathForClass(Assert.class)));
     ModuleRootModificationUtil.addModuleLibrary(module, url);
   }
   return true;
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   ModuleRootModificationUtil.setModuleSdk(myModule, IdeaTestUtil.getMockJdk18());
   myFixture.enableInspections(myInspection);
 }
 protected Module addDependentModule() {
   Module module = addModule("dependent");
   ModuleRootModificationUtil.addDependency(module, myModule);
   return module;
 }
Exemplo n.º 19
0
 public static void setJavadocUrls(Module module, String... urls) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.getModuleExtension(JavaModuleExternalPaths.class).setJavadocUrls(urls));
 }
Exemplo n.º 20
0
 public static void setExcludeCompileOutput(Module module, boolean exclude) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.getModuleExtension(CompilerModuleExtension.class).setExcludeOutput(exclude));
 }
 private void assignJdk(Module module) {
   myJdk = ModuleRootManager.getInstance(myModule).getSdk();
   ModuleRootModificationUtil.setModuleSdk(module, myJdk);
 }
Exemplo n.º 22
0
 public static void addLibrary(Module module, String libName, String libPath, String... jarArr) {
   ModuleRootModificationUtil.updateModel(
       module, model -> addLibrary(module, model, libName, libPath, jarArr));
 }
Exemplo n.º 23
0
 public static void addSourceContentToRoots(
     Module module, @NotNull VirtualFile vDir, boolean testSource) {
   ModuleRootModificationUtil.updateModel(
       module, model -> model.addContentEntry(vDir).addSourceFolder(vDir, testSource));
 }
Exemplo n.º 24
0
 public static void removeContentEntry(Module module, VirtualFile contentRoot) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.removeContentEntry(findContentEntryWithAssertion(model, contentRoot)));
 }