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; }
@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; }
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())); }
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(); } } }
@Nullable static DartSdk getSdkByLibrary(@NotNull final Library library) { final VirtualFile[] roots = library.getFiles(OrderRootType.CLASSES); if (roots.length == 1 && DartSdkLibraryPresentationProvider.isDartSdkLibRoot(roots[0])) { final String homePath = roots[0].getParent().getPath(); final String version = StringUtil.notNullize(DartSdkUtil.getSdkVersion(homePath), UNKNOWN_VERSION); return new DartSdk(homePath, version); } return null; }
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)); } }
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; }
@NotNull static VirtualFile[] getLibraryRoots(@NotNull LibraryOrderEntry orderEntry) { Library library = orderEntry.getLibrary(); if (library == null) return VirtualFile.EMPTY_ARRAY; OrderRootType[] rootTypes = LibraryType.DEFAULT_EXTERNAL_ROOT_TYPES; if (library instanceof LibraryEx) { if (((LibraryEx) library).isDisposed()) return VirtualFile.EMPTY_ARRAY; PersistentLibraryKind<?> libKind = ((LibraryEx) library).getKind(); if (libKind != null) { rootTypes = LibraryType.findByKind(libKind).getExternalRootTypes(); } } final ArrayList<VirtualFile> files = new ArrayList<VirtualFile>(); for (OrderRootType rootType : rootTypes) { files.addAll(Arrays.asList(library.getFiles(rootType))); } return VfsUtilCore.toVirtualFileArray(files); }
protected List<File> getImplicitClasspathRoots(@NotNull Module module) { final List<File> toExclude = new ArrayList<File>(); VirtualFile sdkRoot = getSdkRoot(module); if (sdkRoot != null) toExclude.add(VfsUtil.virtualToIoFile(sdkRoot)); ContainerUtil.addIfNotNull(getCommonPluginsDir(module), toExclude); final VirtualFile appRoot = findAppRoot(module); if (appRoot != null) { VirtualFile pluginDir = appRoot.findChild(MvcModuleStructureUtil.PLUGINS_DIRECTORY); if (pluginDir != null) toExclude.add(VfsUtil.virtualToIoFile(pluginDir)); VirtualFile libDir = appRoot.findChild("lib"); if (libDir != null) toExclude.add(VfsUtil.virtualToIoFile(libDir)); } final Library library = MvcModuleStructureUtil.findUserLibrary(module, getUserLibraryName()); if (library != null) { for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) { toExclude.add(VfsUtil.virtualToIoFile(PathUtil.getLocalFile(file))); } } return toExclude; }
private static void removeObsolete( @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport, @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport, @NotNull Set<LibraryDependencyData> toImport, @NotNull ModifiableRootModel moduleRootModel) { Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(); for (OrderEntry entry : moduleRootModel.getOrderEntries()) { if (entry instanceof ModuleLibraryOrderEntryImpl) { Library library = ((ModuleLibraryOrderEntryImpl) entry).getLibrary(); if (library == null) { LOG.warn( "Skipping module-level library entry because it doesn't have backing Library object. Entry: " + entry); continue; } moduleLibraryKey.clear(); for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) { moduleLibraryKey.add(ExternalSystemApiUtil.getLocalFileSystemPath(file)); } LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey); if (existing == null) { moduleRootModel.removeOrderEntry(entry); } else { toImport.remove(existing); } } else if (entry instanceof LibraryOrderEntry) { String libraryName = ((LibraryOrderEntry) entry).getLibraryName(); LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName); if (existing == null) { moduleRootModel.removeOrderEntry(entry); } else { toImport.remove(existing); } } } }
@Override public boolean isSDKLibrary(Library library) { return GriffonLibraryPresentationProvider.isGriffonSdk(library.getFiles(OrderRootType.CLASSES)); }
@Override public boolean isSDKLibrary(Library library) { if (library == null) return false; return LibrariesUtil.getGroovyLibraryHome(library.getFiles(OrderRootType.CLASSES)) != null; }
@Nullable public static List<LocalQuickFix> registerFixes( @NotNull final QuickFixActionRegistrar registrar, @NotNull final PsiReference reference) { final PsiElement psiElement = reference.getElement(); @NonNls final String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText()); Project project = psiElement.getProject(); PsiFile containingFile = psiElement.getContainingFile(); if (containingFile == null) return null; final VirtualFile classVFile = containingFile.getVirtualFile(); if (classVFile == null) return null; final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); final Module currentModule = fileIndex.getModuleForFile(classVFile); if (currentModule == null) return null; final List<LocalQuickFix> providedFixes = findFixes( new Function<MissingDependencyFixProvider, List<LocalQuickFix>>() { @Override public List<LocalQuickFix> fun(MissingDependencyFixProvider provider) { return provider.registerFixes(registrar, reference); } }); if (providedFixes != null) { return providedFixes; } List<LocalQuickFix> result = new ArrayList<LocalQuickFix>(); JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); String fullReferenceText = reference.getCanonicalText(); for (ExternalLibraryResolver resolver : ExternalLibraryResolver.EP_NAME.getExtensions()) { final ExternalClassResolveResult resolveResult = resolver.resolveClass(shortReferenceName, isReferenceToAnnotation(psiElement)); OrderEntryFix fix = null; if (resolveResult != null && psiFacade.findClass( resolveResult.getQualifiedClassName(), currentModule.getModuleWithDependenciesAndLibrariesScope(true)) == null) { fix = new AddExternalLibraryToDependenciesQuickFix( currentModule, resolveResult.getLibrary(), reference, resolveResult.getQualifiedClassName()); } else if (!fullReferenceText.equals(shortReferenceName)) { ExternalLibraryDescriptor descriptor = resolver.resolvePackage(fullReferenceText); if (descriptor != null) { fix = new AddExternalLibraryToDependenciesQuickFix( currentModule, descriptor, reference, null); } } if (fix != null) { registrar.register(fix); result.add(fix); } } if (!result.isEmpty()) { return result; } Set<Object> librariesToAdd = new THashSet<Object>(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject()); PsiClass[] classes = PsiShortNamesCache.getInstance(project) .getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project)); List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes); if (allowedDependencies.isEmpty()) { return result; } classes = allowedDependencies.toArray(new PsiClass[allowedDependencies.size()]); OrderEntryFix moduleDependencyFix = new AddModuleDependencyFix(currentModule, classVFile, classes, reference); final PsiClass[] finalClasses = classes; final OrderEntryFix finalModuleDependencyFix = moduleDependencyFix; final OrderEntryFix providedModuleDependencyFix = provideFix( new Function<MissingDependencyFixProvider, OrderEntryFix>() { @Override public OrderEntryFix fun(MissingDependencyFixProvider provider) { return provider.getAddModuleDependencyFix( reference, finalModuleDependencyFix, currentModule, classVFile, finalClasses); } }); moduleDependencyFix = ObjectUtils.notNull(providedModuleDependencyFix, moduleDependencyFix); registrar.register(moduleDependencyFix); result.add(moduleDependencyFix); for (final PsiClass aClass : classes) { if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue; PsiFile psiFile = aClass.getContainingFile(); if (psiFile == null) continue; VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) continue; ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex(); for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry; final Library library = libraryEntry.getLibrary(); if (library == null) continue; VirtualFile[] files = library.getFiles(OrderRootType.CLASSES); if (files.length == 0) continue; final VirtualFile jar = files[0]; if (jar == null || libraryEntry.isModuleLevel() && !librariesToAdd.add(jar) || !librariesToAdd.add(library)) continue; OrderEntry entryForFile = moduleFileIndex.getOrderEntryForFile(virtualFile); if (entryForFile != null && !(entryForFile instanceof ExportableOrderEntry && ((ExportableOrderEntry) entryForFile).getScope() == DependencyScope.TEST && !ModuleRootManager.getInstance(currentModule) .getFileIndex() .isInTestSourceContent(classVFile))) { continue; } final OrderEntryFix platformFix = new OrderEntryFix() { @Override @NotNull public String getText() { return QuickFixBundle.message( "orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName()); } @Override @NotNull public String getFamilyName() { return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath"); } @Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { return !project.isDisposed() && !currentModule.isDisposed() && libraryEntry.isValid(); } @Override public void invoke( @NotNull final Project project, @Nullable final Editor editor, PsiFile file) { OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule); if (editor != null) { DumbService.getInstance(project) .withAlternativeResolveEnabled( new Runnable() { @Override public void run() { new AddImportAction(project, reference, editor, aClass).execute(); } }); } } }; final OrderEntryFix providedFix = provideFix( new Function<MissingDependencyFixProvider, OrderEntryFix>() { @Override public OrderEntryFix fun(MissingDependencyFixProvider provider) { return provider.getAddLibraryToClasspathFix( reference, platformFix, currentModule, libraryEntry, aClass); } }); final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix); registrar.register(fix); result.add(fix); } } } return result; }