@NotNull public static String getLibraryName(@NotNull Library library) { final String result = library.getName(); if (result != null) { return result; } String[] endingsToStrip = {"/", "!", ".jar"}; StringBuilder buffer = new StringBuilder(); for (OrderRootType type : OrderRootType.getAllTypes()) { for (String url : library.getUrls(type)) { buffer.setLength(0); buffer.append(url); for (String ending : endingsToStrip) { if (buffer.lastIndexOf(ending) == buffer.length() - ending.length()) { buffer.setLength(buffer.length() - ending.length()); } } final int i = buffer.lastIndexOf(PATH_SEPARATOR); if (i < 0 || i >= buffer.length() - 1) { continue; } String candidate = buffer.substring(i + 1); if (!StringUtil.isEmpty(candidate)) { return candidate; } } } assert false; return "unknown-lib"; }
@Override @NotNull public Collection<AbstractTreeNode> getChildren() { Module module = getValue().getModule(); final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>(); final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries(); for (final OrderEntry orderEntry : orderEntries) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; final Library library = libraryOrderEntry.getLibrary(); if (library == null) { continue; } final String libraryName = library.getName(); if (libraryName == null || libraryName.length() == 0) { addLibraryChildren(libraryOrderEntry, children, getProject(), this); } else { children.add( new NamedLibraryElementNode( getProject(), new NamedLibraryElement(module, libraryOrderEntry), getSettings())); } } else if (orderEntry instanceof SdkOrderEntry) { final SdkOrderEntry sdkOrderEntry = (SdkOrderEntry) orderEntry; final Sdk jdk = sdkOrderEntry.getSdk(); if (jdk != null) { children.add( new NamedLibraryElementNode( getProject(), new NamedLibraryElement(module, sdkOrderEntry), getSettings())); } } } return children; }
@NotNull public static Library updatePackagesLibraryRoots( @NotNull final Project project, @NotNull final DartLibInfo libInfo) { final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project); final Library existingLibrary = projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME); final Library library = existingLibrary != null ? existingLibrary : ApplicationManager.getApplication() .runWriteAction( new Computable<Library>() { @Override public Library compute() { final LibraryTableBase.ModifiableModel libTableModel = ProjectLibraryTable.getInstance(project).getModifiableModel(); final Library lib = libTableModel.createLibrary( DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME, DartPackagesLibraryType.LIBRARY_KIND); libTableModel.commit(); return lib; } }); final String[] existingUrls = library.getUrls(OrderRootType.CLASSES); final Collection<String> libRootUrls = libInfo.getLibRootUrls(); if ((!libInfo.isProjectWithoutPubspec() && isBrokenPackageMap(((LibraryEx) library).getProperties())) || existingUrls.length != libRootUrls.size() || !libRootUrls.containsAll(Arrays.asList(existingUrls))) { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx) library.getModifiableModel(); for (String url : existingUrls) { model.removeRoot(url, OrderRootType.CLASSES); } for (String url : libRootUrls) { model.addRoot(url, OrderRootType.CLASSES); } final DartPackagesLibraryProperties libraryProperties = new DartPackagesLibraryProperties(); libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap()); model.setProperties(libraryProperties); model.commit(); } }); } return library; }
private static List<? extends Library> getNotAddedLibraries( @NotNull final ArtifactEditorContext context, @NotNull Artifact artifact, List<Library> librariesList) { final Set<VirtualFile> roots = new HashSet<VirtualFile>(); ArtifactUtil.processPackagingElements( artifact, PackagingElementFactoryImpl.FILE_COPY_ELEMENT_TYPE, new Processor<FileCopyPackagingElement>() { public boolean process(FileCopyPackagingElement fileCopyPackagingElement) { final VirtualFile root = fileCopyPackagingElement.getLibraryRoot(); if (root != null) { roots.add(root); } return true; } }, context, true); final List<Library> result = new ArrayList<Library>(); for (Library library : librariesList) { if (!roots.containsAll(Arrays.asList(library.getFiles(OrderRootType.CLASSES)))) { result.add(library); } } return result; }
@Override @Nullable protected ClasspathTableItem<?> createTableItem(final Library item) { // clear invalid order entry corresponding to added library if any final ModifiableRootModel rootModel = myClasspathPanel.getRootModel(); final OrderEntry[] orderEntries = rootModel.getOrderEntries(); for (OrderEntry orderEntry : orderEntries) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; if (item.equals(libraryOrderEntry.getLibrary())) { return ClasspathTableItem.createItem(libraryOrderEntry, myContext); } String name = item.getName(); if (name != null && name.equals(libraryOrderEntry.getLibraryName())) { if (orderEntry.isValid()) { Messages.showErrorDialog( ProjectBundle.message("classpath.message.library.already.added", item.getName()), ProjectBundle.message("classpath.title.adding.dependency")); return null; } else { rootModel.removeOrderEntry(orderEntry); } } } } final LibraryOrderEntry orderEntry = rootModel.addLibraryEntry(item); DependencyScope defaultScope = getDefaultScope(item); if (defaultScope != null) { orderEntry.setScope(defaultScope); } return ClasspathTableItem.createItem(orderEntry, myContext); }
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; } } } } }
public boolean tryToSetUpGroovyFacetOnTheFly(final Module module) { final Project project = module.getProject(); final Library[] libraries = getAllSDKLibraries(project); if (libraries.length > 0) { final Library library = libraries[0]; int result = Messages.showOkCancelDialog( GroovyBundle.message( "groovy.like.library.found.text", module.getName(), library.getName(), getSDKLibVersion(library)), GroovyBundle.message("groovy.like.library.found"), JetgroovyIcons.Groovy.Groovy_32x32); if (result == Messages.OK) { AccessToken accessToken = WriteAction.start(); try { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]); LibrariesUtil.placeEntryToCorrectPlace(model, entry); model.commit(); return true; } finally { accessToken.finish(); } } } return false; }
private void doEdit() { final OrderEntry entry = getSelectedEntry(); if (!(entry instanceof LibraryOrderEntry)) return; final Library library = ((LibraryOrderEntry) entry).getLibrary(); if (library == null) { return; } final LibraryTable table = library.getTable(); final String tableLevel = table != null ? table.getTableLevel() : LibraryTableImplUtil.MODULE_LEVEL; final LibraryTablePresentation presentation = LibraryEditingUtil.getLibraryTablePresentation(getProject(), tableLevel); final LibraryTableModifiableModelProvider provider = getModifiableModelProvider(tableLevel); EditExistingLibraryDialog dialog = EditExistingLibraryDialog.createDialog( this, provider, library, myState.getProject(), presentation, getStructureConfigurableContext()); dialog.setContextModule(getRootModel().getModule()); dialog.show(); myEntryTable.repaint(); ModuleStructureConfigurable.getInstance(myState.getProject()).getTree().repaint(); }
private void removeLibraryIfNeeded() { ApplicationManager.getApplication().assertIsDispatchThread(); ModifiableModelsProvider modelsProvider = ModifiableModelsProvider.SERVICE.getInstance(); ModifiableRootModel model = modelsProvider.getModuleModifiableModel(myModule); LibraryOrderEntry goLibraryEntry = OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName()); if (goLibraryEntry != null) { ApplicationManager.getApplication() .runWriteAction( () -> { Library library = goLibraryEntry.getLibrary(); if (library != null) { LibraryTable table = library.getTable(); if (table != null) { table.removeLibrary(library); model.removeOrderEntry(goLibraryEntry); modelsProvider.commitModuleModifiableModel(model); } } else { modelsProvider.disposeModuleModifiableModel(model); } }); } else { ApplicationManager.getApplication() .runWriteAction(() -> modelsProvider.disposeModuleModifiableModel(model)); } }
private void appendChosenAnnotationsRoot( @NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) { if (entry instanceof LibraryOrderEntry) { Library library = ((LibraryOrderEntry) entry).getLibrary(); LOG.assertTrue(library != null); final ModifiableRootModel rootModel = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final Library.ModifiableModel model = library.getModifiableModel(); model.addRoot(vFile, AnnotationOrderRootType.getInstance()); model.commit(); rootModel.commit(); } else if (entry instanceof ModuleSourceOrderEntry) { final ModifiableRootModel model = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final JavaModuleExternalPaths extension = model.getModuleExtension(JavaModuleExternalPaths.class); extension.setExternalAnnotationUrls( ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl())); model.commit(); } else if (entry instanceof JdkOrderEntry) { final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator(); sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance()); sdkModificator.commitChanges(); } myExternalAnnotations.clear(); }
public void assertProjectLibraries(String... expectedNames) { List<String> actualNames = new ArrayList<String>(); for (Library each : ProjectLibraryTable.getInstance(myProject).getLibraries()) { String name = each.getName(); actualNames.add(name == null ? "<unnamed>" : name); } assertUnorderedElementsAreEqual(actualNames, expectedNames); }
@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(); }
@Nullable public static DartSdk findDartSdkAmongGlobalLibs(final Library[] globalLibraries) { for (final Library library : globalLibraries) { if (DART_SDK_GLOBAL_LIB_NAME.equals(library.getName())) { return getSdkByLibrary(library); } } return null; }
@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(); }
@NotNull private String getLibraryName(@NotNull Library library) { final LibrariesModifiableModel model = getLibrariesModifiableModel(library.getTable()); if (model != null) { if (model.hasLibraryEditor(library)) { return model.getLibraryEditor(library).getName(); } } return library.getName(); }
public ActionCallback select( @NotNull LibraryOrderEntry libraryOrderEntry, final boolean requestFocus) { final Library lib = libraryOrderEntry.getLibrary(); if (lib == null || lib.getTable() == null) { return selectOrderEntry(libraryOrderEntry.getOwnerModule(), libraryOrderEntry); } Place place = createPlaceFor(getConfigurableFor(lib)); place.putPath(BaseStructureConfigurable.TREE_NAME, libraryOrderEntry.getLibraryName()); return navigateTo(place, requestFocus); }
@Nullable private static Library getAutoLibrary(SModuleReference reference, Project project) { String libraryName = LIBRARY_PREFIX + reference.getModuleName() + AUTO_SUFFIX; for (Library lib : ModuleLibrariesUtil.getLibraries(reference, project)) { if (lib.getName().equals(libraryName)) { return lib; } } return null; }
private void syncExistingAndRemoveObsolete( @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport, @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport, @NotNull Set<LibraryDependencyData> toImport, @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap, @NotNull ModifiableRootModel moduleRootModel, boolean hasUnresolvedLibraries) { for (OrderEntry entry : moduleRootModel.getOrderEntries()) { if (entry instanceof ModuleLibraryOrderEntryImpl) { ModuleLibraryOrderEntryImpl moduleLibraryOrderEntry = (ModuleLibraryOrderEntryImpl) entry; Library library = moduleLibraryOrderEntry.getLibrary(); if (library == null) { LOG.warn( "Skipping module-level library entry because it doesn't have backing Library object. Entry: " + entry); continue; } final VirtualFile[] libraryFiles = library.getFiles(OrderRootType.CLASSES); final Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(libraryFiles.length); for (VirtualFile file : libraryFiles) { moduleLibraryKey.add( ExternalSystemApiUtil.getLocalFileSystemPath(file) + moduleLibraryOrderEntry.getScope().name()); } LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey); if (existing == null || !StringUtil.equals(existing.getInternalName(), library.getName())) { moduleRootModel.removeOrderEntry(entry); } else { orderEntryDataMap.put(entry, existing); syncExistingLibraryDependency( modelsProvider, existing, library, moduleRootModel, moduleLibraryOrderEntry.getOwnerModule()); toImport.remove(existing); } } else if (entry instanceof LibraryOrderEntry) { LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry; String libraryName = libraryOrderEntry.getLibraryName(); LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName + libraryOrderEntry.getScope().name()); if (existing != null) { toImport.remove(existing); orderEntryDataMap.put(entry, existing); } else if (!hasUnresolvedLibraries) { // There is a possible case that a project has been successfully imported from external // model and after // that network/repo goes down. We don't want to drop existing binary mappings then. moduleRootModel.removeOrderEntry(entry); } } } }
private static void processLibrariesAndJpsPlugins( final File jarFile, final File zipFile, final String pluginName, final Set<Library> libs, Map<Module, String> jpsModules, final ProgressIndicator progressIndicator) throws IOException { if (FileUtil.ensureCanCreateFile(zipFile)) { ZipOutputStream zos = null; try { zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); addStructure(pluginName, zos); addStructure(pluginName + "/" + MIDDLE_LIB_DIR, zos); final String entryName = pluginName + JAR_EXTENSION; ZipUtil.addFileToZip( zos, jarFile, getZipPath(pluginName, entryName), new HashSet<String>(), createFilter(progressIndicator, FileTypeManager.getInstance())); for (Map.Entry<Module, String> entry : jpsModules.entrySet()) { File jpsPluginJar = jarModulesOutput(Collections.singleton(entry.getKey()), null, null); ZipUtil.addFileToZip( zos, jpsPluginJar, getZipPath(pluginName, entry.getValue()), null, null); } Set<String> usedJarNames = new HashSet<String>(); usedJarNames.add(entryName); Set<VirtualFile> jarredVirtualFiles = new HashSet<VirtualFile>(); for (Library library : libs) { final VirtualFile[] files = library.getFiles(OrderRootType.CLASSES); for (VirtualFile virtualFile : files) { if (jarredVirtualFiles.add(virtualFile)) { if (virtualFile.getFileSystem() instanceof JarFileSystem) { addLibraryJar( virtualFile, zipFile, pluginName, zos, usedJarNames, progressIndicator); } else { makeAndAddLibraryJar( virtualFile, zipFile, pluginName, zos, usedJarNames, progressIndicator, library.getName()); } } } } } finally { if (zos != null) zos.close(); } } }
private static void fillGlobalLibraries(List<GlobalLibrary> globals) { final Iterator<Library> iterator = LibraryTablesRegistrar.getInstance().getLibraryTable().getLibraryIterator(); while (iterator.hasNext()) { Library library = iterator.next(); final String name = library.getName(); if (name != null) { final List<String> paths = convertToLocalPaths(library.getFiles(OrderRootType.CLASSES)); globals.add(new GlobalLibrary(name, paths)); } } }
private void addLibs(SolutionDescriptor solutionDescriptor) { // adding libraries for (OrderEntry e : ModuleRootManager.getInstance(myModule).getOrderEntries()) { if (!(e instanceof LibraryOrderEntry)) continue; LibraryOrderEntry loe = (LibraryOrderEntry) e; if (!loe.isModuleLevel()) continue; Library library = loe.getLibrary(); if (library == null) continue; StubSolutionIdea.addModelRoots(solutionDescriptor, library.getFiles(OrderRootType.CLASSES)); } }
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; }
@NotNull public static Set<SModuleReference> getModules(SRepository repository, final Library library) { if (!ModuleLibraryType.isModuleLibrary(library)) { return Collections.emptySet(); } final Set<SModuleReference> modules = new HashSet<SModuleReference>(); final Set<IFile> moduleXmls = new HashSet<IFile>(); for (VirtualFile file : library.getFiles(ModuleXmlRootDetector.MPS_MODULE_XML)) { moduleXmls.add(VirtualFileUtils.toIFile(file)); } repository .getModelAccess() .runReadAction( new Runnable() { @Override public void run() { for (IFile moduleDescriptor : moduleXmls) { SModule moduleByFile = ModuleFileTracker.getInstance().getModuleByFile(moduleDescriptor); if (moduleByFile != null) { modules.add(moduleByFile.getModuleReference()); } } } }); return modules; }
public static void showDialogAndAddLibraryToDependencies( final Library library, final Project project, boolean allowEmptySelection) { for (ProjectStructureValidator validator : EP_NAME.getExtensions()) { if (validator.addLibraryToDependencies(library, project, allowEmptySelection)) { return; } } final ModuleStructureConfigurable moduleStructureConfigurable = ModuleStructureConfigurable.getInstance(project); final List<Module> modules = LibraryEditingUtil.getSuitableModules( moduleStructureConfigurable, ((LibraryEx) library).getKind(), library); if (modules.isEmpty()) return; final ChooseModulesDialog dlg = new ChooseModulesDialog( moduleStructureConfigurable.getProject(), modules, ProjectBundle.message("choose.modules.dialog.title"), ProjectBundle.message("choose.modules.dialog.description", library.getName())); if (dlg.showAndGet()) { final List<Module> chosenModules = dlg.getChosenElements(); for (Module module : chosenModules) { moduleStructureConfigurable.addLibraryOrderEntry(module, library); } } }
private static void fillLibrary( @NotNull Library library, @NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) { ApplicationManager.getApplication().assertWriteAccessAllowed(); Library.ModifiableModel libraryModel = library.getModifiableModel(); for (String root : libraryModel.getUrls(OrderRootType.CLASSES)) { libraryModel.removeRoot(root, OrderRootType.CLASSES); } for (String root : libraryModel.getUrls(OrderRootType.SOURCES)) { libraryModel.removeRoot(root, OrderRootType.SOURCES); } for (VirtualFile libraryRoot : libraryRoots) { libraryModel.addRoot( libraryRoot, OrderRootType .CLASSES); // in order to consider GOPATH as library and show it in Ext. Libraries libraryModel.addRoot( libraryRoot, OrderRootType.SOURCES); // in order to find usages inside GOPATH } for (VirtualFile root : exclusions) { ((LibraryEx.ModifiableModelEx) libraryModel).addExcludedRoot(root.getUrl()); } libraryModel.commit(); }
public BaseLibrariesConfigurable getConfigurableFor(final Library library) { if (LibraryTablesRegistrar.PROJECT_LEVEL.equals(library.getTable().getTableLevel())) { return myProjectLibrariesConfig; } else { return myGlobalLibrariesConfig; } }
private String getDUnitPath() { LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project); for (Library lib : projectLibraryTable.getLibraries()) { String name = lib.getName(); if (name != null) { if (name.contains("d-unit-")) { VirtualFile[] files = lib.getFiles(OrderRootType.CLASSES); if (files.length > 0) { return dubImportPath(files[0].getCanonicalPath()); } } } } return null; }
private static boolean hasUserPaths(OrderRootType rootType, Library library, String pathToJar) { String[] sources = library.getUrls(rootType); for (String each : sources) { if (!FileUtil.startsWith(each, pathToJar)) return true; } return false; }
private void importMissing( @NotNull Set<LibraryDependencyData> toImport, @NotNull ModifiableRootModel moduleRootModel, @NotNull LibraryTable moduleLibraryTable, @NotNull LibraryTable libraryTable, @NotNull Module module) { for (LibraryDependencyData dependencyData : toImport) { LibraryData libraryData = dependencyData.getTarget(); if (libraryData.isUnresolved()) { continue; } switch (dependencyData.getLevel()) { case MODULE: @SuppressWarnings("ConstantConditions") Library moduleLib = moduleLibraryTable.createLibrary(dependencyData.getName()); Library.ModifiableModel libModel = moduleLib.getModifiableModel(); try { Map<OrderRootType, Collection<File>> files = myLibraryManager.prepareLibraryFiles(libraryData); myLibraryManager.registerPaths(files, libModel, dependencyData.getName()); } finally { libModel.commit(); } break; case PROJECT: final Library projectLib = libraryTable.getLibraryByName(dependencyData.getName()); if (projectLib == null) { assert false; continue; } LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib); LOG.info( String.format( "Adding library dependency '%s' to module '%s'", projectLib.getName(), module.getName())); orderEntry.setExported(dependencyData.isExported()); orderEntry.setScope(dependencyData.getScope()); LOG.info( String.format( "Configuring library dependency '%s' of module '%s' to be%s exported and have scope %s", projectLib.getName(), module.getName(), dependencyData.isExported() ? " not" : "", dependencyData.getScope())); } } }
private static boolean hasModule(Library library, SModule module) { if (!isSuitableModule(module) || !ModuleLibraryType.isModuleLibrary(library)) { return false; } Solution solution = (Solution) module; return Arrays.asList(library.getFiles(ModuleXmlRootDetector.MPS_MODULE_XML)) .contains(VirtualFileUtils.getOrCreateVirtualFile(solution.getDescriptorFile())); }