public void unConfigureModule(@NotNull ModifiableRootModel model) { for (OrderEntry orderEntry : model.getOrderEntries()) { if (orderEntry instanceof LibraryOrderEntry) { LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; Library library = libraryOrderEntry.getLibrary(); if (library != null) { String libraryName = library.getName(); if (libraryName != null && libraryName.equals(this.libraryName)) { // Dispose attached roots Library.ModifiableModel modifiableModel = library.getModifiableModel(); for (String rootUrl : library.getRootProvider().getUrls(OrderRootType.CLASSES)) { modifiableModel.removeRoot(rootUrl, OrderRootType.CLASSES); } modifiableModel.commit(); model.getModuleLibraryTable().removeLibrary(library); break; } } } } }
@Override protected void addJUnitDefaultLib( ModifiableRootModel rootModel, String junitName, ExpandMacroToPathMap macroMap) { final Library library = rootModel.getModuleLibraryTable().getModifiableModel().createLibrary(junitName); final Library.ModifiableModel modifiableModel = library.getModifiableModel(); modifiableModel.addRoot(getJunitClsUrl(junitName.contains("4")), OrderRootType.CLASSES); modifiableModel.commit(); }
protected void createLibrary(ModifiableRootModel model, final String name, final String path) { final Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary(name).getModifiableModel(); final VirtualFile home = LocalFileSystem.getInstance().refreshAndFindFileByPath(PathManager.getHomePath() + path); modifiableModel.addRoot(home, OrderRootType.CLASSES); modifiableModel.commit(); }
@Override public void configureModule( @NotNull Module module, @NotNull ModifiableRootModel model, @Nullable ContentEntry contentEntry) { Library library = model.getModuleLibraryTable().createLibrary(libraryName); Library.ModifiableModel modifiableModel = library.getModifiableModel(); modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot(libraryFile), libraryRootType); modifiableModel.commit(); }
protected Module createModule(final String name) { final Module module = super.createModule(name); final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); final LibraryTable.ModifiableModel modifiableModel = model.getModuleLibraryTable().getModifiableModel(); final Library library = modifiableModel.createLibrary("junit"); final Library.ModifiableModel libModel = library.getModifiableModel(); libModel.addRoot( VfsUtil.getUrlForLibraryRoot(new File(PathUtil.getJarPathForClass(Before.class))), OrderRootType.CLASSES); libModel.commit(); model.commit(); return module; }
@Override protected void addModuleLibrary( ModifiableRootModel rootModel, Element element, boolean exported, String libName, String url, String srcUrl, ExpandMacroToPathMap macroMap) { final Library library = rootModel.getModuleLibraryTable().getModifiableModel().createLibrary(libName); final Library.ModifiableModel modifiableModel = library.getModifiableModel(); modifiableModel.addRoot(url, OrderRootType.CLASSES); if (srcUrl != null) { modifiableModel.addRoot(srcUrl, OrderRootType.SOURCES); } EJavadocUtil.appendJavadocRoots(element, rootModel, myCurrentRoots, modifiableModel); modifiableModel.commit(); setLibraryEntryExported(rootModel, exported, library); }
public Library createOrGetXtendJavaLibrary( final ModifiableRootModel rootModel, final Module module) { LibraryTablesRegistrar _instance = LibraryTablesRegistrar.getInstance(); Project _project = module.getProject(); LibraryTable _libraryTable = _instance.getLibraryTable(_project); Library[] _libraries = _libraryTable.getLibraries(); LibraryTablesRegistrar _instance_1 = LibraryTablesRegistrar.getInstance(); LibraryTable _libraryTable_1 = _instance_1.getLibraryTable(); Library[] _libraries_1 = _libraryTable_1.getLibraries(); final Iterable<Library> libraryTable = Iterables.<Library>concat( ((Iterable<? extends Library>) Conversions.doWrapArray(_libraries)), ((Iterable<? extends Library>) Conversions.doWrapArray(_libraries_1))); final Function1<Library, Boolean> _function = new Function1<Library, Boolean>() { @Override public Boolean apply(final Library it) { String _name = it.getName(); boolean _startsWith = false; if (_name != null) { _startsWith = _name.startsWith(XtendLibraryDescription.XTEND_LIBRARY_NAME); } return Boolean.valueOf(_startsWith); } }; final Iterable<Library> xtendLibs = IterableExtensions.<Library>filter(libraryTable, _function); boolean _isEmpty = IterableExtensions.isEmpty(xtendLibs); boolean _not = (!_isEmpty); if (_not) { final Function1<Library, Boolean> _function_1 = new Function1<Library, Boolean>() { @Override public Boolean apply(final Library it) { String _name = it.getName(); return Boolean.valueOf( Objects.equal(_name, XtendLibraryDescription.XTEND_LIBRARY_NAME)); } }; boolean _exists = IterableExtensions.<Library>exists(xtendLibs, _function_1); if (_exists) { final Function1<Library, Boolean> _function_2 = new Function1<Library, Boolean>() { @Override public Boolean apply(final Library it) { String _name = it.getName(); return Boolean.valueOf( Objects.equal(_name, XtendLibraryDescription.XTEND_LIBRARY_NAME)); } }; return IterableExtensions.<Library>findFirst(xtendLibs, _function_2); } else { return IterableExtensions.<Library>head(xtendLibs); } } else { final NewLibraryConfiguration libDescr = this.xtendLibDescr.createLibraryDescription(); LibraryTable _moduleLibraryTable = rootModel.getModuleLibraryTable(); final LibraryTable.ModifiableModel model = _moduleLibraryTable.getModifiableModel(); String _defaultLibraryName = libDescr.getDefaultLibraryName(); final Library createdLib = model.createLibrary(_defaultLibraryName); final Library.ModifiableModel libModel = createdLib.getModifiableModel(); HashMap<OrderRootType, List<String>> _libraryRoots = this.xtendLibDescr.libraryRoots(); final Procedure2<OrderRootType, List<String>> _function_3 = new Procedure2<OrderRootType, List<String>>() { @Override public void apply(final OrderRootType type, final List<String> roots) { final Procedure1<String> _function = new Procedure1<String>() { @Override public void apply(final String it) { libModel.addRoot(it, type); } }; IterableExtensions.<String>forEach(roots, _function); } }; MapExtensions.<OrderRootType, List<String>>forEach(_libraryRoots, _function_3); libModel.commit(); model.commit(); return createdLib; } }
@Override protected Map<OrderEntry, OrderAware> importData( @NotNull final Collection<DataNode<LibraryDependencyData>> nodesToImport, @NotNull final Module module, @NotNull final IdeModifiableModelsProvider modelsProvider) { // The general idea is to import all external project library dependencies and module libraries // which don't present at the // ide side yet and remove all project library dependencies and module libraries which present // at the ide but not at // the given collection. // The trick is that we should perform module settings modification inside try/finally block // against target root model. // That means that we need to prepare all necessary data, obtain a model and modify it as // necessary. final Map<Set<String> /* library paths */, LibraryDependencyData> moduleLibrariesToImport = ContainerUtilRt.newHashMap(); final Map<String /* library name + scope */, LibraryDependencyData> projectLibrariesToImport = ContainerUtilRt.newHashMap(); final Set<LibraryDependencyData> toImport = ContainerUtilRt.newLinkedHashSet(); final Map<OrderEntry, OrderAware> orderEntryDataMap = ContainerUtil.newLinkedHashMap(); boolean hasUnresolved = false; for (DataNode<LibraryDependencyData> dependencyNode : nodesToImport) { LibraryDependencyData dependencyData = dependencyNode.getData(); LibraryData libraryData = dependencyData.getTarget(); hasUnresolved |= libraryData.isUnresolved(); switch (dependencyData.getLevel()) { case MODULE: Set<String> paths = ContainerUtilRt.newHashSet(); for (String path : libraryData.getPaths(LibraryPathType.BINARY)) { paths.add( ExternalSystemApiUtil.toCanonicalPath(path) + dependencyData.getScope().name()); } moduleLibrariesToImport.put(paths, dependencyData); toImport.add(dependencyData); break; case PROJECT: projectLibrariesToImport.put( libraryData.getInternalName() + dependencyData.getScope().name(), dependencyData); toImport.add(dependencyData); } } final boolean finalHasUnresolved = hasUnresolved; final ModifiableRootModel modifiableRootModel = modelsProvider.getModifiableRootModel(module); LibraryTable moduleLibraryTable = modifiableRootModel.getModuleLibraryTable(); syncExistingAndRemoveObsolete( modelsProvider, moduleLibrariesToImport, projectLibrariesToImport, toImport, orderEntryDataMap, modifiableRootModel, finalHasUnresolved); // Import missing library dependencies. if (!toImport.isEmpty()) { importMissing( modelsProvider, toImport, orderEntryDataMap, modifiableRootModel, moduleLibraryTable, module); } return orderEntryDataMap; }
public void testModuleLibraries() throws Exception { File moduleFile = new File(getTestRoot(), "test.iml"); Module module = createModule(moduleFile); final ModuleRootManagerImpl moduleRootManager = (ModuleRootManagerImpl) ModuleRootManager.getInstance(module); final ModifiableRootModel rootModel = moduleRootManager.getModifiableModel(); final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable(); final Library unnamedLibrary = moduleLibraryTable.createLibrary(); final File unnamedLibClasses = new File(getTestRoot(), "unnamedLibClasses"); final VirtualFile unnamedLibClassesRoot = LocalFileSystem.getInstance().findFileByIoFile(unnamedLibClasses); final Library.ModifiableModel libraryModifyableModel = unnamedLibrary.getModifiableModel(); libraryModifyableModel.addRoot(unnamedLibClassesRoot.getUrl(), OrderRootType.CLASSES); final Library namedLibrary = moduleLibraryTable.createLibrary("namedLibrary"); final File namedLibClasses = new File(getTestRoot(), "namedLibClasses"); final VirtualFile namedLibClassesRoot = LocalFileSystem.getInstance().findFileByIoFile(namedLibClasses); final Library.ModifiableModel namedLibraryModel = namedLibrary.getModifiableModel(); namedLibraryModel.addRoot(namedLibClassesRoot.getUrl(), OrderRootType.CLASSES); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { libraryModifyableModel.commit(); namedLibraryModel.commit(); } }); final Iterator libraryIterator = moduleLibraryTable.getLibraryIterator(); assertEquals(libraryIterator.next(), unnamedLibrary); assertEquals(libraryIterator.next(), namedLibrary); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { rootModel.commit(); } }); final Element element = new Element("root"); moduleRootManager.getState().writeExternal(element); assertElementEquals( element, "<root inherit-compiler-output=\"true\">" + "<exclude-output />" + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />" + "<orderEntry type=\"module-library\">" + "<library>" + "<CLASSES><root url=\"file://$MODULE_DIR$/unnamedLibClasses\" /></CLASSES>" + "<JAVADOC />" + "<SOURCES />" + "</library>" + "</orderEntry>" + "<orderEntry type=\"module-library\">" + "<library name=\"namedLibrary\">" + "<CLASSES><root url=\"file://$MODULE_DIR$/namedLibClasses\" /></CLASSES>" + "<JAVADOC />" + "<SOURCES />" + "</library>" + "</orderEntry>" + "</root>", module); }