Search in sources :

Example 1 with ArtifactLocationDecoder

use of com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder in project intellij by bazelbuild.

the class BlazeClassJarProviderIntegrationTest method doSetup.

@Before
public void doSetup() {
    module = testFixture.getModule();
    ArtifactLocationDecoder decoder = (location) -> new File("/src", location.getExecutionRootRelativePath());
    BlazeProjectData blazeProjectData = MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(buildTargetMap()).setArtifactLocationDecoder(decoder).build();
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(blazeProjectData));
    classJarProvider = new BlazeClassJarProvider(getProject());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) RunWith(org.junit.runner.RunWith) ClassJarProvider(com.android.tools.idea.model.ClassJarProvider) AndroidIntegrationTestSetupRule(com.google.idea.blaze.android.AndroidIntegrationTestSetupRule) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) Kind(com.google.idea.blaze.base.model.primitives.Kind) BlazeIntegrationTestCase(com.google.idea.blaze.base.BlazeIntegrationTestCase) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ModifiableFacetModel(com.intellij.facet.ModifiableFacetModel) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) Module(com.intellij.openapi.module.Module) Before(org.junit.Before) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) ResourceClassRegistry(com.android.tools.idea.res.ResourceClassRegistry) AndroidResourceModule(com.google.idea.blaze.android.sync.model.AndroidResourceModule) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) IOException(java.io.IOException) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) FacetManager(com.intellij.facet.FacetManager) AndroidFacetConfiguration(org.jetbrains.android.facet.AndroidFacetConfiguration) BlazeProjectDataManager(com.google.idea.blaze.base.sync.data.BlazeProjectDataManager) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) Label(com.google.idea.blaze.base.model.primitives.Label) ApplicationManager(com.intellij.openapi.application.ApplicationManager) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) TargetMapBuilder(com.google.idea.blaze.base.ideinfo.TargetMapBuilder) Mockito.mock(org.mockito.Mockito.mock) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) Before(org.junit.Before)

Example 2 with ArtifactLocationDecoder

use of com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder in project intellij by bazelbuild.

the class BlazeBuildSystemServiceTest method createMockBlazeProjectData.

private BlazeProjectData createMockBlazeProjectData() {
    TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel(Label.create("//foo:bar")).setBuildFile(ArtifactLocation.builder().setRelativePath("foo/BUILD").build()).build()).build();
    ArtifactLocationDecoder decoder = (location) -> new File("/", location.getRelativePath());
    return MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).setArtifactLocationDecoder(decoder).build();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectViewManager(com.google.idea.blaze.base.projectview.ProjectViewManager) MockModule(com.intellij.mock.MockModule) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) BlazeBuildService(com.google.idea.blaze.base.actions.BlazeBuildService) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) VirtualFileSystemProvider(com.google.idea.blaze.base.io.VirtualFileSystemProvider) Module(com.intellij.openapi.module.Module) Blaze(com.google.idea.blaze.base.settings.Blaze) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) AndroidResourceModule(com.google.idea.blaze.android.sync.model.AndroidResourceModule) BuildSystemService(com.android.tools.idea.project.BuildSystemService) MockVirtualFile(com.intellij.mock.MockVirtualFile) BlazeProjectDataManager(com.google.idea.blaze.base.sync.data.BlazeProjectDataManager) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) TargetMapBuilder(com.google.idea.blaze.base.ideinfo.TargetMapBuilder) Mockito.mock(org.mockito.Mockito.mock) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) LazyRangeMarkerFactory(com.intellij.openapi.editor.LazyRangeMarkerFactory) RunWith(org.junit.runner.RunWith) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ArgumentCaptor(org.mockito.ArgumentCaptor) BlazeSyncManager(com.google.idea.blaze.base.sync.BlazeSyncManager) PsiElement(com.intellij.psi.PsiElement) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) PsiFile(com.intellij.psi.PsiFile) WorkspacePathResolver(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver) LazyRangeMarkerFactoryImpl(com.intellij.openapi.editor.impl.LazyRangeMarkerFactoryImpl) Nullable(javax.annotation.Nullable) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) BlazeTestCase(com.google.idea.blaze.base.BlazeTestCase) BlazeImportSettingsManager(com.google.idea.blaze.base.settings.BlazeImportSettingsManager) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Maps(com.google.common.collect.Maps) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) BlazeImportSettings(com.google.idea.blaze.base.settings.BlazeImportSettings) ExtensionPointName(com.intellij.openapi.extensions.ExtensionPointName) TempFileSystem(com.intellij.openapi.vfs.ex.temp.TempFileSystem) Label(com.google.idea.blaze.base.model.primitives.Label) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap)

