Search in sources :

Example 66 with ModifiableRootModel

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

the class EclipseLibrariesModificationsTest method doTestExisting.

private void doTestExisting(final String[] classRoots, final String[] sourceRoots, final String[] javadocs) throws Exception {
    final Project project = getProject();
    final String path = project.getBaseDir().getPath() + "/test";
    final Module module = EclipseClasspathTest.setUpModule(path, project);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
        final String parentUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, model.getContentRoots()[0].getParent().getPath());
        final Library library = model.getModuleLibraryTable().getLibraryByName("test.jar");
        final Library.ModifiableModel libModifiableModel = library.getModifiableModel();
        final String[] oldClsRoots = libModifiableModel.getUrls(OrderRootType.CLASSES);
        for (String oldClsRoot : oldClsRoots) {
            libModifiableModel.removeRoot(oldClsRoot, OrderRootType.CLASSES);
        }
        final String[] oldSrcRoots = libModifiableModel.getUrls(OrderRootType.SOURCES);
        for (String oldSrcRoot : oldSrcRoots) {
            libModifiableModel.removeRoot(oldSrcRoot, OrderRootType.SOURCES);
        }
        final String[] oldJdcRoots = libModifiableModel.getUrls(JavadocOrderRootType.getInstance());
        for (String oldJavadocRoot : oldJdcRoots) {
            libModifiableModel.removeRoot(oldJavadocRoot, JavadocOrderRootType.getInstance());
        }
        for (String classRoot : classRoots) {
            libModifiableModel.addRoot(parentUrl + classRoot, OrderRootType.CLASSES);
        }
        for (String sourceRoot : sourceRoots) {
            libModifiableModel.addRoot(parentUrl + sourceRoot, OrderRootType.SOURCES);
        }
        for (String javadocRoot : javadocs) {
            libModifiableModel.addRoot(parentUrl + javadocRoot, JavadocOrderRootType.getInstance());
        }
        libModifiableModel.commit();
        model.commit();
    });
    EclipseClasspathTest.checkModule(project.getBaseDir().getPath() + "/expected", module);
    EclipseEmlTest.checkModule(project.getBaseDir().getPath() + "/expected", module);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Example 67 with ModifiableRootModel

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

the class RepositoryAddLibraryAction method addLibraryToModule.

public static void addLibraryToModule(RepositoryLibraryDescription libraryDescription, Module module) {
    RepositoryLibraryPropertiesModel model = new RepositoryLibraryPropertiesModel(RepositoryUtils.DefaultVersionId, false, false);
    RepositoryLibraryPropertiesDialog dialog = new RepositoryLibraryPropertiesDialog(module.getProject(), model, libraryDescription, false);
    if (!dialog.showAndGet()) {
        return;
    }
    IdeaModifiableModelsProvider modifiableModelsProvider = new IdeaModifiableModelsProvider();
    final ModifiableRootModel modifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
    RepositoryLibrarySupport librarySupport = new RepositoryLibrarySupport(module.getProject(), libraryDescription, model);
    assert modifiableModel != null;
    librarySupport.addSupport(module, modifiableModel, modifiableModelsProvider);
    ApplicationManager.getApplication().runWriteAction(modifiableModel::commit);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) IdeaModifiableModelsProvider(com.intellij.openapi.roots.IdeaModifiableModelsProvider) RepositoryLibraryPropertiesModel(org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesModel) RepositoryLibraryPropertiesDialog(org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesDialog)

Example 68 with ModifiableRootModel

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

the class MavenFrameworkSupportProvider method createConfigurable.

