com.intellij.openapi.roots.ModuleRootModificationUtil Java Examples

The following examples show how to use com.intellij.openapi.roots.ModuleRootModificationUtil. 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: ModificationAction.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
  Project project = event.getProject();
  if (project == null) return;
  Navigatable element = event.getData(CommonDataKeys.NAVIGATABLE);
  if (element instanceof PsiClass) {
    PsiFile file = ((PsiClass) element).getContainingFile();
    if (file == null) return;
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null) return;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final Module module = fileIndex.getModuleForFile(virtualFile);
    if (module == null) return;
    if (!ModuleRootManager.getInstance(module).getFileIndex().isInContent(virtualFile)) {
      ModuleRootModificationUtil.addModuleLibrary(module, virtualFile.getUrl());
    }
  }

}
 
Example #2
Source File: BlazeSampleDataDirectoryProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public PathString getOrCreateSampleDataDirectory() throws IOException {
  PathString sampleDataDirectory = getSampleDataDirectory();
  if (sampleDataDirectory == null) {
    return null;
  }

  PathString rootPath = sampleDataDirectory.getRoot();
  VirtualFile root = rootPath != null ? toVirtualFile(rootPath) : null;
  if (root == null) {
    return sampleDataDirectory;
  }

  createDirectoryIfMissing(root, sampleDataDirectory.getPortablePath());

  // We want to make sure that the sample data directory is associated with this resource module
  // instead of just being lumped into the workspace module because it sits outside the res
  // folder.
  ModuleRootModificationUtil.addContentRoot(module, sampleDataDirectory.getPortablePath());
  return sampleDataDirectory;
}
 
Example #3
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void canLoadWorkspaceWithConfigFile() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  tmp.ensureDir("abc/dart/config/intellij-plugins");
  tmp.writeFile("abc/dart/config/intellij-plugins/flutter.json",
                "{\"daemonScript\": \"something_daemon.sh\"," +
                "\"doctorScript\": \"something_doctor.sh\"}");
  tmp.writeFile("abc/something_daemon.sh", "");
  tmp.writeFile("abc/something_doctor.sh", "");

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertEquals("something_daemon.sh", w.getDaemonScript());
  assertEquals("something_doctor.sh", w.getDoctorScript());
}
 
Example #4
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void canLoadWorkspaceWithConfigFileAndScriptInReadonly() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  tmp.ensureDir("READONLY/abc/dart/config/intellij-plugins");
  tmp.writeFile("READONLY/abc/dart/config/intellij-plugins/flutter.json",
                "{\"daemonScript\": \"scripts/flutter_daemon.sh\"," +
                "\"doctorScript\": \"scripts/flutter_doctor.sh\"}");
  tmp.ensureDir("READONLY/abc/scripts");
  tmp.writeFile("READONLY/abc/scripts/flutter_daemon.sh", "");
  tmp.writeFile("READONLY/abc/scripts/flutter_doctor.sh", "");

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertEquals("../READONLY/abc/scripts/flutter_daemon.sh", w.getDaemonScript());
  assertEquals("../READONLY/abc/scripts/flutter_doctor.sh", w.getDoctorScript());
}
 
Example #5
Source File: CamelProjectComponentTestIT.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
public void testAddModule() throws IOException {
    CamelService service = ServiceManager.getService(myProject, CamelService.class);
    assertEquals(0, service.getLibraries().size());

    File camelJar = createTestArchive("camel-core-2.22.0.jar");
    VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(camelJar);

    final LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("myNewModel.iml");
        Library library = projectLibraryTable.createLibrary("Maven: org.apache.camel:camel-core:2.22.0-snapshot");
        final Library.ModifiableModel libraryModifiableModel = library.getModifiableModel();
        libraryModifiableModel.addRoot(virtualFile, OrderRootType.CLASSES);
        libraryModifiableModel.commit();
        ModuleRootModificationUtil.addDependency(moduleA, library);
    });

    UIUtil.dispatchAllInvocationEvents();
    assertEquals(1, service.getLibraries().size());
}
 