Example 3 with ArtifactLocationDecoder

use of com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder in project intellij by bazelbuild.

the class BlazeAndroidWorkspaceImporterTest method initTest.

@Override
protected void initTest(Container applicationServices, Container projectServices) {
    MockExperimentService mockExperimentService = new MockExperimentService();
    applicationServices.register(ExperimentService.class, mockExperimentService);
    BlazeExecutor blazeExecutor = new MockBlazeExecutor();
    applicationServices.register(BlazeExecutor.class, blazeExecutor);
    projectServices.register(BlazeImportSettingsManager.class, new BlazeImportSettingsManager());
    BlazeImportSettingsManager.getInstance(getProject()).setImportSettings(DUMMY_IMPORT_SETTINGS);
    MockFileOperationProvider mockFileOperationProvider = new MockFileOperationProvider();
    applicationServices.register(FileOperationProvider.class, mockFileOperationProvider);
    context = new BlazeContext();
    context.addOutputSink(IssueOutput.class, errorCollector);
    registerExtensionPoint(BlazeJavaSyncAugmenter.EP_NAME, BlazeJavaSyncAugmenter.class);
    // For importJavaWorkspace.
    applicationServices.register(JavaSourcePackageReader.class, new JavaSourcePackageReader() {

        @Nullable
        @Override
        public String getDeclaredPackageOfJavaFile(BlazeContext context, ArtifactLocationDecoder artifactLocationDecoder, SourceArtifact sourceArtifact) {
            return null;
        }
    });
    applicationServices.register(PackageManifestReader.class, new PackageManifestReader());
    applicationServices.register(PrefetchService.class, new MockPrefetchService());
    registerExtensionPoint(JavaLikeLanguage.EP_NAME, JavaLikeLanguage.class).registerExtension(new JavaLikeLanguage.Java());
}
Also used : MockExperimentService(com.google.idea.common.experiments.MockExperimentService) BlazeImportSettingsManager(com.google.idea.blaze.base.settings.BlazeImportSettingsManager) JavaLikeLanguage(com.google.idea.blaze.java.sync.source.JavaLikeLanguage) PackageManifestReader(com.google.idea.blaze.java.sync.source.PackageManifestReader) MockBlazeExecutor(com.google.idea.blaze.base.async.executor.MockBlazeExecutor) SourceArtifact(com.google.idea.blaze.java.sync.source.SourceArtifact) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) MockPrefetchService(com.google.idea.blaze.base.prefetch.MockPrefetchService) MockBlazeExecutor(com.google.idea.blaze.base.async.executor.MockBlazeExecutor) BlazeExecutor(com.google.idea.blaze.base.async.executor.BlazeExecutor) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) JavaSourcePackageReader(com.google.idea.blaze.java.sync.source.JavaSourcePackageReader) Nullable(javax.annotation.Nullable)

Example 4 with ArtifactLocationDecoder

use of com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder in project intellij by bazelbuild.

the class BlazeClassJarProvider method findModuleClassFile.