@NotNull
@Override
public FrameworkSupportConfigurable createConfigurable(@NotNull FrameworkSupportModel model) {
    return new FrameworkSupportConfigurable() {

        @Override
        public JComponent getComponent() {
            return new JPanel();
        }

        @Override
        public void addSupport(@NotNull Module module, @NotNull ModifiableRootModel model, @Nullable Library library) {
            VirtualFile[] roots = model.getContentRoots();
            VirtualFile root;
            if (roots.length == 0) {
                VirtualFile moduleFile = module.getModuleFile();
                if (moduleFile != null) {
                    root = moduleFile.getParent();
                    model.addContentEntry(root);
                } else {
                    return;
                }
            } else {
                root = roots[0];
            }
            VirtualFile existingPom = null;
            for (VirtualFile child : root.getChildren()) {
                if (child.getName().startsWith("pom.")) {
                    existingPom = child;
                }
            }
            if (existingPom != null) {
                MavenProjectsManager.getInstance(module.getProject()).addManagedFilesOrUnignore(Collections.singletonList(existingPom));
            } else {
                prepareProjectStructure(model, root);
                new MavenModuleBuilderHelper(new MavenId("groupId", module.getName(), "1.0-SNAPSHOT"), null, null, false, false, null, null, "Add Maven Support").configure(model.getProject(), root, true);
            }
        }
    };
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenId(org.jetbrains.idea.maven.model.MavenId) FrameworkSupportConfigurable(com.intellij.ide.util.frameworkSupport.FrameworkSupportConfigurable) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 69 with ModifiableRootModel

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

the class EclipseImportBuilder method commit.

@Override
public List<Module> commit(final Project project, ModifiableModuleModel model, ModulesProvider modulesProvider, ModifiableArtifactModel artifactModel) {
    final Collection<String> unknownLibraries = new TreeSet<>();
    final Collection<String> unknownJdks = new TreeSet<>();
    final Set<String> refsToModules = new HashSet<>();
    final List<Module> result = new ArrayList<>();
    final Map<Module, Set<String>> module2NatureNames = new HashMap<>();
    try {
        final ModifiableModuleModel moduleModel = model != null ? model : ModuleManager.getInstance(project).getModifiableModel();
        final ModifiableRootModel[] rootModels = new ModifiableRootModel[getParameters().projectsToConvert.size()];
        final Set<File> files = new HashSet<>();
        final Set<String> moduleNames = new THashSet<>(getParameters().projectsToConvert.size());
        for (String path : getParameters().projectsToConvert) {
            String modulesDirectory = getParameters().converterOptions.commonModulesDirectory;
            if (modulesDirectory == null) {
                modulesDirectory = path;
            }
            final String moduleName = EclipseProjectFinder.findProjectName(path);
            moduleNames.add(moduleName);
            final File imlFile = new File(modulesDirectory + File.separator + moduleName + ModuleManagerImpl.IML_EXTENSION);
            if (imlFile.isFile()) {
                files.add(imlFile);
            }
            final File emlFile = new File(modulesDirectory + File.separator + moduleName + EclipseXml.IDEA_SETTINGS_POSTFIX);
            if (emlFile.isFile()) {
                files.add(emlFile);
            }
        }
        if (!files.isEmpty()) {
            final int resultCode = Messages.showYesNoCancelDialog(ApplicationNamesInfo.getInstance().getFullProductName() + " module files found:\n" + StringUtil.join(files, file -> file.getPath(), "\n") + ".\n Would you like to reuse them?", "Module Files Found", Messages.getQuestionIcon());
            if (resultCode != Messages.YES) {
                if (resultCode == Messages.NO) {
                    final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
                    for (File file : files) {
                        final VirtualFile virtualFile = localFileSystem.findFileByIoFile(file);
                        if (virtualFile != null) {
                            ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<Void, IOException>() {

                                @Override
                                public Void compute() throws IOException {
                                    virtualFile.delete(this);
                                    return null;
                                }
                            });
                        } else {
                            FileUtil.delete(file);
                        }
                    }
                } else {
                    return result;
                }
            }
        }
        int idx = 0;
        for (String path : getParameters().projectsToConvert) {
            String modulesDirectory = getParameters().converterOptions.commonModulesDirectory;
            if (modulesDirectory == null) {
                modulesDirectory = path;
            }
            final Module module = moduleModel.newModule(modulesDirectory + "/" + EclipseProjectFinder.findProjectName(path) + ModuleManagerImpl.IML_EXTENSION, StdModuleTypes.JAVA.getId());
            result.add(module);
            final Set<String> natures = collectNatures(path);
            if (natures.size() > 0) {
                module2NatureNames.put(module, natures);
            }
            final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
            rootModels[idx++] = rootModel;
            final File classpathFile = new File(path, EclipseXml.DOT_CLASSPATH_EXT);
            final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, getParameters().projectsToConvert, moduleNames);
            classpathReader.init(rootModel);
            if (classpathFile.exists()) {
                Element classpathElement = JDOMUtil.load(classpathFile);
                classpathReader.readClasspath(rootModel, unknownLibraries, unknownJdks, refsToModules, getParameters().converterOptions.testPattern, classpathElement);
            } else {
                EclipseClasspathReader.setOutputUrl(rootModel, path + "/bin");
            }
            ClasspathStorage.setStorageType(rootModel, getParameters().linkConverted ? JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID : ClassPathStorageUtil.DEFAULT_STORAGE);
            if (model != null) {
                ApplicationManager.getApplication().runWriteAction(() -> rootModel.commit());
            }
        }
        if (model == null) {
            ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(rootModels, moduleModel));
        }
    } catch (Exception e) {
        LOG.error(e);
    }
    scheduleNaturesImporting(project, module2NatureNames);
    createEclipseLibrary(project, unknownLibraries, IdeaXml.ECLIPSE_LIBRARY);
    StringBuilder message = new StringBuilder();
    refsToModules.removeAll(getParameters().existingModuleNames);
    for (String path : getParameters().projectsToConvert) {
        final String projectName = EclipseProjectFinder.findProjectName(path);
        if (projectName != null) {
            refsToModules.remove(projectName);
            getParameters().existingModuleNames.add(projectName);
        }
    }
    if (!refsToModules.isEmpty()) {
        message.append("Unknown modules detected");
        for (String module : refsToModules) {
            message.append("\n").append(module);
        }
    }
    if (!unknownJdks.isEmpty()) {
        if (message.length() > 0) {
            message.append("\nand jdks");
        } else {
            message.append("Imported project refers to unknown jdks");
        }
        for (String unknownJdk : unknownJdks) {
            message.append("\n").append(unknownJdk);
        }
    }
    if (!unknownLibraries.isEmpty()) {
        final StringBuilder buf = new StringBuilder();
        buf.append("<html><body>");
        buf.append(EclipseBundle.message("eclipse.import.warning.undefinded.libraries"));
        for (String name : unknownLibraries) {
            buf.append("<br>").append(name);
        }
        if (model == null) {
            buf.append("<br><b>Please export Eclipse user libraries and import them now from resulted .userlibraries file</b>");
            buf.append("</body></html>");
            final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false) {

                @Override
                public boolean isFileSelectable(VirtualFile file) {
                    return super.isFileSelectable(file) && Comparing.strEqual(file.getExtension(), "userlibraries");
                }
            };
            descriptor.setDescription(buf.toString());
            descriptor.setTitle(getTitle());
            final VirtualFile selectedFile = FileChooser.chooseFile(descriptor, project, project.getBaseDir());
            if (selectedFile != null) {
                try {
                    EclipseUserLibrariesHelper.readProjectLibrariesContent(selectedFile, project, unknownLibraries);
                } catch (Exception e) {
                    LOG.error(e);
                }
            }
        }
    }
    if (message.length() > 0) {
        Messages.showErrorDialog(project, message.toString(), getTitle());
    }
    return result;
}
Also used : ClassPathStorageUtil(com.intellij.openapi.roots.impl.storage.ClassPathStorageUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) IdeaXml(org.jetbrains.idea.eclipse.IdeaXml) ClasspathStorage(com.intellij.openapi.roots.impl.storage.ClasspathStorage) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) EclipseClasspathReader(org.jetbrains.idea.eclipse.conversion.EclipseClasspathReader) EclipseIcons(icons.EclipseIcons) Library(com.intellij.openapi.roots.libraries.Library) ProjectImportBuilder(com.intellij.projectImport.ProjectImportBuilder) Task(com.intellij.openapi.progress.Task) JDOMException(org.jdom.JDOMException) EclipseUserLibrariesHelper(org.jetbrains.idea.eclipse.conversion.EclipseUserLibrariesHelper) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ApplicationNamesInfo(com.intellij.openapi.application.ApplicationNamesInfo) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) Messages(com.intellij.openapi.ui.Messages) ModuleManagerImpl(com.intellij.openapi.module.impl.ModuleManagerImpl) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) Notifications(com.intellij.notification.Notifications) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) LibraryTablesRegistrar(com.intellij.openapi.roots.libraries.LibraryTablesRegistrar) ModifiableModelCommitter(com.intellij.openapi.roots.impl.ModifiableModelCommitter) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) NotificationType(com.intellij.notification.NotificationType) Notification(com.intellij.notification.Notification) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) java.util(java.util) ModuleManager(com.intellij.openapi.module.ModuleManager) Comparing(com.intellij.openapi.util.Comparing) StartupManager(com.intellij.openapi.startup.StartupManager) JDOMUtil(com.intellij.openapi.util.JDOMUtil) EclipseXml(org.jetbrains.idea.eclipse.EclipseXml) Project(com.intellij.openapi.project.Project) JpsEclipseClasspathSerializer(org.jetbrains.jps.eclipse.model.JpsEclipseClasspathSerializer) StringUtil(com.intellij.openapi.util.text.StringUtil) IOException(java.io.IOException) File(java.io.File) EclipseProjectFinder(org.jetbrains.idea.eclipse.EclipseProjectFinder) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ProjectMacrosUtil(com.intellij.openapi.project.impl.ProjectMacrosUtil) ModifiableArtifactModel(com.intellij.packaging.artifacts.ModifiableArtifactModel) Element(org.jdom.Element) FileChooser(com.intellij.openapi.fileChooser.FileChooser) EclipseBundle(org.jetbrains.idea.eclipse.EclipseBundle) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) Element(org.jdom.Element) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) EclipseClasspathReader(org.jetbrains.idea.eclipse.conversion.EclipseClasspathReader) THashSet(gnu.trove.THashSet) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 70 with ModifiableRootModel

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