Example #6
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void canLoadWorkspaceWithConfigFileAndScriptInReadonly() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  tmp.ensureDir("READONLY/abc/dart/config/intellij-plugins");
  tmp.writeFile("READONLY/abc/dart/config/intellij-plugins/flutter.json",
                "{\"daemonScript\": \"scripts/flutter_daemon.sh\"," +
                "\"doctorScript\": \"scripts/flutter_doctor.sh\"}");
  tmp.ensureDir("READONLY/abc/scripts");
  tmp.writeFile("READONLY/abc/scripts/flutter_daemon.sh", "");
  tmp.writeFile("READONLY/abc/scripts/flutter_doctor.sh", "");

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertEquals("../READONLY/abc/scripts/flutter_daemon.sh", w.getDaemonScript());
  assertEquals("../READONLY/abc/scripts/flutter_doctor.sh", w.getDoctorScript());
}
 
Example #7
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void canLoadWorkspaceWithConfigFile() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  tmp.ensureDir("abc/dart/config/intellij-plugins");
  tmp.writeFile("abc/dart/config/intellij-plugins/flutter.json",
                "{\"daemonScript\": \"something_daemon.sh\"," +
                "\"doctorScript\": \"something_doctor.sh\"}");
  tmp.writeFile("abc/something_daemon.sh", "");
  tmp.writeFile("abc/something_doctor.sh", "");

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertEquals("something_daemon.sh", w.getDaemonScript());
  assertEquals("something_doctor.sh", w.getDoctorScript());
}
 
Example #8
Source File: QuarkusModuleUtil.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Check if the Quarkus library needs to be recomputed and update it if required.
 *
 * @param module the module to check
 */
public static void ensureQuarkusLibrary(Module module) {
    LOGGER.info("Ensuring library to " + module.getName());
    long start = System.currentTimeMillis();
    ToolDelegate toolDelegate = ToolDelegate.getDelegate(module);
    if (toolDelegate != null) {
        LOGGER.info("Tool delegate found for " + module.getName());
        if (isQuarkusModule(module)) {
            LOGGER.info("isQuarkus module " + module.getName());
            Integer previousHash = module.getComponent(QuarkusModuleComponent.class).getHash();
            Integer actualHash = computeHash(module);
            if (actualHash != null && !actualHash.equals(previousHash)) {
                ModuleRootModificationUtil.updateModel(module, model -> {
                    LibraryTable table = model.getModuleLibraryTable();
                    Library library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME);
                    while (library != null) {
                        table.removeLibrary(library);
                        library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME);
                    }
                    List<VirtualFile>[] files = toolDelegate.getDeploymentFiles(module);
                    LOGGER.info("Adding library to " + module.getName() + " previousHash=" + previousHash + " newHash=" + actualHash);
                    addLibrary(model, files);
                });
                module.getComponent(QuarkusModuleComponent.class).setHash(actualHash);
            }
        }
    }
    LOGGER.info("ensureQuarkusLibrary ran in " + (System.currentTimeMillis() - start));
}
 
Example #9
Source File: PackagingElementsTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
static Library addProjectLibrary(final Project project, final @javax.annotation.Nullable Module module, final String name, final DependencyScope scope, final VirtualFile[] jars) {
  return WriteAction.compute(() -> {
    final Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(project).createLibrary(name);
    final Library.ModifiableModel libraryModel = library.getModifiableModel();
    for (VirtualFile jar : jars) {
      libraryModel.addRoot(jar, BinariesOrderRootType.getInstance());
    }
    libraryModel.commit();
    if (module != null) {
      ModuleRootModificationUtil.addDependency(module, library, scope, false);
    }
    return library;
  });
}
 
