use of com.google.idea.blaze.java.sync.model.BlazeSourceDirectory in project intellij by bazelbuild.
the class KotlinSyncTest method testKotlinClassesPresentInClassPath.
@Test
public void testKotlinClassesPresentInClassPath() {
setProjectView("directories:", " src/main/kotlin/com/google", "targets:", " //src/main/kotlin/com/google:lib", "additional_languages:", " kotlin");
workspace.createFile(new WorkspacePath("src/main/kotlin/com/google/ClassWithUniqueName1.kt"), "package com.google;", "public class ClassWithUniqueName1 {}");
workspace.createFile(new WorkspacePath("src/main/kotlin/com/google/ClassWithUniqueName2.kt"), "package com.google;", "public class ClassWithUniqueName2 {}");
workspace.createDirectory(new WorkspacePath("external/com_github_jetbrains_kotlin"));
TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("src/main/kotlin/com/google/BUILD")).setLabel("//src/main/kotlin/com/google:lib").setKind("kt_jvm_library").addSource(sourceRoot("src/main/kotlin/com/google/ClassWithUniqueName1.scala")).addSource(sourceRoot("src/main/kotlin/com/google/ClassWithUniqueName2.scala")).setJavaInfo(JavaIdeInfo.builder())).build();
setTargetMap(targetMap);
BlazeSyncParams syncParams = new BlazeSyncParams.Builder("Full Sync", BlazeSyncParams.SyncMode.FULL).addProjectViewTargets(true).build();
runBlazeSync(syncParams);
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData();
assertThat(blazeProjectData).isNotNull();
assertThat(blazeProjectData.targetMap).isEqualTo(targetMap);
assertThat(blazeProjectData.workspaceLanguageSettings).isEqualTo(new WorkspaceLanguageSettings(WorkspaceType.JAVA, ImmutableSet.of(LanguageClass.GENERIC, LanguageClass.JAVA, LanguageClass.KOTLIN)));
BlazeJavaSyncData javaSyncData = blazeProjectData.syncState.get(BlazeJavaSyncData.class);
assertThat(javaSyncData).isNotNull();
List<BlazeContentEntry> contentEntries = javaSyncData.importResult.contentEntries;
assertThat(contentEntries).hasSize(1);
BlazeContentEntry contentEntry = contentEntries.get(0);
assertThat(contentEntry.contentRoot.getPath()).isEqualTo(this.workspaceRoot.fileForPath(new WorkspacePath("src/main/kotlin/com/google")).getPath());
assertThat(contentEntry.sources).hasSize(1);
BlazeSourceDirectory sourceDir = contentEntry.sources.get(0);
assertThat(sourceDir.getPackagePrefix()).isEqualTo("com.google");
assertThat(sourceDir.getDirectory().getPath()).isEqualTo(this.workspaceRoot.fileForPath(new WorkspacePath("src/main/kotlin/com/google")).getPath());
}
use of com.google.idea.blaze.java.sync.model.BlazeSourceDirectory in project intellij by bazelbuild.
the class SourceDirectoryCalculator method calculateContentEntries.
public ImmutableList<BlazeContentEntry> calculateContentEntries(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, ArtifactLocationDecoder artifactLocationDecoder, ImportRoots importRoots, Collection<SourceArtifact> sources, Map<TargetKey, ArtifactLocation> javaPackageManifests) {
ManifestFilePackageReader manifestFilePackageReader = Scope.push(context, (childContext) -> {
childContext.push(new TimingScope("ReadPackageManifests", EventType.Other));
Map<TargetKey, Map<ArtifactLocation, String>> manifestMap = PackageManifestReader.getInstance().readPackageManifestFiles(project, childContext, artifactLocationDecoder, javaPackageManifests, packageReaderExecutorService);
return new ManifestFilePackageReader(manifestMap);
});
final List<JavaPackageReader> javaPackageReaders = Lists.newArrayList(manifestFilePackageReader, JavaSourcePackageReader.getInstance(), generatedFileJavaPackageReader);
Collection<SourceArtifact> nonGeneratedSources = filterGeneratedArtifacts(sources);
// Sort artifacts and excludes into their respective workspace paths
Multimap<WorkspacePath, SourceArtifact> sourcesUnderDirectoryRoot = sortArtifactLocationsByRootDirectory(context, importRoots, nonGeneratedSources);
List<BlazeContentEntry> result = Lists.newArrayList();
Scope.push(context, (childContext) -> {
childContext.push(new TimingScope("CalculateSourceDirectories", EventType.Other));
for (WorkspacePath workspacePath : importRoots.rootDirectories()) {
File contentRoot = workspaceRoot.fileForPath(workspacePath);
ImmutableList<BlazeSourceDirectory> sourceDirectories = calculateSourceDirectoriesForContentRoot(context, workspaceRoot, artifactLocationDecoder, workspacePath, sourcesUnderDirectoryRoot.get(workspacePath), javaPackageReaders);
if (!sourceDirectories.isEmpty()) {
result.add(new BlazeContentEntry(contentRoot, sourceDirectories));
}
}
result.sort(Comparator.comparing(lhs -> lhs.contentRoot));
});
return ImmutableList.copyOf(result);
}
use of com.google.idea.blaze.java.sync.model.BlazeSourceDirectory in project intellij by bazelbuild.
the class JavaSyncTest method testJavaClassesPresentInClassPath.
@Test
public void testJavaClassesPresentInClassPath() throws Exception {
setProjectView("directories:", " java/com/google", "targets:", " //java/com/google:lib");
workspace.createFile(new WorkspacePath("java/com/google/ClassWithUniqueName1.java"), "package com.google;", "public class ClassWithUniqueName1 {}");
workspace.createFile(new WorkspacePath("java/com/google/ClassWithUniqueName2.java"), "package com.google;", "public class ClassWithUniqueName2 {}");
TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("java/com/google/BUILD")).setLabel("//java/com/google:lib").setKind("java_library").addSource(sourceRoot("java/com/google/ClassWithUniqueName1.java")).addSource(sourceRoot("java/com/google/ClassWithUniqueName2.java")).setJavaInfo(JavaIdeInfo.builder())).build();
setTargetMap(targetMap);
BlazeSyncParams syncParams = new BlazeSyncParams.Builder("Full Sync", BlazeSyncParams.SyncMode.FULL).addProjectViewTargets(true).build();
runBlazeSync(syncParams);
errorCollector.assertNoIssues();
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData();
assertThat(blazeProjectData).isNotNull();
assertThat(blazeProjectData.targetMap).isEqualTo(targetMap);
assertThat(blazeProjectData.workspaceLanguageSettings.getWorkspaceType()).isEqualTo(WorkspaceType.JAVA);
BlazeJavaSyncData javaSyncData = blazeProjectData.syncState.get(BlazeJavaSyncData.class);
List<BlazeContentEntry> contentEntries = javaSyncData.importResult.contentEntries;
assertThat(contentEntries).hasSize(1);
BlazeContentEntry contentEntry = contentEntries.get(0);
assertThat(contentEntry.contentRoot.getPath()).isEqualTo(workspaceRoot.fileForPath(new WorkspacePath("java/com/google")).getPath());
assertThat(contentEntry.sources).hasSize(1);
BlazeSourceDirectory sourceDir = contentEntry.sources.get(0);
assertThat(sourceDir.getPackagePrefix()).isEqualTo("com.google");
assertThat(sourceDir.getDirectory().getPath()).isEqualTo(workspaceRoot.fileForPath(new WorkspacePath("java/com/google")).getPath());
}
use of com.google.idea.blaze.java.sync.model.BlazeSourceDirectory in project intellij by bazelbuild.
the class SourceDirectoryCalculator method calculateJavaSourceDirectories.
/**
* Adds the java source directories.
*/
private void calculateJavaSourceDirectories(BlazeContext context, WorkspaceRoot workspaceRoot, ArtifactLocationDecoder artifactLocationDecoder, WorkspacePath directoryRoot, Collection<SourceArtifact> javaArtifacts, Collection<JavaPackageReader> javaPackageReaders, Collection<BlazeSourceDirectory> result) {
List<SourceRoot> sourceRootsPerFile = Lists.newArrayList();
// Get java sources
List<ListenableFuture<SourceRoot>> sourceRootFutures = Lists.newArrayList();
for (final SourceArtifact sourceArtifact : javaArtifacts) {
ListenableFuture<SourceRoot> future = executorService.submit(() -> sourceRootForJavaSource(context, artifactLocationDecoder, sourceArtifact, javaPackageReaders));
sourceRootFutures.add(future);
}
try {
for (SourceRoot sourceRoot : Futures.allAsList(sourceRootFutures).get()) {
if (sourceRoot != null) {
sourceRootsPerFile.add(sourceRoot);
}
}
} catch (ExecutionException | InterruptedException e) {
logger.error(e);
throw new IllegalStateException("Could not read sources");
}
// Sort source roots into their respective directories
Map<WorkspacePath, Multiset<SourceRoot>> sourceDirectoryToSourceRoots = new HashMap<>();
for (SourceRoot sourceRoot : sourceRootsPerFile) {
sourceDirectoryToSourceRoots.computeIfAbsent(sourceRoot.workspacePath, k -> HashMultiset.create()).add(sourceRoot);
}
// Create a mapping from directory to package prefix
Map<WorkspacePath, SourceRoot> workspacePathToSourceRoot = Maps.newHashMap();
for (WorkspacePath workspacePath : sourceDirectoryToSourceRoots.keySet()) {
Multiset<SourceRoot> sources = sourceDirectoryToSourceRoots.get(workspacePath);
Multiset<String> packages = HashMultiset.create();
for (Multiset.Entry<SourceRoot> entry : sources.entrySet()) {
packages.setCount(entry.getElement().packagePrefix, entry.getCount());
}
final String directoryPackagePrefix;
// Common case -- all source files agree on a single package
if (packages.elementSet().size() == 1) {
directoryPackagePrefix = packages.elementSet().iterator().next();
} else {
String preferredPackagePrefix = PackagePrefixCalculator.packagePrefixOf(workspacePath);
directoryPackagePrefix = pickMostFrequentlyOccurring(packages, preferredPackagePrefix);
}
SourceRoot candidateRoot = new SourceRoot(workspacePath, directoryPackagePrefix);
workspacePathToSourceRoot.put(workspacePath, candidateRoot);
}
// Add content entry base if it doesn't exist
if (!workspacePathToSourceRoot.containsKey(directoryRoot)) {
SourceRoot candidateRoot = new SourceRoot(directoryRoot, PackagePrefixCalculator.packagePrefixOf(directoryRoot));
workspacePathToSourceRoot.put(directoryRoot, candidateRoot);
}
// First, create a graph of the directory structure from root to each source file
Map<WorkspacePath, SourceRootDirectoryNode> sourceRootDirectoryNodeMap = Maps.newHashMap();
SourceRootDirectoryNode rootNode = new SourceRootDirectoryNode(directoryRoot, null);
sourceRootDirectoryNodeMap.put(directoryRoot, rootNode);
for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) {
final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath);
List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of();
SourceRootDirectoryNode previousNode = rootNode;
for (int i = 0; i < pathComponents.size(); ++i) {
final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, i + 1);
SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath);
if (node == null) {
node = new SourceRootDirectoryNode(workspacePath, pathComponents.get(i));
sourceRootDirectoryNodeMap.put(workspacePath, node);
previousNode.children.add(node);
}
previousNode = node;
}
}
// Add package prefix votes at each directory node
for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) {
final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath);
List<String> packageComponents = PACKAGE_SPLITTER.splitToList(sourceRoot.packagePrefix);
List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of();
int packageIndex = packageComponents.size();
int pathIndex = pathComponents.size();
while (pathIndex >= 0 && packageIndex >= 0) {
final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, pathIndex);
SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath);
String packagePrefix = PACKAGE_JOINER.join(packageComponents.subList(0, packageIndex));
// Otherwise just add a vote
if (sourceRoot.workspacePath.equals(workspacePath)) {
node.forcedPackagePrefix = packagePrefix;
} else {
node.packagePrefixVotes.add(packagePrefix);
}
String pathComponent = pathIndex > 0 ? pathComponents.get(pathIndex - 1) : "";
String packageComponent = packageIndex > 0 ? packageComponents.get(packageIndex - 1) : "";
if (!pathComponent.equals(packageComponent)) {
break;
}
--packageIndex;
--pathIndex;
}
}
Map<WorkspacePath, SourceRoot> sourceRoots = Maps.newHashMap();
SourceRootDirectoryNode root = sourceRootDirectoryNodeMap.get(directoryRoot);
visitDirectoryNode(sourceRoots, root, null);
for (SourceRoot sourceRoot : sourceRoots.values()) {
result.add(BlazeSourceDirectory.builder(workspaceRoot.fileForPath(sourceRoot.workspacePath)).setPackagePrefix(sourceRoot.packagePrefix).setGenerated(false).build());
}
}
use of com.google.idea.blaze.java.sync.model.BlazeSourceDirectory in project intellij by bazelbuild.
the class JavaSourceFolderProvider method initializeSourceFolders.
@Override
public ImmutableMap<File, SourceFolder> initializeSourceFolders(ContentEntry contentEntry) {
Map<File, SourceFolder> map = new HashMap<>();
BlazeContentEntry javaContentEntry = blazeContentEntries.get(UrlUtil.urlToFile(contentEntry.getUrl()));
if (javaContentEntry != null) {
for (BlazeSourceDirectory sourceDirectory : javaContentEntry.sources) {
File file = sourceDirectory.getDirectory();
if (map.containsKey(file)) {
continue;
}
SourceFolder sourceFolder = addSourceFolderToContentEntry(contentEntry, sourceDirectory);
map.put(file, sourceFolder);
}
}
return ImmutableMap.copyOf(map);
}
Aggregations