the class MavenProjectsManagerTest method testForceReimport.

public void testForceReimport() throws Exception {
    createProjectPom("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<dependencies>" + "  <dependency>" + "    <groupId>junit</groupId>" + "    <artifactId>junit</artifactId>" + "    <version>4.0</version>" + "  </dependency>" + "</dependencies>");
    importProject();
    assertModules("project");
    createProjectSubDir("src/main/java");
    ApplicationManager.getApplication().runWriteAction(() -> {
        ModifiableRootModel model = ModuleRootManager.getInstance(getModule("project")).getModifiableModel();
        for (OrderEntry each : model.getOrderEntries()) {
            if (each instanceof LibraryOrderEntry && MavenRootModelAdapter.isMavenLibrary(((LibraryOrderEntry) each).getLibrary())) {
                model.removeOrderEntry(each);
            }
        }
        model.commit();
    });
    assertSources("project");
    assertModuleLibDeps("project");
    myProjectsManager.forceUpdateAllProjectsOrFindAllAvailablePomFiles();
    waitForReadingCompletion();
    myProjectsManager.waitForResolvingCompletion();
    myProjectsManager.performScheduledImportInTests();
    assertSources("project", "src/main/java");
    assertModuleLibDeps("project", "Maven: junit:junit:4.0");
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry)

Aggregations

ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)101 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