use of com.google.idea.blaze.base.ideinfo.TargetIdeInfo in project intellij by bazelbuild.
the class BlazeKotlinRunConfigurationProducer method getTargetIdeInfo.
@Nullable
private static TargetIdeInfo getTargetIdeInfo(ConfigurationContext context) {
Location<?> location = context.getLocation();
if (location == null) {
return null;
}
VirtualFile virtualFile = location.getVirtualFile();
if (virtualFile == null) {
return null;
}
KtDeclarationContainer entryPointContainer = KotlinRunConfigurationProducer.Companion.getEntryPointContainer(location.getPsiElement());
if (entryPointContainer == null) {
return null;
}
String startClassFqName = KotlinRunConfigurationProducer.Companion.getStartClassFqName(entryPointContainer);
if (startClassFqName == null) {
return null;
}
Collection<TargetIdeInfo> kotlinBinaryTargets = findKotlinBinaryTargets(context.getProject(), VfsUtil.virtualToIoFile(virtualFile));
// first look for a matching main_class
TargetIdeInfo match = kotlinBinaryTargets.stream().filter(target -> target.javaIdeInfo != null && startClassFqName.equals(target.javaIdeInfo.javaBinaryMainClass)).findFirst().orElse(null);
if (match != null) {
return match;
}
match = kotlinBinaryTargets.stream().filter(target -> startClassFqName.equals(target.key.label.targetName().toString())).findFirst().orElse(null);
if (match != null) {
return match;
}
return Iterables.getFirst(kotlinBinaryTargets, null);
}
use of com.google.idea.blaze.base.ideinfo.TargetIdeInfo in project intellij by bazelbuild.
the class FastBuildCompilerFactoryImplTest method testMultipleJavaToolchains.
@Test
public void testMultipleJavaToolchains() {
Map<TargetKey, TargetIdeInfo> targetMap = new HashMap<>();
TargetIdeInfo buildTargetInfo = TargetIdeInfo.builder().setLabel(Label.create("//our/build:target")).addDependency(Label.create("//some/jdk:langtools")).addDependency(Label.create("//other/jdk:langtools")).build();
targetMap.put(TargetKey.forPlainTarget(Label.create("//our/build:target")), buildTargetInfo);
targetMap.put(TargetKey.forPlainTarget(Label.create("//some/jdk:langtools")), TargetIdeInfo.builder().setJavaToolchainIdeInfo(JavaToolchainIdeInfo.builder().setJavacJar(ArtifactLocation.builder().setRelativePath(JAVAC_JAR.getPath()).build())).build());
targetMap.put(TargetKey.forPlainTarget(Label.create("//other/jdk:langtools")), TargetIdeInfo.builder().setJavaToolchainIdeInfo(JavaToolchainIdeInfo.builder().setJavacJar(ArtifactLocation.builder().setRelativePath(JAVAC_JAR.getPath()).build())).build());
configureTestForTargetMap(targetMap);
try {
compilerFactory.getCompilerFor(buildTargetInfo);
fail("Should have thrown FastBuildException");
} catch (FastBuildException e) {
assertThat(e.getMessage()).contains("Java toolchain");
}
}
use of com.google.idea.blaze.base.ideinfo.TargetIdeInfo in project intellij by bazelbuild.
the class BlazeScalaMainClassRunConfigurationProducer method getTarget.
@Nullable
private static TargetIdeInfo getTarget(Project project, ScObject mainObject) {
File mainObjectFile = RunUtil.getFileForClass(mainObject);
if (mainObjectFile == null) {
return null;
}
Collection<TargetIdeInfo> scalaBinaryTargets = findScalaBinaryTargets(project, mainObjectFile);
// Scala objects are basically singletons with a '$' appended to the class name.
// The trunced qualified name removes the '$',
// so it matches the main class specified in the scala_binary rule.
String qualifiedName = mainObject.getTruncedQualifiedName();
if (qualifiedName == null) {
// out of date psi element; just take the first match
return Iterables.getFirst(scalaBinaryTargets, null);
}
// Can't use getName because of the '$'.
String className = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
// first look for a matching main_class
TargetIdeInfo match = scalaBinaryTargets.stream().filter(target -> target.javaIdeInfo != null && qualifiedName.equals(target.javaIdeInfo.javaBinaryMainClass)).findFirst().orElse(null);
if (match != null) {
return match;
}
match = scalaBinaryTargets.stream().filter(target -> className.equals(target.key.label.targetName().toString())).findFirst().orElse(null);
if (match != null) {
return match;
}
return Iterables.getFirst(scalaBinaryTargets, null);
}
use of com.google.idea.blaze.base.ideinfo.TargetIdeInfo 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.base.ideinfo.TargetIdeInfo in project intellij by bazelbuild.
the class BlazeAndroidProjectStructureSyncer method ensureRunConfigurationModule.
/**
* Ensures a suitable module exists for the given android target.
*/
@Nullable
public static Module ensureRunConfigurationModule(Project project, Label label) {
TargetKey targetKey = TargetKey.forPlainTarget(label);
String moduleName = moduleNameForAndroidModule(targetKey);
Module module = ModuleFinder.getInstance(project).findModuleByName(moduleName);
if (module != null) {
return module;
}
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (blazeProjectData == null) {
return null;
}
AndroidSdkPlatform androidSdkPlatform = SdkUtil.getAndroidSdkPlatform(blazeProjectData);
if (androidSdkPlatform == null) {
return null;
}
TargetIdeInfo target = blazeProjectData.targetMap.get(targetKey);
if (target == null) {
return null;
}
if (target.androidIdeInfo == null) {
return null;
}
// invokeAndWait it because the caller may have a read action.
if (!ApplicationManager.getApplication().isDispatchThread()) {
return null;
}
BlazeSyncPlugin.ModuleEditor moduleEditor = ModuleEditorProvider.getInstance().getModuleEditor(project, BlazeImportSettingsManager.getInstance(project).getImportSettings());
Module newModule = moduleEditor.createModule(moduleName, StdModuleTypes.JAVA);
ApplicationManager.getApplication().runWriteAction(() -> {
AndroidFacetModuleCustomizer.createAndroidFacet(newModule);
moduleEditor.commit();
});
File moduleDirectory = moduleDirectoryForAndroidTarget(WorkspaceRoot.fromProject(project), target);
updateModuleFacetInMemoryState(project, androidSdkPlatform, newModule, moduleDirectory, manifestFileForAndroidTarget(blazeProjectData.artifactLocationDecoder, target.androidIdeInfo, moduleDirectory), target.androidIdeInfo.resourceJavaPackage, ImmutableList.of(), null);
return newModule;
}
Aggregations