use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.
the class LibraryDataService method syncPaths.
private void syncPaths(@NotNull final LibraryData externalLibrary, @NotNull final Library ideLibrary, @NotNull final IdeModifiableModelsProvider modelsProvider) {
if (externalLibrary.isUnresolved()) {
return;
}
final Map<OrderRootType, Set<String>> toRemove = ContainerUtilRt.newHashMap();
final Map<OrderRootType, Set<String>> toAdd = ContainerUtilRt.newHashMap();
for (LibraryPathType pathType : LibraryPathType.values()) {
OrderRootType ideType = myLibraryPathTypeMapper.map(pathType);
HashSet<String> toAddPerType = ContainerUtilRt.newHashSet(externalLibrary.getPaths(pathType));
toAdd.put(ideType, toAddPerType);
HashSet<String> toRemovePerType = ContainerUtilRt.newHashSet();
toRemove.put(ideType, toRemovePerType);
for (VirtualFile ideFile : ideLibrary.getFiles(ideType)) {
String idePath = ExternalSystemApiUtil.getLocalFileSystemPath(ideFile);
if (!toAddPerType.remove(idePath)) {
toRemovePerType.add(ideFile.getUrl());
}
}
}
if (toRemove.isEmpty() && toAdd.isEmpty()) {
return;
}
final Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(ideLibrary);
for (Map.Entry<OrderRootType, Set<String>> entry : toRemove.entrySet()) {
for (String path : entry.getValue()) {
libraryModel.removeRoot(path, entry.getKey());
}
}
for (Map.Entry<OrderRootType, Set<String>> entry : toAdd.entrySet()) {
Map<OrderRootType, Collection<File>> roots = ContainerUtilRt.newHashMap();
roots.put(entry.getKey(), ContainerUtil.map(entry.getValue(), PATH_TO_FILE));
registerPaths(externalLibrary.isUnresolved(), roots, libraryModel, externalLibrary.getInternalName());
}
}
use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.
the class LibraryDataService method importLibrary.
private void importLibrary(@NotNull final LibraryData toImport, @NotNull final IdeModifiableModelsProvider modelsProvider) {
Map<OrderRootType, Collection<File>> libraryFiles = prepareLibraryFiles(toImport);
final String libraryName = toImport.getInternalName();
Library library = modelsProvider.getLibraryByName(libraryName);
if (library != null) {
syncPaths(toImport, library, modelsProvider);
return;
}
library = modelsProvider.createLibrary(libraryName);
final Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(library);
registerPaths(toImport.isUnresolved(), libraryFiles, libraryModel, libraryName);
}
use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.
the class LibraryDataService method postProcess.
/**
* Remove orphan project libraries during postprocess phase (after execution of LibraryDependencyDataService#import)
* in order to use LibraryDataService.isOrphanProjectLibrary method properly
*/
@Override
public void postProcess(@NotNull Collection<DataNode<LibraryData>> toImport, @Nullable ProjectData projectData, @NotNull Project project, @NotNull IdeModifiableModelsProvider modelsProvider) {
if (projectData == null)
return;
// and hence #isOrphanProjectLibrary() method will work incorrectly
if (modelsProvider instanceof IdeUIModifiableModelsProvider)
return;
final List<Library> orphanIdeLibraries = ContainerUtil.newSmartList();
final LibraryTable.ModifiableModel librariesModel = modelsProvider.getModifiableProjectLibrariesModel();
for (Library library : librariesModel.getLibraries()) {
if (!ExternalSystemApiUtil.isExternalSystemLibrary(library, projectData.getOwner()))
continue;
if (isOrphanProjectLibrary(library, modelsProvider)) {
orphanIdeLibraries.add(library);
}
}
for (Library library : orphanIdeLibraries) {
String libraryName = library.getName();
if (libraryName != null) {
Library libraryToRemove = librariesModel.getLibraryByName(libraryName);
if (libraryToRemove != null) {
librariesModel.removeLibrary(libraryToRemove);
}
}
}
}
use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.
the class LibraryDependencyDataService method syncExistingAndRemoveObsolete.
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(StringUtil.nullize(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);
libraryOrderEntry.setExported(existing.isExported());
libraryOrderEntry.setScope(existing.getScope());
} 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);
}
}
}
}
use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.
the class LibraryDependencyDataService method importMissing.
private void importMissing(@NotNull IdeModifiableModelsProvider modelsProvider, @NotNull Set<LibraryDependencyData> toImport, @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap, @NotNull ModifiableRootModel moduleRootModel, @NotNull LibraryTable moduleLibraryTable, @NotNull Module module) {
for (final LibraryDependencyData dependencyData : toImport) {
final LibraryData libraryData = dependencyData.getTarget();
final String libraryName = libraryData.getInternalName();
switch(dependencyData.getLevel()) {
case MODULE:
final Library moduleLib;
if (libraryName.isEmpty()) {
moduleLib = moduleLibraryTable.createLibrary();
} else {
moduleLib = moduleLibraryTable.createLibrary(libraryName);
}
final LibraryOrderEntry existingLibraryDependency = syncExistingLibraryDependency(modelsProvider, dependencyData, moduleLib, moduleRootModel, module);
orderEntryDataMap.put(existingLibraryDependency, dependencyData);
break;
case PROJECT:
final Library projectLib = modelsProvider.getLibraryByName(libraryName);
if (projectLib == null) {
final LibraryOrderEntry existingProjectLibraryDependency = syncExistingLibraryDependency(modelsProvider, dependencyData, moduleLibraryTable.createLibrary(libraryName), moduleRootModel, module);
orderEntryDataMap.put(existingProjectLibraryDependency, dependencyData);
break;
}
LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
orderEntryDataMap.put(orderEntry, dependencyData);
setLibraryScope(orderEntry, projectLib, module, dependencyData);
}
}
}
Aggregations