use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.
the class ProjectUpdateSyncTask method updateProjectStructure.
private void updateProjectStructure(BlazeContext context, BlazeImportSettings importSettings, ProjectViewSet projectViewSet, BlazeVersionData blazeVersionData, DirectoryStructure directoryStructure, BlazeProjectData newBlazeProjectData, @Nullable BlazeProjectData oldBlazeProjectData) {
for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
syncPlugin.updateProjectSdk(project, context, projectViewSet, blazeVersionData, newBlazeProjectData);
}
ModuleEditorImpl moduleEditor = ModuleEditorProvider.getInstance().getModuleEditor(project, importSettings);
ModuleType<?> workspaceModuleType = null;
for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
workspaceModuleType = syncPlugin.getWorkspaceModuleType(newBlazeProjectData.getWorkspaceLanguageSettings().getWorkspaceType());
if (workspaceModuleType != null) {
break;
}
}
if (workspaceModuleType == null) {
workspaceModuleType = ModuleTypeManager.getInstance().getDefaultModuleType();
}
Module workspaceModule = moduleEditor.createModule(BlazeDataStorage.WORKSPACE_MODULE_NAME, workspaceModuleType);
ModifiableRootModel workspaceModifiableModel = moduleEditor.editModule(workspaceModule);
ContentEntryEditor.createContentEntries(project, workspaceRoot, projectViewSet, newBlazeProjectData, directoryStructure, workspaceModifiableModel);
List<BlazeLibrary> libraries = BlazeLibraryCollector.getLibraries(projectViewSet, newBlazeProjectData);
LibraryEditor.updateProjectLibraries(project, context, projectViewSet, newBlazeProjectData, libraries);
LibraryEditor.configureDependencies(workspaceModifiableModel, libraries);
for (BlazeSyncPlugin blazeSyncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
blazeSyncPlugin.updateProjectStructure(project, context, workspaceRoot, projectViewSet, newBlazeProjectData, oldBlazeProjectData, moduleEditor, workspaceModule, workspaceModifiableModel);
}
createProjectDataDirectoryModule(moduleEditor, new File(importSettings.getProjectDataDirectory()), workspaceModuleType);
moduleEditor.commitWithGc(context);
}
use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.
the class LibraryEditor method configureDependencies.
/**
* Configures the passed libraries as dependencies for the given root in IntelliJ's project model.
* Libraries which don't exist in the project model will be ignored.
*
* <p>Note: Callers of this method must invoke {@code commit()} on the passed {@link
* ModifiableRootModel} or on higher-level model providers 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 modifiableRootModel a modifier for a specific root in IntelliJ's project model
* @param libraries the libraries to add as dependencies
*/
public static void configureDependencies(ModifiableRootModel modifiableRootModel, Collection<BlazeLibrary> libraries) {
LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(modifiableRootModel.getProject());
ImmutableList<Library> foundLibraries = findLibraries(libraries, libraryTable);
// Add the libraries in a batch operation as adding them one after the other is not performant.
modifiableRootModel.addLibraryEntries(foundLibraries, DependencyScope.COMPILE, /* exported= */
false);
}
use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.
the class LibraryEditor method updateProjectLibraries.
public static void updateProjectLibraries(Project project, BlazeContext context, ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, Collection<BlazeLibrary> libraries) {
Set<LibraryKey> intelliJLibraryState = Sets.newHashSet();
IdeModifiableModelsProvider modelsProvider = BaseSdkCompat.createModifiableModelsProvider(project);
for (Library library : modelsProvider.getAllLibraries()) {
String name = library.getName();
if (name != null) {
intelliJLibraryState.add(LibraryKey.fromIntelliJLibraryName(name));
}
}
context.output(PrintOutput.log(String.format("Workspace has %d libraries", libraries.size())));
try {
for (BlazeLibrary library : libraries) {
updateLibrary(project, blazeProjectData.getArtifactLocationDecoder(), modelsProvider, library);
}
// Garbage collect unused libraries
List<LibrarySource> librarySources = Lists.newArrayList();
for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
LibrarySource librarySource = syncPlugin.getLibrarySource(projectViewSet, blazeProjectData);
if (librarySource != null) {
librarySources.add(librarySource);
}
}
Predicate<Library> gcRetentionFilter = librarySources.stream().map(LibrarySource::getGcRetentionFilter).filter(Objects::nonNull).reduce(Predicate::or).orElse(o -> false);
Set<LibraryKey> newLibraryKeys = libraries.stream().map((blazeLibrary) -> blazeLibrary.key).collect(Collectors.toSet());
for (LibraryKey libraryKey : intelliJLibraryState) {
String libraryIntellijName = libraryKey.getIntelliJLibraryName();
if (!newLibraryKeys.contains(libraryKey)) {
Library library = modelsProvider.getLibraryByName(libraryIntellijName);
if (!gcRetentionFilter.test(library)) {
if (library != null) {
modelsProvider.removeLibrary(library);
}
}
}
}
} finally {
modelsProvider.commit();
}
}
use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.
the class BlazeAttachSourceProvider method attachSources.
private static void attachSources(Project project, BlazeProjectData blazeProjectData, Collection<BlazeLibrary> librariesToAttachSourceTo) {
ApplicationManager.getApplication().runWriteAction(() -> {
IdeModifiableModelsProvider modelsProvider = BaseSdkCompat.createModifiableModelsProvider(project);
for (BlazeLibrary blazeLibrary : librariesToAttachSourceTo) {
// Make sure we don't do it twice
if (AttachedSourceJarManager.getInstance(project).hasSourceJarAttached(blazeLibrary.key)) {
continue;
}
AttachedSourceJarManager.getInstance(project).setHasSourceJarAttached(blazeLibrary.key, true);
LibraryEditor.updateLibrary(project, blazeProjectData.getArtifactLocationDecoder(), modelsProvider, blazeLibrary);
}
modelsProvider.commit();
});
}
use of com.google.idea.blaze.base.model.BlazeLibrary in project intellij by bazelbuild.
the class UnpackedAars method onSync.
void onSync(BlazeContext context, ProjectViewSet projectViewSet, BlazeProjectData projectData, BlazeSyncParams.SyncMode syncMode) {
Collection<BlazeLibrary> libraries = BlazeLibraryCollector.getLibraries(projectViewSet, projectData);
boolean fullRefresh = syncMode == SyncMode.FULL;
boolean removeMissingFiles = syncMode == SyncMode.INCREMENTAL;
if (!enabled || fullRefresh) {
clearCache();
}
if (!enabled) {
return;
}
List<AarLibrary> aarLibraries = libraries.stream().filter(library -> library instanceof AarLibrary).map(library -> (AarLibrary) library).collect(Collectors.toList());
ArtifactLocationDecoder artifactLocationDecoder = projectData.artifactLocationDecoder;
BiMap<File, String> sourceAarFileToCacheKey = HashBiMap.create(aarLibraries.size());
BiMap<File, String> sourceJarFileToCacheKey = HashBiMap.create(aarLibraries.size());
for (AarLibrary library : aarLibraries) {
File aarFile = artifactLocationDecoder.decode(library.aarArtifact);
String cacheKey = cacheKeyForAar(aarFile);
sourceAarFileToCacheKey.put(aarFile, cacheKey);
File jarFile = artifactLocationDecoder.decode(library.libraryArtifact.jarForIntellijLibrary());
// Use the aar key for the jar as well.
sourceJarFileToCacheKey.put(jarFile, cacheKey);
}
this.aarTraits = new AarTraits(cacheDir, sourceAarFileToCacheKey);
this.jarTraits = new JarTraits(cacheDir, sourceJarFileToCacheKey);
refresh(context, removeMissingFiles);
}
Aggregations