use of com.google.idea.blaze.java.sync.model.BlazeJarLibrary in project intellij by bazelbuild.
the class BlazeJavaWorkspaceImporterTest method testSyncAugmenter.
@Test
public void testSyncAugmenter() {
augmenters.registerExtension((workspaceLanguageSettings, projectViewSet, target, jars, genJars) -> {
if (target.key.label.equals(Label.create("//java/example:source"))) {
jars.add(new BlazeJarLibrary(LibraryArtifact.builder().setInterfaceJar(gen("source.jar")).build()));
}
});
ProjectView projectView = ProjectView.builder().add(ListSection.builder(DirectorySection.KEY).add(DirectoryEntry.include(new WorkspacePath("java/example")))).build();
TargetMapBuilder targetMapBuilder = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//java/example:source").setBuildFile(source("java/example/BUILD")).setKind("java_library").addSource(source("Source.java")).addDependency("//java/lib:lib").setJavaInfo(JavaIdeInfo.builder())).addTarget(TargetIdeInfo.builder().setLabel("//java/lib:lib").setBuildFile(source("java/lib/BUILD")).setKind("java_library").addSource(source("Lib.java")).setJavaInfo(JavaIdeInfo.builder()));
BlazeJavaImportResult result = importWorkspace(workspaceRoot, targetMapBuilder, projectView);
assertThat(result.libraries.values().stream().map(BlazeJavaWorkspaceImporterTest::libraryFileName).collect(Collectors.toList())).containsExactly("source.jar");
}
use of com.google.idea.blaze.java.sync.model.BlazeJarLibrary in project intellij by bazelbuild.
the class BlazeAndroidWorkspaceImporterTest method testEmptyResourceModuleIsAddedAsJar.
/**
* Test adding empty resource modules as jars.
*/
@Test
public void testEmptyResourceModuleIsAddedAsJar() {
ProjectView projectView = ProjectView.builder().add(ListSection.builder(DirectorySection.KEY).add(DirectoryEntry.include(new WorkspacePath("java/apps/example"))).add(DirectoryEntry.include(new WorkspacePath("javatests/apps/example")))).build();
/**
* Deps are project -> lib0 (no res) -> lib1 (has res) \ -> lib2 (has res)
*/
TargetMapBuilder response = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//java/apps/example/lib0:lib0").setKind("android_library").setBuildFile(source("java/apps/example/lib0/BUILD")).addSource(source("java/apps/example/lib0/SharedActivity.java")).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("java/apps/example/lib0/AndroidManifest.xml")).setGenerateResourceClass(true).setResourceJavaPackage("com.google.android.apps.example.lib0").setResourceJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib0/lib0_resources.jar")).setClassJar(gen("java/apps/example/lib0/lib0_resources.jar")))).addDependency("//java/apps/example/lib1:lib1").addDependency("//java/apps/example/lib2:lib2").setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib0/lib0.jar")).setClassJar(gen("java/apps/example/lib0/lib0.jar"))).addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib0/lib0_resources.jar")).setClassJar(gen("java/apps/example/lib0/lib0_resources.jar"))))).addTarget(TargetIdeInfo.builder().setLabel("//java/apps/example/lib1:lib1").setKind("android_library").setBuildFile(source("java/apps/example/lib1/BUILD")).addSource(source("java/apps/example/lib1/SharedActivity.java")).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("java/apps/example/lib1/AndroidManifest.xml")).addResource(source("java/apps/example/lib1/res")).setGenerateResourceClass(true).setResourceJavaPackage("com.google.android.apps.example.lib1").setResourceJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib1/li11_resources.jar")).setClassJar(gen("java/apps/example/lib1/lib1_resources.jar")))).setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib1/lib1.jar")).setClassJar(gen("java/apps/example/lib1/lib1.jar"))).addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib1/lib1_resources.jar")).setClassJar(gen("java/apps/example/lib1/lib1_resources.jar"))))).addTarget(TargetIdeInfo.builder().setLabel("//java/apps/example/lib2:lib2").setBuildFile(source("java/apps/example/lib2/BUILD")).setKind("android_library").addSource(source("java/apps/example/lib2/SharedActivity.java")).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("java/apps/example/lib2/AndroidManifest.xml")).addResource(source("java/apps/example/lib2/res")).setGenerateResourceClass(true).setResourceJavaPackage("com.google.android.libraries.example.lib2").setResourceJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib2/lib2_resources.jar")).setClassJar(gen("java/apps/example/lib2/lib2_resources.jar")))).setBuildFile(source("java/apps/example/lib2/BUILD")).setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib2/lib2.jar")).setClassJar(gen("java/apps/example/lib2/lib2.jar"))).addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/lib2/lib2_resources.jar")).setClassJar(gen("java/apps/example/lib2/lib2_resources.jar"))))).addTarget(TargetIdeInfo.builder().setLabel("//java/apps/example:example_debug").setKind("android_binary").setBuildFile(source("java/apps/example/BUILD")).addSource(source("java/apps/example/MainActivity.java")).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("java/apps/example/AndroidManifest.xml")).addResource(source("java/apps/example/res")).setGenerateResourceClass(true).setResourceJavaPackage("com.google.android.apps.example")).addDependency("//java/apps/example/lib0:lib0").setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setInterfaceJar(gen("java/apps/example/example_debug.jar")).setClassJar(gen("java/apps/example/example_debug.jar")))));
TargetMap targetMap = response.build();
BlazeAndroidJavaSyncAugmenter syncAugmenter = new BlazeAndroidJavaSyncAugmenter();
List<BlazeJarLibrary> jars = Lists.newArrayList();
List<BlazeJarLibrary> genJars = Lists.newArrayList();
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, BuildSystem.Blaze).add(ProjectViewSet.builder().add(projectView).build()).build();
ProjectViewSet projectViewSet = ProjectViewSet.builder().add(projectView).build();
for (TargetIdeInfo target : targetMap.targets()) {
if (importRoots.importAsSource(target.key.label)) {
syncAugmenter.addJarsForSourceTarget(workspaceLanguageSettings, projectViewSet, target, jars, genJars);
}
}
assertThat(jars.stream().map(library -> library.libraryArtifact.interfaceJar).map(artifactLocation -> new File(artifactLocation.relativePath).getName()).collect(Collectors.toList())).containsExactly("lib0_resources.jar");
}
use of com.google.idea.blaze.java.sync.model.BlazeJarLibrary in project intellij by bazelbuild.
the class BlazeAndroidWorkspaceImporterTest method testAarImport_multipleJarLibraries_aarLibraryOnlyOverridesAarJar.
@Test
public void testAarImport_multipleJarLibraries_aarLibraryOnlyOverridesAarJar() {
ProjectView projectView = ProjectView.builder().add(ListSection.builder(DirectorySection.KEY).add(DirectoryEntry.include(new WorkspacePath("java/example")))).build();
TargetMapBuilder targetMapBuilder = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//java/example:lib").setBuildFile(source("java/example/BUILD")).setKind(Kind.ANDROID_LIBRARY).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("java/example/AndroidManifest.xml")).addResource(source("java/example/res")).setGenerateResourceClass(true).setResourceJavaPackage("example")).setJavaInfo(JavaIdeInfo.builder()).addSource(source("java/example/Source.java")).addDependency("//third_party/lib:consume_export_aar").addDependency("//third_party/lib:dep_library").build()).addTarget(TargetIdeInfo.builder().setLabel("//third_party/lib:consume_export_aar").setBuildFile(source("third_party/lib/BUILD")).setKind(Kind.AAR_IMPORT).setAndroidAarInfo(new AndroidAarIdeInfo(source("third_party/lib/lib1_aar.aar"))).setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setClassJar(gen("third_party/lib/_aar/consume_export_aar/" + "classes_and_libs_merged.jar")))).addDependency("//third_party/lib:dep_aar").build()).addTarget(TargetIdeInfo.builder().setLabel("//third_party/lib:dep_aar").setBuildFile(source("third_party/lib/BUILD")).setKind(Kind.AAR_IMPORT).setAndroidAarInfo(new AndroidAarIdeInfo(source("third_party/lib/lib2_aar.aar"))).setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setClassJar(gen("third_party/lib/_aar/dep_aar/" + "classes_and_libs_merged.jar")))).build()).addTarget(TargetIdeInfo.builder().setLabel("//third_party/lib:dep_library").setBuildFile(source("third_party/lib/BUILD")).setKind(Kind.ANDROID_LIBRARY).addSource(source("third_party/lib/SharedActivity.java")).setAndroidInfo(AndroidIdeInfo.builder().setManifestFile(source("third_party/lib/AndroidManifest.xml")).addResource(source("third_party/lib/res")).setGenerateResourceClass(true).setResourceJavaPackage("com.lib")).setJavaInfo(JavaIdeInfo.builder().addJar(LibraryArtifact.builder().setInterfaceJar(gen("third_party/lib/dep_library.jar")).setClassJar(gen("third_party/lib/dep_library.jar")))).build());
jdepsMap.put(TargetKey.forPlainTarget(Label.create("//java/example:lib")), ImmutableList.of(jdepsPath("third_party/lib/_aar/dep_aar/classes_and_libs_merged.jar"), jdepsPath("third_party/lib/_aar/consume_export_aar/classes_and_libs_merged.jar"), jdepsPath("third_party/lib/dep_library.jar")));
BlazeJavaImportResult javaResult = importJavaWorkspace(workspaceRoot, targetMapBuilder, projectView);
BlazeAndroidImportResult androidResult = importWorkspace(workspaceRoot, targetMapBuilder, projectView);
errorCollector.assertNoIssues();
assertThat(javaResult.libraries).hasSize(3);
assertThat(androidResult.aarLibraries).hasSize(2);
assertThat(androidResult.aarLibraries.stream().map(BlazeAndroidWorkspaceImporterTest::aarJarName).collect(Collectors.toList())).containsExactly("classes_and_libs_merged.jar", "classes_and_libs_merged.jar");
assertThat(androidResult.aarLibraries.stream().map(BlazeAndroidWorkspaceImporterTest::aarName).collect(Collectors.toList())).containsExactly("lib1_aar.aar", "lib2_aar.aar");
BlazeAndroidLibrarySource.AarJarFilter aarFilter = new BlazeAndroidLibrarySource.AarJarFilter(androidResult.aarLibraries);
ImmutableList<BlazeJarLibrary> blazeJarLibraries = javaResult.libraries.values().asList();
for (BlazeJarLibrary jarLibrary : blazeJarLibraries) {
if (libraryJarName(jarLibrary).equals("dep_library.jar")) {
assertThat(aarFilter.test(jarLibrary)).isTrue();
} else {
assertThat(aarFilter.test(jarLibrary)).isFalse();
}
}
}
use of com.google.idea.blaze.java.sync.model.BlazeJarLibrary in project intellij by bazelbuild.
the class BlazeAndroidJavaSyncAugmenter method addJarsForSourceTarget.
@Override
public void addJarsForSourceTarget(WorkspaceLanguageSettings workspaceLanguageSettings, ProjectViewSet projectViewSet, TargetIdeInfo target, Collection<BlazeJarLibrary> jars, Collection<BlazeJarLibrary> genJars) {
if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.ANDROID)) {
return;
}
AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
if (androidIdeInfo == null) {
return;
}
LibraryArtifact idlJar = androidIdeInfo.idlJar;
if (idlJar != null) {
genJars.add(new BlazeJarLibrary(idlJar));
}
Set<String> whitelistedGenResourcePaths = projectViewSet.listItems(GeneratedAndroidResourcesSection.KEY).stream().map(genfilesPath -> genfilesPath.relativePath).collect(Collectors.toSet());
if (BlazeAndroidWorkspaceImporter.shouldGenerateResources(androidIdeInfo) && !BlazeAndroidWorkspaceImporter.shouldGenerateResourceModule(androidIdeInfo, whitelistedGenResourcePaths)) {
// Add blaze's output unless it's a top level rule.
// In these cases the resource jar contains the entire
// transitive closure of R classes. It's unlikely this is wanted to resolve in the IDE.
boolean discardResourceJar = target.kindIsOneOf(Kind.ANDROID_BINARY, Kind.ANDROID_TEST);
if (!discardResourceJar) {
LibraryArtifact resourceJar = androidIdeInfo.resourceJar;
if (resourceJar != null) {
jars.add(new BlazeJarLibrary(resourceJar));
}
}
}
}
use of com.google.idea.blaze.java.sync.model.BlazeJarLibrary in project intellij by bazelbuild.
the class BlazeSourceJarNavigationPolicy method getSourceJarRoot.
@Nullable
private VirtualFile getSourceJarRoot(Project project, BlazeProjectData blazeProjectData, PsiJavaFile clsFile) {
Library library = findLibrary(project, clsFile);
if (library == null || library.getFiles(OrderRootType.SOURCES).length != 0) {
// If the library already has sources attached, no need to hunt for them.
return null;
}
BlazeJarLibrary blazeLibrary = LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
if (blazeLibrary == null) {
return null;
}
// TODO: If there are multiple source jars, search for one containing this PsiJavaFile.
for (ArtifactLocation jar : blazeLibrary.libraryArtifact.sourceJars) {
VirtualFile root = getSourceJarRoot(project, blazeProjectData.artifactLocationDecoder, jar);
if (root != null) {
return root;
}
}
return null;
}
Aggregations