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()));
}
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);
}
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();
}
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();
}
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;
}
});
}
Aggregations