Search in sources :

Example 71 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-plugins by JetBrains.

the class DesignerTests method configureByFiles.

public static VirtualFile[] configureByFiles(@Nullable VirtualFile rawProjectRoot, VirtualFile[] files, @Nullable VirtualFile[] auxiliaryFiles, Module module, @Nullable TripleFunction<ModifiableRootModel, VirtualFile, List<String>, Void> moduleInitializer) throws Exception {
    return WriteAction.compute(() -> {
        VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
        //noinspection ConstantConditions
        dummyRoot.refresh(false, false);
        final VirtualFile sourceDir = dummyRoot.createChildDirectory(DesignerTests.class, "s");
        assert sourceDir != null;
        final IndexableFileSet indexableFileSet = new IndexableFileSet() {

            @Override
            public boolean isInSet(@NotNull final VirtualFile file) {
                return file.getFileSystem() == sourceDir.getFileSystem();
            }

            @Override
            public void iterateIndexableFilesIn(@NotNull final VirtualFile file, @NotNull final ContentIterator iterator) {
                if (file.isDirectory()) {
                    for (VirtualFile child : file.getChildren()) {
                        iterateIndexableFilesIn(child, iterator);
                    }
                } else {
                    iterator.processFile(file);
                }
            }
        };
        FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, module.getProject());
        Disposer.register(module, new Disposable() {

            @Override
            public void dispose() {
                FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet);
                ApplicationManager.getApplication().runWriteAction(() -> {
                    try {
                        sourceDir.delete(null);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        });
        final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
        VirtualFile[] toFiles = copyFiles(files, sourceDir, rawProjectRoot);
        if (auxiliaryFiles != null) {
            copyFiles(auxiliaryFiles, sourceDir, rawProjectRoot);
        }
        rootModel.addContentEntry(sourceDir).addSourceFolder(sourceDir, false);
        final List<String> libs = new ArrayList<>();
        if (moduleInitializer != null) {
            moduleInitializer.fun(rootModel, sourceDir, libs);
        }
        rootModel.commit();
        for (String path : libs) {
            VirtualFile virtualFile = path.charAt(0) != '/' ? getFile("lib", path) : getFile(path);
            FlexTestUtils.addLibrary(module, path, virtualFile.getParent().getPath(), virtualFile.getName(), null, null);
        }
        return toFiles;
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) ContentIterator(com.intellij.openapi.roots.ContentIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IndexableFileSet(com.intellij.util.indexing.IndexableFileSet) NotNull(org.jetbrains.annotations.NotNull) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel)

Example 72 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class IdeFrameFixture method getSourceFolderRelativePaths.

@NotNull
public Collection<String> getSourceFolderRelativePaths(@NotNull String moduleName, @NotNull final JpsModuleSourceRootType<?> sourceType) {
    final Set<String> paths = new HashSet<>();
    Module module = getModule(moduleName);
    final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
            try {
                for (ContentEntry contentEntry : rootModel.getContentEntries()) {
                    for (SourceFolder folder : contentEntry.getSourceFolders()) {
                        JpsModuleSourceRootType<?> rootType = folder.getRootType();
                        if (rootType.equals(sourceType)) {
                            String path = urlToPath(folder.getUrl());
                            String relativePath = getRelativePath(myProjectPath, new File(toSystemDependentName(path)));
                            paths.add(relativePath);
                        }
                    }
                }
            } finally {
                rootModel.dispose();
            }
        }
    });
    return paths;
}
Also used : ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) GuiTask(org.fest.swing.edt.GuiTask) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) SourceFolder(com.intellij.openapi.roots.SourceFolder) JpsModuleSourceRootType(org.jetbrains.jps.model.module.JpsModuleSourceRootType) ContentEntry(com.intellij.openapi.roots.ContentEntry) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 73 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class TemplateModuleBuilder method fixModuleName.

private void fixModuleName(Module module) {
    List<RunConfiguration> configurations = RunManager.getInstance(module.getProject()).getAllConfigurationsList();
    for (RunConfiguration configuration : configurations) {
        if (configuration instanceof ModuleBasedConfiguration) {
            ((ModuleBasedConfiguration) configuration).getConfigurationModule().setModule(module);
        }
    }
    ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
    for (WizardInputField field : myAdditionalFields) {
        ProjectTemplateParameterFactory factory = WizardInputField.getFactoryById(field.getId());
        factory.applyResult(field.getValue(), model);
    }
    model.commit();
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) ModuleBasedConfiguration(com.intellij.execution.configurations.ModuleBasedConfiguration)

Example 74 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class PlatformProjectConfigurator method configureProject.

@Override
public void configureProject(final Project project, @NotNull final VirtualFile baseDir, final Ref<Module> moduleRef) {
    final ModuleManager moduleManager = ModuleManager.getInstance(project);
    final Module[] modules = moduleManager.getModules();
    if (modules.length == 0) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            // correct module name when opening root of drive as project (RUBY-5181)
            String moduleName = baseDir.getName().replace(":", "");
            String imlName = baseDir.getPath() + "/.idea/" + moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
            ModuleTypeManager instance = ModuleTypeManager.getInstance();
            String id = instance == null ? "unknown" : instance.getDefaultModuleType().getId();
            final Module module = moduleManager.newModule(imlName, id);
            ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
            ModifiableRootModel rootModel = rootManager.getModifiableModel();
            if (rootModel.getContentRoots().length == 0) {
                rootModel.addContentEntry(baseDir);
            }
            rootModel.inheritSdk();
            rootModel.commit();
            moduleRef.set(module);
        });
    }
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModuleTypeManager(com.intellij.openapi.module.ModuleTypeManager) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 75 with ModifiableRootModel

use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.

the class ProjectLibrariesTest method testNestedModelUpdate.

public void testNestedModelUpdate() {
    assertNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
    final ModifiableRootModel moduleModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
    try {
        assertNotNull(moduleModel.findLibraryOrderEntry(myLib));
        final Library.ModifiableModel libModel = myLib.getModifiableModel();
        ApplicationManager.getApplication().runWriteAction(() -> {
            libModel.addRoot(myRoot, OrderRootType.CLASSES);
            libModel.commit();
        });
        assertNotNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
        assertTrue(Arrays.asList(moduleModel.orderEntries().librariesOnly().classes().getRoots()).contains(myRoot));
    } finally {
        ApplicationManager.getApplication().runWriteAction(() -> moduleModel.commit());
    }
    assertNotNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Library(com.intellij.openapi.roots.libraries.Library)

Aggregations

ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)102 Module (com.intellij.openapi.module.Module)44 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 ContentEntry (com.intellij.openapi.roots.ContentEntry)27 File (java.io.File)18 Library (com.intellij.openapi.roots.libraries.Library)15 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)14 ModuleManager (com.intellij.openapi.module.ModuleManager)8 Sdk (com.intellij.openapi.projectRoots.Sdk)8 IOException (java.io.IOException)8 NotNull (org.jetbrains.annotations.NotNull)8 OrderEntry (com.intellij.openapi.roots.OrderEntry)7 Project (com.intellij.openapi.project.Project)6 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)6 Nullable (org.jetbrains.annotations.Nullable)6 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)5 ConfigurationException (com.intellij.openapi.options.ConfigurationException)5 THashMap (gnu.trove.THashMap)5 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)4 AccessToken (com.intellij.openapi.application.AccessToken)4