Search in sources :

Example 66 with Library

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());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryPathType(com.intellij.openapi.externalSystem.model.project.LibraryPathType) Library(com.intellij.openapi.roots.libraries.Library)

Example 67 with Library

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);
}
Also used : OrderRootType(com.intellij.openapi.roots.OrderRootType) Library(com.intellij.openapi.roots.libraries.Library)

Example 68 with Library

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);
            }
        }
    }
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) IdeUIModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeUIModifiableModelsProvider) Library(com.intellij.openapi.roots.libraries.Library)

Example 69 with Library

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);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleLibraryOrderEntryImpl(com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl) Library(com.intellij.openapi.roots.libraries.Library)

Example 70 with Library

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);
        }
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)240 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)58 VirtualFile (com.intellij.openapi.vfs.VirtualFile)58 Module (com.intellij.openapi.module.Module)41 NotNull (org.jetbrains.annotations.NotNull)31 File (java.io.File)29 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)25 Nullable (org.jetbrains.annotations.Nullable)25 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)23 Project (com.intellij.openapi.project.Project)22 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)20 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)10 THashSet (gnu.trove.THashSet)9 IOException (java.io.IOException)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 LibrariesContainer (com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer)7