Search in sources :

Example 41 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project android by JetBrains.

the class ModuleDisposer method disposeModulesAndMarkImlFilesForDeletion.

public void disposeModulesAndMarkImlFilesForDeletion(@NotNull Collection<Module> modules, @NotNull Project project, @NotNull IdeModifiableModelsProvider ideModelsProvider) {
    if (!modules.isEmpty()) {
        ModifiableModuleModel moduleModel = ideModelsProvider.getModifiableModuleModel();
        List<File> imlFilesToRemove = new ArrayList<>();
        for (Module toDispose : modules) {
            File imlFilePath = new File(toSystemDependentName(toDispose.getModuleFilePath()));
            imlFilesToRemove.add(imlFilePath);
            moduleModel.disposeModule(toDispose);
        }
        DisposedModules.getInstance(project).markImlFilesForDeletion(imlFilesToRemove);
    }
}
Also used : ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ArrayList(java.util.ArrayList) Module(com.intellij.openapi.module.Module) File(java.io.File)

Example 42 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class BndProjectImporter method addEntry.

private void addEntry(ModifiableModuleModel moduleModel, LibraryTable.ModifiableModel libraryModel, ModifiableRootModel rootModel, Container dependency, DependencyScope scope) throws IllegalArgumentException {
    File file = dependency.getFile();
    String bsn = dependency.getBundleSymbolicName();
    String version = dependency.getVersion();
    String path = file.getPath();
    if (path.contains(": ")) {
        throw new IllegalArgumentException("Cannot resolve " + bsn + ":" + version + ": " + path);
    }
    if (JDK_DEPENDENCY.equals(bsn)) {
        String name = BND_LIB_PREFIX + bsn + ":" + version;
        if (FileUtil.isAncestor(myWorkspace.getBase(), file, true)) {
            name += "-" + myProject.getName();
        }
        ProjectJdkTable jdkTable = ProjectJdkTable.getInstance();
        Sdk jdk = jdkTable.findJdk(name);
        if (jdk == null) {
            jdk = jdkTable.createSdk(name, JavaSdk.getInstance());
            SdkModificator jdkModel = jdk.getSdkModificator();
            jdkModel.setHomePath(file.getParent());
            jdkModel.setVersionString(version);
            VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(url(file));
            assert root != null : file + " " + file.exists();
            jdkModel.addRoot(root, OrderRootType.CLASSES);
            VirtualFile srcRoot = VirtualFileManager.getInstance().findFileByUrl(url(file) + SRC_ROOT);
            if (srcRoot != null)
                jdkModel.addRoot(srcRoot, OrderRootType.SOURCES);
            jdkModel.commitChanges();
            jdkTable.addJdk(jdk);
        }
        rootModel.setSdk(jdk);
        return;
    }
    ExportableOrderEntry entry;
    switch(dependency.getType()) {
        case PROJECT:
            {
                String name = dependency.getProject().getName();
                Module module = moduleModel.findModuleByName(name);
                if (module == null) {
                    throw new IllegalArgumentException("Unknown module '" + name + "'");
                }
                entry = (ModuleOrderEntry) ContainerUtil.find(rootModel.getOrderEntries(), e -> e instanceof ModuleOrderEntry && ((ModuleOrderEntry) e).getModule() == module);
                if (entry == null) {
                    entry = rootModel.addModuleOrderEntry(module);
                }
                break;
            }
        case REPO:
            {
                String name = BND_LIB_PREFIX + bsn + ":" + version;
                Library library = libraryModel.getLibraryByName(name);
                if (library == null) {
                    library = libraryModel.createLibrary(name);
                }
                Library.ModifiableModel model = library.getModifiableModel();
                for (String url : model.getUrls(OrderRootType.CLASSES)) model.removeRoot(url, OrderRootType.CLASSES);
                for (String url : model.getUrls(OrderRootType.SOURCES)) model.removeRoot(url, OrderRootType.SOURCES);
                model.addRoot(url(file), OrderRootType.CLASSES);
                String srcRoot = mySourcesMap.get(path);
                if (srcRoot != null) {
                    model.addRoot(url(file) + srcRoot, OrderRootType.SOURCES);
                }
                model.commit();
                entry = rootModel.addLibraryEntry(library);
                break;
            }
        case EXTERNAL:
            {
                Library library = rootModel.getModuleLibraryTable().createLibrary(file.getName());
                Library.ModifiableModel model = library.getModifiableModel();
                model.addRoot(url(file), OrderRootType.CLASSES);
                String srcRoot = mySourcesMap.get(path);
                if (srcRoot != null) {
                    model.addRoot(url(file) + srcRoot, OrderRootType.SOURCES);
                }
                model.commit();
                entry = rootModel.findLibraryOrderEntry(library);
                assert entry != null : library;
                break;
            }
        default:
            throw new IllegalArgumentException("Unknown dependency '" + dependency + "' of type " + dependency.getType());
    }
    entry.setScope(scope);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileUtilRt(com.intellij.openapi.util.io.FileUtilRt) OsmorcFacetType(org.osmorc.facet.OsmorcFacetType) CompilerConfiguration(com.intellij.compiler.CompilerConfiguration) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) FacetUtil(com.intellij.facet.impl.FacetUtil) Refreshable(aQute.bnd.service.Refreshable) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) Library(com.intellij.openapi.roots.libraries.Library) Task(com.intellij.openapi.progress.Task) OsmorcFacet(org.osmorc.facet.OsmorcFacet) ZipFile(java.util.zip.ZipFile) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ZipEntry(java.util.zip.ZipEntry) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) JpsJavaCompilerOptions(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerOptions) LanguageLevel(com.intellij.pom.java.LanguageLevel) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) ModifiableModelCommitter(com.intellij.openapi.roots.impl.ModifiableModelCommitter) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) OutputPathType(org.jetbrains.osgi.jps.model.OutputPathType) NotificationType(com.intellij.notification.NotificationType) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProjectJdkTable(com.intellij.openapi.projectRoots.ProjectJdkTable) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) NotificationDisplayType(com.intellij.notification.NotificationDisplayType) java.util(java.util) OsmorcFacetConfiguration(org.osmorc.facet.OsmorcFacetConfiguration) ModuleManager(com.intellij.openapi.module.ModuleManager) ContainerUtil(com.intellij.util.containers.ContainerUtil) Container(aQute.bnd.build.Container) com.intellij.openapi.roots(com.intellij.openapi.roots) Workspace(aQute.bnd.build.Workspace) NotificationGroup(com.intellij.notification.NotificationGroup) ManifestGenerationMode(org.jetbrains.osgi.jps.model.ManifestGenerationMode) OsmorcBundle.message(org.osmorc.i18n.OsmorcBundle.message) Project(aQute.bnd.build.Project) StringUtil(com.intellij.openapi.util.text.StringUtil) JavacConfiguration(com.intellij.compiler.impl.javaCompiler.javac.JavacConfiguration) Key(com.intellij.openapi.util.Key) IOException(java.io.IOException) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) Attrs(aQute.bnd.header.Attrs) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) VfsUtil(com.intellij.openapi.vfs.VfsUtil) ObjectUtils(com.intellij.util.ObjectUtils) PathUtil(com.intellij.util.PathUtil) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) Condition(com.intellij.openapi.util.Condition) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) ProjectJdkTable(com.intellij.openapi.projectRoots.ProjectJdkTable) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 43 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class BndProjectImporter method createProjectStructure.

