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;
}
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;
}
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;
}
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();
}
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);
}
Aggregations