@Override
@Nullable
public VirtualFile findModuleClassFile(String className, Module module) {
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return null;
    }
    TargetMap targetMap = blazeProjectData.targetMap;
    ArtifactLocationDecoder decoder = blazeProjectData.artifactLocationDecoder;
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo target = blazeProjectData.targetMap.get(registry.getTargetKey(module));
    if (target == null || target.javaIdeInfo == null) {
        return null;
    }
    // As a potential optimization, we could choose an arbitrary android_binary target
    // that depends on the library to provide a single complete resource jar,
    // instead of having to rely on dynamic class generation.
    // TODO: benchmark to see if optimization is worthwhile.
    String classNamePath = className.replace('.', File.separatorChar) + SdkConstants.DOT_CLASS;
    List<LibraryArtifact> jarsToSearch = Lists.newArrayList(target.javaIdeInfo.jars);
    jarsToSearch.addAll(TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key).stream().map(targetMap::get).filter(Objects::nonNull).flatMap(BlazeClassJarProvider::getNonResourceJars).collect(Collectors.toList()));
    List<File> missingClassJars = Lists.newArrayList();
    for (LibraryArtifact jar : jarsToSearch) {
        if (jar.classJar == null || jar.classJar.isSource()) {
            continue;
        }
        File classJarFile = decoder.decode(jar.classJar);
        VirtualFile classJarVF = VirtualFileSystemProvider.getInstance().getSystem().findFileByIoFile(classJarFile);
        if (classJarVF == null) {
            if (classJarFile.exists()) {
                missingClassJars.add(classJarFile);
            }
            continue;
        }
        VirtualFile classFile = findClassInJar(classJarVF, classNamePath);
        if (classFile != null) {
            return classFile;
        }
    }
    maybeRefreshJars(missingClassJars, pendingJarsRefresh);
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Nullable(javax.annotation.Nullable)

Example 5 with ArtifactLocationDecoder

use of com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder in project intellij by bazelbuild.

the class BlazeClassJarProvider method getModuleExternalLibraries.

@Override
public List<File> getModuleExternalLibraries(Module module) {
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return ImmutableList.of();
    }
    TargetMap targetMap = blazeProjectData.targetMap;
    ArtifactLocationDecoder decoder = blazeProjectData.artifactLocationDecoder;
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo target = targetMap.get(registry.getTargetKey(module));
    if (target == null) {
        return ImmutableList.of();
    }
    AppResourceRepository repository = AppResourceRepository.getOrCreateInstance(module);
    ImmutableList.Builder<File> results = ImmutableList.builder();
    for (TargetKey dependencyTargetKey : TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key)) {
        TargetIdeInfo dependencyTarget = targetMap.get(dependencyTargetKey);
        if (dependencyTarget == null) {
            continue;
        }
        // Add all import jars as external libraries.
        JavaIdeInfo javaIdeInfo = dependencyTarget.javaIdeInfo;
        if (javaIdeInfo != null) {
            for (LibraryArtifact jar : javaIdeInfo.jars) {
                if (jar.classJar != null && jar.classJar.isSource()) {
                    results.add(decoder.decode(jar.classJar));
                }
            }
        }
        // Tell ResourceClassRegistry which repository contains our resources and the java packages of
        // the resources that we're interested in.
        // When the class loader tries to load a custom view, and the view references resource
        // classes, layoutlib will ask the class loader for these resource classes.
        // If these resource classes are in a separate jar from the target (i.e., in a dependency),
        // then offering their jars will lead to a conflict in the resource IDs.
        // So instead, the resource class generator will produce dummy resource classes with
        // non-conflicting IDs to satisfy the class loader.
        // The resource repository remembers the dynamic IDs that it handed out and when the layoutlib
        // calls to ask about the name and content of a given resource ID, the repository can just
        // answer what it has already stored.
        AndroidIdeInfo androidIdeInfo = dependencyTarget.androidIdeInfo;
        if (androidIdeInfo != null && !Strings.isNullOrEmpty(androidIdeInfo.resourceJavaPackage) && repository != null) {
            ResourceClassRegistry.get(module.getProject()).addLibrary(repository, androidIdeInfo.resourceJavaPackage);
        }
    }
    return results.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap)

Aggregations

ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)27 File (java.io.File)15 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)13 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)13 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)12 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)12 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)12 Nullable (javax.annotation.Nullable)12 ImmutableList (com.google.common.collect.ImmutableList)9 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)9 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)8 BlazeImportSettingsManager (com.google.idea.blaze.base.settings.BlazeImportSettingsManager)8 Project (com.intellij.openapi.project.Project)8 List (java.util.List)8 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)6 WorkspaceRoot (com.google.idea.blaze.base.model.primitives.WorkspaceRoot)6 Logger (com.intellij.openapi.diagnostic.Logger)6 Maps (com.google.common.collect.Maps)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5