Search in sources :

Example 46 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class MvcModuleStructureUtil method createAuxiliaryModule.

@NotNull
public static Module createAuxiliaryModule(@NotNull Module appModule, final String moduleName, final MvcFramework framework) {
    ModuleManager moduleManager = ModuleManager.getInstance(appModule.getProject());
    final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
    final String moduleFilePath = new File(appModule.getModuleFilePath()).getParent() + "/" + moduleName + ".iml";
    final VirtualFile existing = LocalFileSystem.getInstance().findFileByPath(moduleFilePath);
    if (existing != null) {
        try {
            existing.delete("Grails/Griffon plugins maintenance");
        } catch (IOException e) {
            LOG.error(e);
        }
    }
    moduleModel.newModule(moduleFilePath, StdModuleTypes.JAVA.getId());
    moduleModel.commit();
    Module pluginsModule = moduleManager.findModuleByName(moduleName);
    assert pluginsModule != null;
    ModifiableRootModel newRootModel = ModuleRootManager.getInstance(pluginsModule).getModifiableModel();
    ModifiableRootModel appModel = ModuleRootManager.getInstance(appModule).getModifiableModel();
    copySdkAndLibraries(appModel, newRootModel, framework);
    newRootModel.commit();
    appModel.commit();
    return pluginsModule;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) IOException(java.io.IOException) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class VcsStructureChooser method calculateModules.

