use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class LocalArtifactCacheTest method put_addsArtifactInDirectory.
@Test
public void put_addsArtifactInDirectory() throws IOException {
// Create blaze artifacts in FS
ImmutableList<OutputArtifact> outputArtifacts = ImmutableList.of(newLocalOutputArtifact("relative/path_1/artifact_1.jar"), newLocalOutputArtifact("relative/path_2/artifact_2.jar"), newLocalOutputArtifact("relative/path_3/artifact_3.jar"));
for (OutputArtifact a : outputArtifacts) {
File file = ((LocalFileOutputArtifact) a).getFile();
assertThat(Paths.get(file.getParent()).toFile().mkdirs()).isTrue();
assertThat(file.createNewFile()).isTrue();
}
// Put blaze artifacts in cache
artifactCache.initialize();
artifactCache.putAll(outputArtifacts, blazeContext, false);
// Check that the artifacts were added to the cache.
ImmutableList<File> expectedFiles = Stream.concat(outputArtifacts.stream().map(a -> {
try {
return CacheEntry.forArtifact(a);
} catch (ArtifactNotFoundException e) {
return null;
}
}).filter(Objects::nonNull).map(CacheEntry::getFileName), Stream.of(CACHE_DATA_FILENAME)).map(f -> new File(cacheDirectory.getRoot(), f)).collect(ImmutableList.toImmutableList());
assertThat(cacheDirectory.getRoot().listFiles()).asList().containsExactlyElementsIn(expectedFiles);
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class RenderJarCache method getCachedJarForBinaryTarget.
/**
* Returns the RenderJAR corresponding to {@code target} or null if no RenderJAR corresponding to
* {@code target} exists in cache.
*/
@Nullable
public File getCachedJarForBinaryTarget(ArtifactLocationDecoder artifactLocationDecoder, TargetIdeInfo target) {
if (!RenderJarClassFileFinder.isEnabled()) {
return null;
}
AndroidIdeInfo androidIdeInfo = target.getAndroidIdeInfo();
if (androidIdeInfo == null) {
return null;
}
ArtifactLocation jarArtifactLocation = androidIdeInfo.getRenderResolveJar();
if (jarArtifactLocation == null) {
return null;
}
BlazeArtifact jarArtifact = artifactLocationDecoder.resolveOutput(jarArtifactLocation);
if (!(jarArtifact instanceof OutputArtifact)) {
Logger.getInstance(RenderJarCache.class).warn("Unexpected render jar that is not an OutputArtifact: " + jarArtifactLocation);
return null;
}
Path jarPath = artifactCache.get((OutputArtifact) jarArtifact);
return jarPath == null ? null : jarPath.toFile();
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class JdepsFileReader method doLoadJdepsFiles.
@Nullable
private JdepsState doLoadJdepsFiles(Project project, BlazeContext context, ArtifactLocationDecoder decoder, @Nullable JdepsState oldState, Collection<TargetIdeInfo> targetsToLoad, SyncMode syncMode) throws InterruptedException, ExecutionException {
Map<OutputArtifact, TargetKey> fileToTargetMap = Maps.newHashMap();
for (TargetIdeInfo target : targetsToLoad) {
BlazeArtifact output = resolveJdepsOutput(decoder, target);
if (output instanceof OutputArtifact) {
fileToTargetMap.put((OutputArtifact) output, target.getKey());
}
}
ArtifactsDiff diff = ArtifactsDiff.diffArtifacts(oldState != null ? oldState.getArtifactState() : null, fileToTargetMap.keySet());
// TODO: handle prefetching for arbitrary OutputArtifacts
List<OutputArtifact> outputArtifacts = diff.getUpdatedOutputs();
// already. Additional logging to identify what is going wrong.
if (!outputArtifacts.isEmpty() && !SyncMode.involvesBlazeBuild(syncMode)) {
logger.warn("ArtifactDiff: " + outputArtifacts.size() + " outputs need to be updated during SyncMode.NO_BUILD ");
if (oldState == null) {
logger.warn("ArtifactDiff: oldState == null, we failed to load prior JdepsState.");
} else {
// Do not list all artifacts since it may be pretty long.
if (oldState.getArtifactState().size() != fileToTargetMap.size()) {
logger.warn("Existing artifact state does not match with target map." + " [oldState.getArtifactState().size() = " + oldState.getArtifactState().size() + ", fileToTargetMap.size() = " + fileToTargetMap.size() + "]");
}
}
}
ListenableFuture<?> downloadArtifactsFuture = RemoteArtifactPrefetcher.getInstance().downloadArtifacts(/* projectName= */
project.getName(), /* outputArtifacts= */
BlazeArtifact.getRemoteArtifacts(outputArtifacts));
ListenableFuture<?> fetchLocalFilesFuture = PrefetchService.getInstance().prefetchFiles(BlazeArtifact.getLocalFiles(outputArtifacts), true, false);
if (!FutureUtil.waitForFuture(context, Futures.allAsList(downloadArtifactsFuture, fetchLocalFilesFuture)).timed("FetchJdeps", EventType.Prefetching).withProgressMessage("Reading jdeps files...").run().success()) {
return null;
}
AtomicLong totalSizeLoaded = new AtomicLong(0);
List<ListenableFuture<Result>> futures = Lists.newArrayList();
for (OutputArtifact updatedFile : outputArtifacts) {
futures.add(FetchExecutor.EXECUTOR.submit(() -> {
totalSizeLoaded.addAndGet(updatedFile.getLength());
try (InputStream inputStream = updatedFile.getInputStream()) {
Deps.Dependencies dependencies = Deps.Dependencies.parseFrom(inputStream);
if (dependencies == null) {
return null;
}
List<String> deps = dependencies.getDependencyList().stream().filter(dep -> relevantDep(dep)).map(Dependency::getPath).collect(toImmutableList());
TargetKey targetKey = fileToTargetMap.get(updatedFile);
return new Result(updatedFile, targetKey, deps);
} catch (IOException e) {
logger.info("Could not read jdeps file: " + updatedFile);
return null;
}
}));
}
JdepsState.Builder state = JdepsState.builder();
if (oldState != null) {
state.list.addAll(oldState.data);
}
state.removeArtifacts(diff.getUpdatedOutputs().stream().map(OutputArtifact::toArtifactState).collect(toImmutableList()));
state.removeArtifacts(diff.getRemovedOutputs());
for (Result result : Futures.allAsList(futures).get()) {
if (result != null) {
state.list.add(JdepsData.create(result.targetKey, result.dependencies, result.output.toArtifactState()));
}
}
context.output(PrintOutput.log(String.format("Loaded %d jdeps files, total size %dkB", diff.getUpdatedOutputs().size(), totalSizeLoaded.get() / 1024)));
return state.build();
}
Aggregations