Example #10
Source File: CMakeWorkspaceOverride.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void clearContentRootsAndLibrariesIfModifiedForCMake(Module module) {
  ModuleRootModificationUtil.updateModel(
      module,
      modifiableModel -> {
        if (areLibrariesAndRootsModifiedByCMake(modifiableModel)) {
          // Nuke the roots from orbit.
          modifiableModel.clear();
        }
      });
}
 
Example #11
Source File: CamelJSonPathAnnotatorTestIT.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    File[] mavenArtifacts = getMavenArtifacts(CAMEL_JSONPATH_MAVEN_ARTIFACT);
    for (File file : mavenArtifacts) {
        final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
        final LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(getModule().getProject());
        ApplicationManager.getApplication().runWriteAction(() -> {
            String name = file.getName();
            // special for camel JARs
            if (name.contains("camel-core")) {
                name = "org.apache.camel:camel-core:2.22.0";
            } else if (name.contains("camel-jsonpath")) {
                name = "org.apache.camel:camel-jsonpath:2.22.0";
            } else {
                // need to fix the name
                if (name.endsWith(".jar")) {
                    name = name.substring(0, name.length() - 4);
                }
                int lastDash = name.lastIndexOf('-');
                name = name.substring(0, lastDash) + ":" + name.substring(lastDash + 1);
                // add bogus groupid
                name = "com.foo:" + name;
            }

            Library library = projectLibraryTable.createLibrary("maven: " + name);
            final Library.ModifiableModel libraryModifiableModel = library.getModifiableModel();
            libraryModifiableModel.addRoot(virtualFile, OrderRootType.CLASSES);
            libraryModifiableModel.commit();
            ModuleRootModificationUtil.addDependency(getModule(), library);
        });
    }

    disposeOnTearDown(ServiceManager.getService(getModule().getProject(), CamelCatalogService.class));
    disposeOnTearDown(ServiceManager.getService(getModule().getProject(), CamelService.class));
    ServiceManager.getService(getModule().getProject(), CamelService.class).setCamelPresent(true);
}
 
Example #12
Source File: CamelProjectComponentTestIT.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
private Library addLibraryToModule(VirtualFile camelSpringVirtualFile, LibraryTable projectLibraryTable, String name) {
    return ApplicationManager.getApplication().runWriteAction((Computable<Library>)() -> {
        Library library = projectLibraryTable.createLibrary(name);
        Library.ModifiableModel libraryModifiableModel = library.getModifiableModel();
        libraryModifiableModel.addRoot(camelSpringVirtualFile, OrderRootType.CLASSES);
        libraryModifiableModel.commit();
        ModuleRootModificationUtil.addDependency(getModule(), library);
        return library;
    });
}
 
Example #13
Source File: MainFileTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  contentRoot = tmp.ensureDir("root");
  appDir = tmp.ensureDir("root/work").getPath();
  tmp.writeFile("root/work/pubspec.yaml", "");
  tmp.ensureDir("root/work/lib");
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #14
Source File: FlutterConsoleFilterTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  contentRoot = tmp.ensureDir("root");
  appDir = tmp.ensureDir("root/test").getPath();
  tmp.writeFile("root/test/widget_test.dart", "");
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #15
Source File: WorkspaceCacheTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  cache = WorkspaceCache.getInstance(fixture.getProject());
  assertNotNull(cache);

  tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");
  tmp.ensureDir("abc/dart/config/intellij-plugins");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");

  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #16
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void canLoadWorkspaceWithoutConfigFile() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertFalse("config shouldn't be there", w.hasPluginConfig());
}
 
Example #17
Source File: ProjectWatchTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldSendEventWhenModuleRootsChange() throws Exception {
  Testing.runOnDispatchThread(() -> {
    final AtomicInteger callCount = new AtomicInteger();
    final ProjectWatch listen = ProjectWatch.subscribe(fixture.getProject(), callCount::incrementAndGet);

    ModuleRootModificationUtil.addContentRoot(fixture.getModule(), "testDir");
    assertEquals(1, callCount.get());
  });
}
 