@NotNull
private Map<VirtualFile, String> calculateModules(@NotNull List<VirtualFile> roots) {
    Map<VirtualFile, String> result = ContainerUtil.newHashMap();
    final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    // assertion for read access inside
    Module[] modules = ApplicationManager.getApplication().runReadAction(new Computable<Module[]>() {

        public Module[] compute() {
            return moduleManager.getModules();
        }
    });
    TreeSet<VirtualFile> checkSet = new TreeSet<>(FilePathComparator.getInstance());
    checkSet.addAll(roots);
    for (Module module : modules) {
        VirtualFile[] files = ModuleRootManager.getInstance(module).getContentRoots();
        for (VirtualFile file : files) {
            VirtualFile floor = checkSet.floor(file);
            if (floor != null) {
                result.put(file, module.getName());
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class TestDiscoveryConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(final JavaTestConfigurationBase configuration, ConfigurationContext configurationContext, Ref<PsiElement> ref) {
    if (!Registry.is("testDiscovery.enabled")) {
        return false;
    }
    final Location contextLocation = configurationContext.getLocation();
    assert contextLocation != null;
    final Location location = JavaExecutionUtil.stepIntoSingleClass(contextLocation);
    if (location == null)
        return false;
    final PsiMethod sourceMethod = getSourceMethod(location);
    final Pair<String, String> position = getPosition(sourceMethod);
    if (sourceMethod != null && position != null) {
        try {
            final Project project = configuration.getProject();
            final TestDiscoveryIndex testDiscoveryIndex = TestDiscoveryIndex.getInstance(project);
            final Collection<String> testsByMethodName = testDiscoveryIndex.getTestsByMethodName(position.first, position.second);
            if (testsByMethodName == null || ContainerUtil.filter(testsByMethodName, s -> s.startsWith(configuration.getFrameworkPrefix())).isEmpty()) {
                return false;
            }
            setPosition(configuration, new PsiLocation<>(sourceMethod));
            configuration.setName("Tests for " + StringUtil.getShortName(position.first) + "." + position.second);
            final RunnerAndConfigurationSettings template = configurationContext.getRunManager().getConfigurationTemplate(getConfigurationFactory());
            final Module predefinedModule = ((ModuleBasedConfiguration) template.getConfiguration()).getConfigurationModule().getModule();
            if (predefinedModule != null) {
                configuration.setModule(predefinedModule);
            }
            //potentially this set won't be big, it reflects modules from where user starts his tests
            final Collection<String> modules = testDiscoveryIndex.getTestModulesByMethodName(position.first, position.second, configuration.getFrameworkPrefix());
            if (modules.isEmpty())
                return true;
            final List<Module> survivedModules = new ArrayList<>();
            final ModuleManager moduleManager = ModuleManager.getInstance(project);
            for (String moduleName : modules) {
                final Module moduleByName = moduleManager.findModuleByName(moduleName);
                if (moduleByName != null) {
                    survivedModules.add(moduleByName);
                }
            }
            if (survivedModules.isEmpty())
                return true;
            final Set<Module> allModules = new HashSet<>(Arrays.asList(moduleManager.getModules()));
            survivedModules.forEach(module -> {
                final List<Module> dependentModules = ModuleUtilCore.getAllDependentModules(module);
                dependentModules.add(module);
                allModules.retainAll(dependentModules);
            });
            if (!allModules.isEmpty()) {
                Module aModule = allModules.iterator().next();
                for (Module module : survivedModules) {
                    if (allModules.contains(module)) {
                        aModule = module;
                    }
                }
                configuration.setModule(aModule);
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    return false;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) IOException(java.io.IOException) ModuleManager(com.intellij.openapi.module.ModuleManager) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) HashSet(com.intellij.util.containers.HashSet)

Example 49 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class MultiModuleEditingTest method testRootsEditing.

public void testRootsEditing() {
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    final MyModuleListener moduleListener = new MyModuleListener();
    connection.subscribe(ProjectTopics.MODULES, moduleListener);
    final Module moduleA;
    final Module moduleB;
    {
        final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
        moduleA = moduleModel.newModule("a.iml", StdModuleTypes.JAVA.getId());
        moduleB = moduleModel.newModule("b.iml", StdModuleTypes.JAVA.getId());
        final ModifiableRootModel rootModelA = ModuleRootManager.getInstance(moduleA).getModifiableModel();
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelB.addModuleOrderEntry(moduleA);
        final ContentEntry contentEntryA = rootModelA.addContentEntry(getVirtualFileInTestData("a"));
        contentEntryA.addSourceFolder(getVirtualFileInTestData("a/src"), false);
        final ContentEntry contentEntryB = rootModelB.addContentEntry(getVirtualFileInTestData("b"));
        contentEntryB.addSourceFolder(getVirtualFileInTestData("b/src"), false);
        ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(new ModifiableRootModel[] { rootModelB, rootModelA }, moduleModel));
    }
    final JavaPsiFacade psiManager = getJavaFacade();
    assertNull(psiManager.findClass("j.B", GlobalSearchScope.moduleWithDependenciesScope(moduleA)));
    assertNull(psiManager.findClass("q.A", GlobalSearchScope.moduleScope(moduleB)));
    assertNotNull(psiManager.findClass("q.A", GlobalSearchScope.moduleScope(moduleA)));
    assertNotNull(psiManager.findClass("q.A", GlobalSearchScope.moduleWithDependenciesScope(moduleB)));
    assertNotNull(psiManager.findClass("j.B", GlobalSearchScope.moduleScope(moduleB)));
    assertNotNull(psiManager.findClass("j.B", GlobalSearchScope.moduleWithDependenciesScope(moduleB)));
    moduleManager.disposeModule(moduleB);
    moduleManager.disposeModule(moduleA);
    moduleListener.assertCorrectEvents(new String[][] { { "+b", "+a" }, { "-b" }, { "-a" } });
    connection.disconnect();
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 50 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class MultiModuleEditingTest method testRenaming.

public void testRenaming() throws Exception {
    final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    final Module moduleA;
    final Module moduleB;
    {
        final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
        moduleA = moduleModel.newModule("a.iml", StdModuleTypes.JAVA.getId());
        moduleB = moduleModel.newModule("b.iml", StdModuleTypes.JAVA.getId());
        final Module moduleC = moduleModel.newModule("c.iml", StdModuleTypes.JAVA.getId());
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelB.addModuleOrderEntry(moduleC);
        moduleModel.disposeModule(moduleC);
        ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(new ModifiableRootModel[] { rootModelB }, moduleModel));
    }
    final ModuleRootManager rootManagerB = ModuleRootManager.getInstance(moduleB);
    assertEquals(0, rootManagerB.getDependencies().length);
    final String[] dependencyModuleNames = rootManagerB.getDependencyModuleNames();
    assertEquals(1, dependencyModuleNames.length);
    assertEquals("c", dependencyModuleNames[0]);
    {
        final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
        moduleModel.renameModule(moduleA, "c");
        assertSame(moduleA, moduleModel.findModuleByName("a"));
        assertSame(moduleA, moduleManager.findModuleByName("a"));
        assertEquals("c", moduleModel.getNewName(moduleA));
        assertSame(moduleA, moduleModel.getModuleToBeRenamed("c"));
        ApplicationManager.getApplication().runWriteAction(() -> moduleModel.commit());
    }
    assertEquals(1, rootManagerB.getDependencies().length);
    assertEquals(moduleA, rootManagerB.getDependencies()[0]);
    assertSame(moduleA, moduleManager.findModuleByName("c"));
    assertNull(moduleManager.findModuleByName("a"));
    assertEquals("c", moduleA.getName());
    moduleManager.disposeModule(moduleA);
    moduleManager.disposeModule(moduleB);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Aggregations

ModuleManager (com.intellij.openapi.module.ModuleManager)51 Module (com.intellij.openapi.module.Module)40 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Project (com.intellij.openapi.project.Project)10 NotNull (org.jetbrains.annotations.NotNull)8 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)7 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)7 File (java.io.File)7 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)3 IOException (java.io.IOException)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 Variant (com.android.builder.model.Variant)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 BuildMode (com.android.tools.idea.gradle.util.BuildMode)2 AccessToken (com.intellij.openapi.application.AccessToken)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ContentEntry (com.intellij.openapi.roots.ContentEntry)2 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)2