Search in sources :

Example 16 with BlazeLibrary

use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.

the class BlazeLibraryCollector method collectLibraries.

/* Collect all libraries of specific types from all library sources. */
private static List<BlazeLibrary> collectLibraries(ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, Function<LibrarySource, List<? extends BlazeLibrary>> librarySelector) {
    // Use set to filter out duplicates.
    Set<BlazeLibrary> result = Sets.newLinkedHashSet();
    List<LibrarySource> librarySources = Lists.newArrayList();
    for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
        LibrarySource librarySource = syncPlugin.getLibrarySource(projectViewSet, blazeProjectData);
        if (librarySource != null) {
            librarySources.add(librarySource);
        }
    }
    for (LibrarySource librarySource : librarySources) {
        result.addAll(librarySelector.apply(librarySource));
    }
    Predicate<BlazeLibrary> libraryFilter = librarySources.stream().map(LibrarySource::getLibraryFilter).filter(Objects::nonNull).reduce(Predicate::and).orElse(o -> true);
    return BlazeLibrarySorter.sortLibraries(result.stream().filter(libraryFilter).collect(Collectors.toList()));
}
Also used : Objects(java.util.Objects) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) BlazeSyncPlugin(com.google.idea.blaze.base.sync.BlazeSyncPlugin)

Example 17 with BlazeLibrary

use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.

the class LibraryEditor method updateLibrary.

/**
 * Updates the library in IntelliJ's project model.
 *
 * <p>Note: Callers of this method must invoke {@link IdeModifiableModelsProvider#commit()} on the
 * passed {@link IdeModifiableModelsProvider} for any changes to take effect. Be aware that {@code
 * commit()} should only be called once after all modifications as frequent calls can be slow.
 *
 * @param project the IntelliJ project
 * @param artifactLocationDecoder a decoder to determine the location of artifacts
 * @param modelsProvider a modifier for IntelliJ's project model which supports quick application
 *     of massive modifications to the project model
 * @param blazeLibrary the library which should be updated in the project context
 */
public static void updateLibrary(Project project, ArtifactLocationDecoder artifactLocationDecoder, IdeModifiableModelsProvider modelsProvider, BlazeLibrary blazeLibrary) {
    String libraryName = blazeLibrary.key.getIntelliJLibraryName();
    Library library = modelsProvider.getLibraryByName(libraryName);
    boolean libraryExists = library != null;
    if (!libraryExists) {
        library = modelsProvider.createLibrary(libraryName);
    }
    Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(library);
    if (libraryExists) {
        for (String url : libraryModel.getUrls(OrderRootType.CLASSES)) {
            libraryModel.removeRoot(url, OrderRootType.CLASSES);
        }
        for (String url : libraryModel.getUrls(OrderRootType.SOURCES)) {
            libraryModel.removeRoot(url, OrderRootType.SOURCES);
        }
    }
    blazeLibrary.modifyLibraryModel(project, artifactLocationDecoder, libraryModel);
}
Also used : BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) Library(com.intellij.openapi.roots.libraries.Library)

Example 18 with BlazeLibrary

use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.

the class LibraryEditor method findLibraries.

private static ImmutableList<Library> findLibraries(Collection<BlazeLibrary> libraries, LibraryTable libraryTable) {
    ImmutableList.Builder<Library> foundLibraries = ImmutableList.builder();
    ImmutableList.Builder<String> missingLibraries = ImmutableList.builder();
    for (BlazeLibrary library : libraries) {
        String libraryName = library.key.getIntelliJLibraryName();
        // This call is slow and causes freezes when done through IdeModifiableModelsProvider.
        Library foundLibrary = libraryTable.getLibraryByName(libraryName);
        if (foundLibrary == null) {
            missingLibraries.add(libraryName);
        } else {
            foundLibraries.add(foundLibrary);
        }
    }
    logMissingLibraries(missingLibraries.build());
    return foundLibraries.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) Library(com.intellij.openapi.roots.libraries.Library)

Example 19 with BlazeLibrary

