use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method updateTargetMap.
@Nullable
private static TargetMapAndInterfaceState updateTargetMap(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, SyncProjectState projectState, BlazeBuildOutputs buildResult, boolean mergeWithOldState, @Nullable BlazeProjectData oldProjectData) {
// any old state that we have in an attempt not to lose too much code.
if (buildResult.buildResult.status == BuildResult.Status.BUILD_ERROR) {
mergeWithOldState = true;
}
TargetMap oldTargetMap = oldProjectData != null ? oldProjectData.getTargetMap() : null;
BlazeIdeInterfaceState prevState = oldProjectData != null ? oldProjectData.getTargetData().ideInterfaceState : null;
Predicate<String> ideInfoPredicate = AspectStrategy.ASPECT_OUTPUT_FILE_PREDICATE;
Collection<OutputArtifact> files = buildResult.getOutputGroupArtifacts(group -> group.startsWith(OutputGroup.INFO.prefix)).stream().filter(f -> ideInfoPredicate.test(f.getKey())).distinct().collect(toImmutableList());
ArtifactsDiff diff;
try {
diff = ArtifactsDiff.diffArtifacts(prevState != null ? prevState.ideInfoFileState : null, files);
} catch (InterruptedException e) {
throw new ProcessCanceledException(e);
} catch (ExecutionException e) {
IssueOutput.error("Failed to diff aspect output files: " + e).submit(context);
return null;
}
// if we're merging with the old state, no files are removed
int targetCount = files.size() + (mergeWithOldState ? diff.getRemovedOutputs().size() : 0);
int removedCount = mergeWithOldState ? 0 : diff.getRemovedOutputs().size();
context.output(PrintOutput.log(String.format("Total rules: %d, new/changed: %d, removed: %d", targetCount, diff.getUpdatedOutputs().size(), removedCount)));
ListenableFuture<?> downloadArtifactsFuture = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(/* projectName= */
project.getName(), /* outputArtifacts= */
BlazeArtifact.getRemoteArtifacts(diff.getUpdatedOutputs()));
ListenableFuture<?> loadFilesInJvmFuture = RemoteArtifactPrefetcher.getInstance().loadFilesInJvm(/* outputArtifacts= */
BlazeArtifact.getRemoteArtifacts(diff.getUpdatedOutputs()));
if (!FutureUtil.waitForFuture(context, Futures.allAsList(downloadArtifactsFuture, loadFilesInJvmFuture)).timed("PrefetchRemoteAspectOutput", EventType.Prefetching).withProgressMessage("Reading IDE info result...").run().success()) {
return null;
}
ListenableFuture<?> fetchLocalFilesFuture = PrefetchService.getInstance().prefetchFiles(/* files= */
BlazeArtifact.getLocalFiles(diff.getUpdatedOutputs()), /* refetchCachedFiles= */
true, /* fetchFileTypes= */
false);
if (!FutureUtil.waitForFuture(context, fetchLocalFilesFuture).timed("FetchAspectOutput", EventType.Prefetching).withProgressMessage("Reading IDE info result...").run().success()) {
return null;
}
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectState.getProjectViewSet()).build();
BlazeConfigurationHandler configHandler = new BlazeConfigurationHandler(projectState.getBlazeInfo());
TargetMapAndInterfaceState state = updateState(project, context, prevState, diff, configHandler, projectState.getBlazeVersionData(), projectState.getLanguageSettings(), importRoots, mergeWithOldState, oldTargetMap);
if (state == null) {
return null;
}
// prefetch ide-resolve genfiles
Scope.push(context, childContext -> {
childContext.push(new TimingScope("GenfilesPrefetchBuildArtifacts", EventType.Other));
ImmutableList<OutputArtifact> resolveOutputs = ImmutableList.copyOf(buildResult.getOutputGroupArtifacts(group -> group.startsWith(OutputGroup.RESOLVE.prefix)));
prefetchGenfiles(context, resolveOutputs);
});
return state;
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class PackageManifestReader method readPackageManifestFiles.
/**
* @return A map from java source absolute file path to declared package string.
*/
public Map<TargetKey, Map<ArtifactLocation, String>> readPackageManifestFiles(Project project, BlazeContext context, ArtifactLocationDecoder decoder, Map<TargetKey, ArtifactLocation> javaPackageManifests, ListeningExecutorService executorService) {
Map<OutputArtifact, TargetKey> fileToLabelMap = Maps.newHashMap();
for (Map.Entry<TargetKey, ArtifactLocation> entry : javaPackageManifests.entrySet()) {
TargetKey key = entry.getKey();
BlazeArtifact artifact = decoder.resolveOutput(entry.getValue());
if (artifact instanceof OutputArtifact) {
fileToLabelMap.put((OutputArtifact) artifact, key);
}
}
ArtifactsDiff diff;
try {
diff = ArtifactsDiff.diffArtifacts(artifactState, fileToLabelMap.keySet());
artifactState = diff.getNewState();
} catch (InterruptedException e) {
throw new ProcessCanceledException(e);
} catch (ExecutionException e) {
context.setHasError();
IssueOutput.error("Updating package manifest files failed: " + e).submit(context);
throw new AssertionError("Unhandled exception", e);
}
// Find all not cached {@link RemoteOutputArtifact} and download them before parsing manifest
// file
ImmutableList.Builder<RemoteOutputArtifact> toDownload = ImmutableList.builder();
for (OutputArtifact outputArtifact : diff.getUpdatedOutputs()) {
if (!(outputArtifact instanceof RemoteOutputArtifact)) {
continue;
}
if (findArtifactInCache(project, outputArtifact) != null) {
continue;
}
toDownload.add((RemoteOutputArtifact) outputArtifact);
}
ListenableFuture<?> fetchRemoteArtifactFuture = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(project.getName(), toDownload.build());
ListenableFuture<?> fetchFuture = PrefetchService.getInstance().prefetchFiles(BlazeArtifact.getLocalFiles(diff.getUpdatedOutputs()), true, false);
if (!FutureUtil.waitForFuture(context, Futures.allAsList(fetchRemoteArtifactFuture, fetchFuture)).timed("FetchPackageManifests", EventType.Prefetching).withProgressMessage("Reading package manifests...").run().success()) {
return null;
}
List<ListenableFuture<Void>> futures = Lists.newArrayList();
for (OutputArtifact file : diff.getUpdatedOutputs()) {
futures.add(executorService.submit(() -> {
Map<ArtifactLocation, String> manifest = parseManifestFile(project, file);
manifestMap.put(fileToLabelMap.get(file), manifest);
return null;
}));
}
for (ArtifactState file : diff.getRemovedOutputs()) {
TargetKey key = this.fileToLabelMap.get(file);
if (key != null) {
manifestMap.remove(key);
}
}
this.fileToLabelMap = fileToLabelMap.entrySet().stream().filter(e -> diff.getNewState().containsKey(e.getKey().getKey())).collect(toImmutableMap(e -> e.getKey().toArtifactState(), Map.Entry::getValue));
try {
Futures.allAsList(futures).get();
} catch (ExecutionException | InterruptedException e) {
logger.error(e);
throw new IllegalStateException("Could not read sources");
}
return manifestMap;
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class EmptyLibrary method doRemoveEmptyLibraries.
private static ImmutableMap<LibraryKey, BlazeJarLibrary> doRemoveEmptyLibraries(Project project, BlazeContext context, ArtifactLocationDecoder locationDecoder, Map<LibraryKey, BlazeJarLibrary> allLibraries, EmptyJarTracker oldProjectData, BlazeJavaImportResult.Builder importResultBuilder) {
long startTime = System.currentTimeMillis();
Set<OutputArtifact> currentOutputArtifacts = new HashSet<>();
Map<LibraryKey, ArtifactState> libraryKeyToArtifactState = new HashMap<>();
// `OutputArtifact` does not have an associated `ArtifactState`
for (Map.Entry<LibraryKey, BlazeJarLibrary> entry : allLibraries.entrySet()) {
ArtifactLocation libraryJar = entry.getValue().libraryArtifact.jarForIntellijLibrary();
BlazeArtifact libraryArtifact = locationDecoder.resolveOutput(libraryJar);
if (libraryArtifact instanceof OutputArtifact) {
ArtifactState libraryArtifactState = ((OutputArtifact) libraryArtifact).toArtifactState();
libraryKeyToArtifactState.put(entry.getKey(), libraryArtifactState);
currentOutputArtifacts.add((OutputArtifact) libraryArtifact);
}
}
// Update emptyJarTracker by reevaluating any artifact that might have changed since last sync
EmptyJarTracker emptyJarTracker = getUpdatedEmptyJarTracker(project, context, currentOutputArtifacts, oldProjectData);
// Put non-empty artifacts in `result`
Map<LibraryKey, BlazeJarLibrary> result = new LinkedHashMap<>();
for (Map.Entry<LibraryKey, BlazeJarLibrary> entry : allLibraries.entrySet()) {
ArtifactState libraryArtifactState = libraryKeyToArtifactState.get(entry.getKey());
if (libraryArtifactState == null || !emptyJarTracker.isKnownEmpty(libraryArtifactState)) {
result.put(entry.getKey(), entry.getValue());
}
}
context.output(PrintOutput.log(String.format("Filtered %d JARs, in %dms", allLibraries.size() - result.size(), System.currentTimeMillis() - startTime)));
importResultBuilder.setEmptyJarTracker(emptyJarTracker);
return ImmutableMap.copyOf(result);
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class EmptyLibrary method getUpdatedEmptyJarTracker.
/**
* Takes the current set of artifacts and artifacts known from previous sync to calculate which of
* new artifacts are empty. Stores the status of each artifact in {@link EmptyJarTracker}.
*
* <p>Artifacts that have changed are fetched and passed to {@link EmptyLibraryFilter} to check if
* they are empty or not.
*
* <p>NOTE: This method does Network IO and File IO, keep an eye on performance
*/
private static EmptyJarTracker getUpdatedEmptyJarTracker(Project project, BlazeContext context, Collection<OutputArtifact> newArtifacts, EmptyJarTracker oldTracker) {
ImmutableMap<String, ArtifactState> oldState = oldTracker.getState();
try {
// Calculate artifacts that have changed or been removed since last sync
ArtifactsDiff diff = ArtifactsDiff.diffArtifacts(oldState, newArtifacts);
ImmutableList<OutputArtifact> updated = diff.getUpdatedOutputs();
ImmutableSet<ArtifactState> removed = diff.getRemovedOutputs();
// Copy over tracking data from previous sync, and remove entries which are no longer valid.
EmptyJarTracker.Builder builder = EmptyJarTracker.builder();
builder.addAllEntries(oldTracker);
builder.removeEntries(removed);
// Prefetch updated artifacts
ListenableFuture<?> future = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(project.getName(), BlazeArtifact.getRemoteArtifacts(updated));
FutureUtil.waitForFuture(context, future).timed("FetchJarsForEmptyStatusTracking", EventType.Prefetching).withProgressMessage("Fetching JARs to track empty status..").run();
// Evaluate if the updated artifacts are empty or not
Map<ArtifactState, Boolean> updatedStatuses = getEmptyStatusInParallel(updated, new EmptyLibraryFilter(), FetchExecutor.EXECUTOR);
builder.addAllEntries(updatedStatuses);
if (!updated.isEmpty()) {
context.output(PrintOutput.log(String.format("[Empty JAR Filter] Calculated empty status of %d JARs", updated.size())));
}
if (!removed.isEmpty()) {
context.output(PrintOutput.log(String.format("[Empty JAR Filter] Removed %d JARs", removed.size())));
}
return builder.build();
} catch (InterruptedException e) {
String message = "Updating EmptyJarTracker failed.";
logger.warn(message, e);
IssueOutput.warn(message).submit(context);
} catch (ExecutionException e) {
Thread.currentThread().interrupt();
context.setCancelled();
}
// Something went wrong, return old tracking data
return oldTracker;
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class RenderJarClassFileFinderTest method createBinaryJars.
/**
* Creates empty files corresponding to content entries in a JAR. Doesn't create an actual
* archive, only mimics the archive roots in file system.
*/
private void createBinaryJars() throws ArtifactNotFoundException {
File cacheDirFile = RenderJarCache.getInstance(getProject()).getCacheDir();
fileSystem.createDirectory(cacheDirFile.getAbsolutePath());
String cacheDir = cacheDirFile.getPath();
OutputArtifact binAArtifact = (OutputArtifact) artifactLocationDecoder.resolveOutput(getArtifactLocation("com/google/example/simple/bin_a.jar"));
CacheEntry binACacheEntry = CacheEntry.forArtifact(binAArtifact);
String binAJar = cacheDir + "/" + binACacheEntry.getFileName();
fileSystem.createFile(binAJar);
fileSystem.createFile(binAJar + "!/com/google/example/simple/bin_a/MainActivity.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/bin_a/R$color.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/src_a/SrcA.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/src_a/SrcA$Inner.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/src_a/R$attr.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/trans_dep_a/TransDepA.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/trans_dep_a/TransDepA$Inner.class");
fileSystem.createFile(binAJar + "!/com/google/example/simple/trans_dep_a/R$dimen.class");
artifactCache.addTrackedFile(binAArtifact, binAJar);
OutputArtifact binBArtifact = (OutputArtifact) artifactLocationDecoder.resolveOutput(getArtifactLocation("com/google/example/simple/bin_b.jar"));
CacheEntry binBCacheEntry = CacheEntry.forArtifact(binBArtifact);
String binBJar = cacheDir + "/" + binBCacheEntry.getFileName();
fileSystem.createFile(binBJar);
fileSystem.createFile(binBJar + "!/com/google/example/simple/bin_b/MainActivity.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/bin_b/R$color.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/src_b/SrcB.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/src_b/SrcB$Inner.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/src_b/R$attr.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/trans_dep_b/TransDepB.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/trans_dep_b/TransDepB$Inner.class");
fileSystem.createFile(binBJar + "!/com/google/example/simple/trans_dep_b/R$dimen.class");
artifactCache.addTrackedFile(binBArtifact, binBJar);
OutputArtifact binCArtifact = (OutputArtifact) artifactLocationDecoder.resolveOutput(getArtifactLocation("com/google/example/simple/bin_c.jar"));
CacheEntry binCCacheEntry = CacheEntry.forArtifact(binCArtifact);
String binCJar = cacheDir + "/" + binCCacheEntry.getFileName();
fileSystem.createFile(binCJar);
fileSystem.createFile(binCJar + "!/com/google/example/simple/bin_c/MainActivity.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/bin_c/R$color.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/src_c/SrcC.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/src_c/SrcC$Inner.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/src_c/R$attr.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/trans_dep_c/TransDepC.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/trans_dep_c/TransDepC$Inner.class");
fileSystem.createFile(binCJar + "!/com/google/example/simple/trans_dep_c/R$dimen.class");
artifactCache.addTrackedFile(binCArtifact, binCJar);
}
Aggregations