private void createProjectStructure() {
    if (myProject.isDisposed()) {
        return;
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        LanguageLevel projectLevel = LanguageLevelProjectExtension.getInstance(myProject).getLanguageLevel();
        Map<Project, ModifiableRootModel> rootModels = ContainerUtil.newHashMap();
        ModifiableModuleModel moduleModel = ModuleManager.getInstance(myProject).getModifiableModel();
        LibraryTable.ModifiableModel libraryModel = ProjectLibraryTable.getInstance(myProject).getModifiableModel();
        try {
            for (Project project : myProjects) {
                try {
                    rootModels.put(project, createModule(moduleModel, project, projectLevel));
                } catch (Exception e) {
                    // should not happen, since project.prepare() is already called
                    LOG.error(e);
                }
            }
            for (Project project : myProjects) {
                try {
                    setDependencies(moduleModel, libraryModel, rootModels.get(project), project);
                } catch (Exception e) {
                    // should not happen, since project.prepare() is already called
                    LOG.error(e);
                }
            }
        } finally {
            libraryModel.commit();
            ModifiableModelCommitter.multiCommit(rootModels.values(), moduleModel);
        }
    });
}
Also used : Project(aQute.bnd.build.Project) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) LanguageLevel(com.intellij.pom.java.LanguageLevel) IOException(java.io.IOException)

Example 44 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by StepicOrg.

the class ProjectFilesUtils method getOrCreateSrcDirectory.

@Nullable
public static VirtualFile getOrCreateSrcDirectory(@NotNull Project project, @NotNull StepNode stepNode, boolean refresh, @Nullable ModifiableModuleModel model) {
    VirtualFile baseDir = project.getBaseDir();
    String srcPath = stepNode.getPath() + "/" + EduNames.SRC;
    VirtualFile srcDirectory = baseDir.findFileByRelativePath(srcPath);
    if (srcDirectory == null && !stepNode.getWasDeleted()) {
        srcDirectory = getOrCreateDirectory(baseDir, srcPath);
        if (srcDirectory != null && PluginUtils.isCurrent(IDEA)) {
            boolean modelOwner = model == null;
            if (modelOwner) {
                model = ModuleManager.getInstance(project).getModifiableModel();
            }
            ModifiableModuleModel finalModel = model;
            Application application = ApplicationManager.getApplication();
            application.invokeAndWait(() -> application.runWriteAction(() -> {
                ModuleUtils.createStepModule(project, stepNode, finalModel);
                if (modelOwner) {
                    finalModel.commit();
                }
            }));
            if (refresh) {
                VirtualFileManager.getInstance().syncRefresh();
            }
        }
    }
    return srcDirectory;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) Application(com.intellij.openapi.application.Application) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)44 Module (com.intellij.openapi.module.Module)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 ModuleManager (com.intellij.openapi.module.ModuleManager)10 Project (com.intellij.openapi.project.Project)9 NotNull (org.jetbrains.annotations.NotNull)8 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)5 File (java.io.File)5 Result (com.intellij.openapi.application.Result)4 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 StdModuleTypes (com.intellij.openapi.module.StdModuleTypes)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)3 Nullable (org.jetbrains.annotations.Nullable)3 Project (aQute.bnd.build.Project)2 MoveModulesToSubGroupAction (com.intellij.ide.projectView.actions.MoveModulesToSubGroupAction)2 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)2