use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.

the class BlazeAndroidLibrarySource method getLibraries.

@Override
public List<BlazeLibrary> getLibraries() {
    BlazeAndroidSyncData syncData = blazeProjectData.getSyncState().get(BlazeAndroidSyncData.class);
    if (syncData == null) {
        return ImmutableList.of();
    }
    ImmutableList.Builder<BlazeLibrary> libraries = ImmutableList.builder();
    for (BlazeJarLibrary javacJarLibrary : syncData.importResult.javacJarLibraries) {
        libraries.add(javacJarLibrary);
    }
    libraries.addAll(syncData.importResult.aarLibraries.values());
    return libraries.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) BlazeAndroidSyncData(com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData)

Example 20 with BlazeLibrary

use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.

the class BlazeAttachSourceProvider method getActions.

@Override
public Collection<AttachSourcesAction> getActions(List<LibraryOrderEntry> orderEntries, final PsiFile psiFile) {
    Project project = psiFile.getProject();
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return ImmutableList.of();
    }
    List<BlazeLibrary> librariesToAttachSourceTo = Lists.newArrayList();
    for (LibraryOrderEntry orderEntry : orderEntries) {
        Library library = orderEntry.getLibrary();
        if (library == null) {
            continue;
        }
        String name = library.getName();
        if (name == null) {
            continue;
        }
        LibraryKey libraryKey = LibraryKey.fromIntelliJLibraryName(name);
        if (AttachedSourceJarManager.getInstance(project).hasSourceJarAttached(libraryKey)) {
            continue;
        }
        BlazeJarLibrary blazeLibrary = LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
        if (blazeLibrary == null) {
            continue;
        }
        LibraryArtifact libraryArtifact = blazeLibrary.libraryArtifact;
        if (libraryArtifact.getSourceJars().isEmpty()) {
            continue;
        }
        librariesToAttachSourceTo.add(blazeLibrary);
    }
    if (librariesToAttachSourceTo.isEmpty()) {
        return ImmutableList.of();
    }
    // background.
    if (attachAutomatically.getValue()) {
        TransactionGuard.getInstance().submitTransactionLater(project, () -> {
            attachSources(project, blazeProjectData, librariesToAttachSourceTo);
        });
        return ImmutableList.of();
    }
    return ImmutableList.of(new AttachSourcesAction() {

        @Override
        public String getName() {
            return "Attach Blaze Source Jars";
        }

        @Override
        public String getBusyText() {
            return "Attaching source jars...";
        }

        @Override
        public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
            ActionCallback callback = new ActionCallback().doWhenDone(() -> navigateToSource(psiFile));
            Transactions.submitTransaction(project, () -> {
                attachSources(project, blazeProjectData, librariesToAttachSourceTo);
                callback.setDone();
            });
            return callback;
        }
    });
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Project(com.intellij.openapi.project.Project) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) LibraryKey(com.google.idea.blaze.base.model.LibraryKey) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) Library(com.intellij.openapi.roots.libraries.Library) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Aggregations

BlazeLibrary (com.google.idea.blaze.base.model.BlazeLibrary)21 ImmutableList (com.google.common.collect.ImmutableList)8 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)7 BlazeJarLibrary (com.google.idea.blaze.java.sync.model.BlazeJarLibrary)7 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)6 Library (com.intellij.openapi.roots.libraries.Library)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)5 Project (com.intellij.openapi.project.Project)5 File (java.io.File)5 Nullable (javax.annotation.Nullable)5 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)4 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)4 PrintOutput (com.google.idea.blaze.base.scope.output.PrintOutput)4 BlazeDataStorage (com.google.idea.blaze.base.sync.data.BlazeDataStorage)4 BlazeLibraryCollector (com.google.idea.blaze.base.sync.libraries.BlazeLibraryCollector)4 Logger (com.intellij.openapi.diagnostic.Logger)4 Collection (java.util.Collection)4 LibraryArtifact (com.google.idea.blaze.base.ideinfo.LibraryArtifact)3