Search in sources :

Example 11 with BlazeLibrary

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);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModuleEditorImpl(com.google.idea.blaze.base.sync.projectstructure.ModuleEditorImpl) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 12 with BlazeLibrary

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);
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) Library(com.intellij.openapi.roots.libraries.Library)

Example 13 with BlazeLibrary

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();
    }
}
Also used : LibraryKey(com.google.idea.blaze.base.model.LibraryKey) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) DependencyScope(com.intellij.openapi.roots.DependencyScope) Library(com.intellij.openapi.roots.libraries.Library) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) BlazeSyncPlugin(com.google.idea.blaze.base.sync.BlazeSyncPlugin) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryTablesRegistrar(com.intellij.openapi.roots.libraries.LibraryTablesRegistrar) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) IdeModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider) PrintOutput(com.google.idea.blaze.base.scope.output.PrintOutput) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) BaseSdkCompat(com.google.idea.sdkcompat.general.BaseSdkCompat) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) Objects(java.util.Objects) LibraryKey(com.google.idea.blaze.base.model.LibraryKey) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) BlazeSyncPlugin(com.google.idea.blaze.base.sync.BlazeSyncPlugin) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) Library(com.intellij.openapi.roots.libraries.Library) IdeModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider)

Example 14 with BlazeLibrary

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();
    });
}
Also used : BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) IdeModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider)

Example 15 with BlazeLibrary

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);
}
Also used : SyncMode(com.google.idea.blaze.base.sync.BlazeSyncParams.SyncMode) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SdkConstants(com.android.SdkConstants) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) FileCache(com.google.idea.blaze.base.filecache.FileCache) BlazeSyncParams(com.google.idea.blaze.base.sync.BlazeSyncParams) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ImmutableList(com.google.common.collect.ImmutableList) BlazeDataStorage(com.google.idea.blaze.base.sync.data.BlazeDataStorage) Map(java.util.Map) AarLibrary(com.google.idea.blaze.android.sync.model.AarLibrary) Project(com.intellij.openapi.project.Project) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) Nullable(javax.annotation.Nullable) BiMap(com.google.common.collect.BiMap) Files(java.nio.file.Files) BlazeLibraryCollector(com.google.idea.blaze.base.sync.libraries.BlazeLibraryCollector) Collection(java.util.Collection) BlazeImportSettingsManager(com.google.idea.blaze.base.settings.BlazeImportSettingsManager) IOException(java.io.IOException) PrintOutput(com.google.idea.blaze.base.scope.output.PrintOutput) Collectors(java.util.stream.Collectors) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File) BlazeImportSettings(com.google.idea.blaze.base.settings.BlazeImportSettings) HashBiMap(com.google.common.collect.HashBiMap) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) FileCacheSynchronizerTraits(com.google.idea.blaze.base.filecache.FileCacheSynchronizerTraits) Paths(java.nio.file.Paths) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Preconditions(com.google.common.base.Preconditions) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) ZipUtil(com.intellij.util.io.ZipUtil) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) FileCacheSynchronizer(com.google.idea.blaze.base.filecache.FileCacheSynchronizer) AarLibrary(com.google.idea.blaze.android.sync.model.AarLibrary) BlazeLibrary(com.google.idea.blaze.base.model.BlazeLibrary) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) File(java.io.File)

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