Example #18
Source File: MainFileTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  contentRoot = tmp.ensureDir("root");
  appDir = tmp.ensureDir("root/work").getPath();
  tmp.writeFile("root/work/pubspec.yaml", "");
  tmp.ensureDir("root/work/lib");
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #19
Source File: FlutterConsoleFilterTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  contentRoot = tmp.ensureDir("root");
  appDir = tmp.ensureDir("root/test").getPath();
  tmp.writeFile("root/test/widget_test.dart", "");
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #20
Source File: WorkspaceCacheTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  cache = WorkspaceCache.getInstance(fixture.getProject());
  assertNotNull(cache);

  tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");
  tmp.ensureDir("abc/dart/config/intellij-plugins");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");

  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath()));
}
 
Example #21
Source File: WorkspaceTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void canLoadWorkspaceWithoutConfigFile() throws Exception {
  final VirtualFile expectedRoot = tmp.ensureDir("abc");
  tmp.writeFile("abc/WORKSPACE", "");

  tmp.ensureDir("abc/dart");
  final VirtualFile contentRoot = tmp.ensureDir("abc/dart/something");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), contentRoot.getPath());

  final Workspace w = Workspace.loadUncached(fixture.getProject());

  assertNotNull("expected a workspace", w);
  assertEquals(expectedRoot, w.getRoot());
  assertFalse("config shouldn't be there", w.hasPluginConfig());
}
 
Example #22
Source File: ProjectWatchTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldSendEventWhenModuleRootsChange() throws Exception {
  Testing.runOnDispatchThread(() -> {
    final AtomicInteger callCount = new AtomicInteger();
    final ProjectWatch listen = ProjectWatch.subscribe(fixture.getProject(), callCount::incrementAndGet);

    ModuleRootModificationUtil.addContentRoot(fixture.getModule(), "testDir");
    assertEquals(1, callCount.get());
  });
}
 
Example #23
Source File: WorkspaceCacheTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void removeContentRoot() throws Exception {
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.updateModel(
      fixture.getModule(),
      (ModifiableRootModel model) -> model.removeContentEntry(model.getContentEntries()[0])));
}
 
Example #24
Source File: FlutterPositionMapperTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  sourceRoot = tmp.ensureDir("root");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), sourceRoot.getPath());
}
 
Example #25
Source File: FlutterPositionMapperTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  sourceRoot = tmp.ensureDir("root");
  ModuleRootModificationUtil.addContentRoot(fixture.getModule(), sourceRoot.getPath());
}
 
Example #26
Source File: WorkspaceCacheTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void removeContentRoot() throws Exception {
  Testing.runOnDispatchThread(
    () -> ModuleRootModificationUtil.updateModel(
      fixture.getModule(),
      (ModifiableRootModel model) -> model.removeContentEntry(model.getContentEntries()[0])));
}
 
Example #27
Source File: MavenImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
protected Sdk setupJdkForModule(final String moduleName) {
  final Sdk sdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
  ModuleRootModificationUtil.setModuleSdk(getModule(moduleName), sdk);
  return sdk;
}
 
Example #28
Source File: ExternalSystemTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
protected Sdk setupJdkForModule(final String moduleName) {
  final Sdk sdk = true ? JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk() : createJdk();
  ModuleRootModificationUtil.setModuleSdk(getModule(moduleName), sdk);
  return sdk;
}
 
Example #29
Source File: PackagingElementsTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected static void addModuleLibrary(final Module module, final VirtualFile jar) {
  ModuleRootModificationUtil.addModuleLibrary(module, jar.getUrl());
}
 
Example #30
Source File: PackagingElementsTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected static void addModuleDependency(final Module module, final Module dependency) {
  ModuleRootModificationUtil.addDependency(module, dependency);
}