use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class RenderJarCache method getArtifactsToCache.
private ImmutableList<OutputArtifact> getArtifactsToCache(ProjectViewSet projectViewSet, BlazeProjectData projectData) {
List<ArtifactLocation> renderJars = BlazeImportUtil.getSourceTargetsStream(project, projectData, projectViewSet).map(TargetIdeInfo::getAndroidIdeInfo).filter(Objects::nonNull).map(AndroidIdeInfo::getRenderResolveJar).filter(Objects::nonNull).collect(Collectors.toList());
ArtifactLocationDecoder decoder = projectData.getArtifactLocationDecoder();
List<OutputArtifact> blazeArtifacts = decoder.resolveOutputs(renderJars).stream().filter(artifact -> artifact instanceof OutputArtifact).map(artifact -> (OutputArtifact) artifact).collect(Collectors.toList());
return ImmutableList.copyOf(blazeArtifacts);
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class BlazeApkDeployInfoProtoHelper method readDeployInfoProtoForTarget.
public AndroidDeployInfo readDeployInfoProtoForTarget(Label target, BuildResultHelper buildResultHelper, Predicate<String> pathFilter) throws GetDeployInfoException {
ImmutableList<File> deployInfoFiles;
try {
deployInfoFiles = BlazeArtifact.getLocalFiles(buildResultHelper.getBuildArtifactsForTarget(target, pathFilter));
} catch (GetArtifactsException e) {
throw new GetDeployInfoException(e.getMessage());
}
if (deployInfoFiles.isEmpty()) {
Logger log = Logger.getInstance(BlazeApkDeployInfoProtoHelper.class.getName());
try {
ParsedBepOutput bepOutput = buildResultHelper.getBuildOutput();
log.warn("Local execroot: " + bepOutput.getLocalExecRoot());
log.warn("All output artifacts:");
for (OutputArtifact outputArtifact : bepOutput.getAllOutputArtifacts(path -> true)) {
log.warn(outputArtifact.getKey() + " -> " + outputArtifact.getRelativePath());
}
log.warn("All local artifacts for " + target + ":");
List<OutputArtifact> allBuildArtifacts = buildResultHelper.getBuildArtifactsForTarget(target, path -> true);
List<File> allLocalFiles = BlazeArtifact.getLocalFiles(allBuildArtifacts);
for (File file : allLocalFiles) {
String path = file.getPath();
log.warn(path);
if (pathFilter.test(path)) {
log.warn("Note: " + path + " passes pathFilter but was not recognized!");
}
}
} catch (GetArtifactsException e) {
log.warn("Error occured when gathering logs:", e);
}
throw new GetDeployInfoException("No deploy info proto artifact found. Was android_deploy_info in the output groups?");
}
if (deployInfoFiles.size() > 1) {
throw new GetDeployInfoException("More than one deploy info proto artifact found: " + deployInfoFiles.stream().map(File::getPath).collect(Collectors.joining(", ", "[", "]")));
}
try (InputStream inputStream = new FileInputStream(deployInfoFiles.get(0))) {
return AndroidDeployInfo.parseFrom(inputStream);
} catch (IOException e) {
throw new GetDeployInfoException(e.getMessage());
}
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class LocalArtifactCacheTest method get_fetchesCorrectFileForArtifact.
@Test
public void get_fetchesCorrectFileForArtifact() 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();
}
// Add the artifacts to cache
artifactCache.initialize();
artifactCache.putAll(outputArtifacts, blazeContext, false);
// Attempt to get an arbitraty artifact
OutputArtifact artifactToFetch = outputArtifacts.get(1);
// Check that the returned file matches the expected file
File expectedFile = new File(cacheDirectory.getRoot(), CacheEntry.forArtifact(artifactToFetch).getFileName());
Path returnedPath = artifactCache.get(artifactToFetch);
assertThat(Collections.singleton(returnedPath)).doesNotContain(null);
assertThat(returnedPath.toFile()).isEqualTo(expectedFile);
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class BlazeBuildOutputs method updateOutputs.
/**
* Merges this {@link BlazeBuildOutputs} with a newer set of outputs.
*/
public BlazeBuildOutputs updateOutputs(BlazeBuildOutputs nextOutputs) {
// first combine common artifacts
Map<String, BepArtifactData> combined = new LinkedHashMap<>(artifacts);
for (Map.Entry<String, BepArtifactData> e : nextOutputs.artifacts.entrySet()) {
BepArtifactData a = e.getValue();
combined.compute(e.getKey(), (k, v) -> v == null ? a : v.update(a));
}
// data accordingly
for (String target : perTargetArtifacts.keySet()) {
if (!nextOutputs.perTargetArtifacts.containsKey(target)) {
continue;
}
Set<OutputArtifact> oldOutputs = perTargetArtifacts.get(target);
Set<OutputArtifact> newOutputs = nextOutputs.perTargetArtifacts.get(target);
// remove out of date target associations
for (OutputArtifact old : oldOutputs) {
if (newOutputs.contains(old)) {
continue;
}
// no longer output by this target; need to update target associations
BepArtifactData data = combined.get(old.getKey());
if (data != null) {
data = data.removeTargetAssociation(target);
}
if (data == null) {
combined.remove(old.getKey());
} else {
combined.put(old.getKey(), data);
}
}
}
return new BlazeBuildOutputs(BuildResult.combine(buildResult, nextOutputs.buildResult), combined, ImmutableList.<String>builder().addAll(buildIds).addAll(nextOutputs.buildIds).build());
}
use of com.google.idea.blaze.base.command.buildresult.OutputArtifact in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method updateState.
@Nullable
private static TargetMapAndInterfaceState updateState(Project project, BlazeContext parentContext, @Nullable BlazeIdeInterfaceState prevState, ArtifactsDiff fileState, BlazeConfigurationHandler configHandler, BlazeVersionData versionData, WorkspaceLanguageSettings languageSettings, ImportRoots importRoots, boolean mergeWithOldState, @Nullable TargetMap oldTargetMap) {
AspectStrategy aspectStrategy = AspectStrategy.getInstance(versionData);
Result<TargetMapAndInterfaceState> result = Scope.push(parentContext, context -> {
context.push(new TimingScope("UpdateTargetMap", EventType.Other));
context.output(new StatusOutput("Updating target map..."));
// ideally, we'd flush through a per-build sync time parsed from BEP. For now, though
// just set an approximate, batched sync time.
Instant syncTime = Instant.now();
Map<String, ArtifactState> nextFileState = new HashMap<>(fileState.getNewState());
// into the new one or we'll miss file removes next time
if (mergeWithOldState && prevState != null) {
prevState.ideInfoFileState.forEach(nextFileState::putIfAbsent);
}
BlazeIdeInterfaceState.Builder state = BlazeIdeInterfaceState.builder();
state.ideInfoFileState = ImmutableMap.copyOf(nextFileState);
Map<TargetKey, TargetIdeInfo> targetMap = Maps.newHashMap();
if (prevState != null && oldTargetMap != null) {
targetMap.putAll(oldTargetMap.map());
state.ideInfoToTargetKey.putAll(prevState.ideInfoFileToTargetKey);
}
// Update removed unless we're merging with the old state
if (!mergeWithOldState) {
for (ArtifactState removed : fileState.getRemovedOutputs()) {
TargetKey key = state.ideInfoToTargetKey.remove(removed.getKey());
if (key != null) {
targetMap.remove(key);
}
}
}
AtomicLong totalSizeLoaded = new AtomicLong(0);
Set<LanguageClass> ignoredLanguages = Sets.newConcurrentHashSet();
ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();
// Read protos from any new files
List<ListenableFuture<TargetFilePair>> futures = Lists.newArrayList();
for (OutputArtifact file : fileState.getUpdatedOutputs()) {
futures.add(executor.submit(() -> {
totalSizeLoaded.addAndGet(file.getLength());
IntellijIdeInfo.TargetIdeInfo message = aspectStrategy.readAspectFile(file);
TargetIdeInfo target = protoToTarget(languageSettings, importRoots, message, ignoredLanguages, syncTime);
return new TargetFilePair(file, target);
}));
}
Set<TargetKey> newTargets = new HashSet<>();
Set<String> configurations = new LinkedHashSet<>();
configurations.add(configHandler.defaultConfigurationPathComponent);
// Update state with result from proto files
int duplicateTargetLabels = 0;
try {
for (TargetFilePair targetFilePair : Futures.allAsList(futures).get()) {
if (targetFilePair.target != null) {
OutputArtifact file = targetFilePair.file;
String config = file.getConfigurationMnemonic();
configurations.add(config);
TargetKey key = targetFilePair.target.getKey();
if (targetMap.putIfAbsent(key, targetFilePair.target) == null) {
state.ideInfoToTargetKey.forcePut(file.getKey(), key);
} else {
if (!newTargets.add(key)) {
duplicateTargetLabels++;
}
// prioritize the default configuration over build order
if (Objects.equals(config, configHandler.defaultConfigurationPathComponent)) {
targetMap.put(key, targetFilePair.target);
state.ideInfoToTargetKey.forcePut(file.getKey(), key);
}
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Result.error(null);
} catch (ExecutionException e) {
return Result.error(e);
}
context.output(PrintOutput.log(String.format("Loaded %d aspect files, total size %dkB", fileState.getUpdatedOutputs().size(), totalSizeLoaded.get() / 1024)));
if (duplicateTargetLabels > 0) {
context.output(new PerformanceWarning(String.format("There were %d duplicate rules, built with the following " + "configurations: %s.\nYour IDE sync is slowed down by ~%d%%.", duplicateTargetLabels, configurations, (100 * duplicateTargetLabels / targetMap.size()))));
}
// remove previously synced targets which are now unsupported
for (TargetKey key : ImmutableSet.copyOf(state.ideInfoToTargetKey.values())) {
TargetIdeInfo target = targetMap.get(key);
if (target != null && shouldIgnoreTarget(languageSettings, importRoots, target, ignoredLanguages)) {
state.ideInfoToTargetKey.inverse().remove(key);
targetMap.remove(key);
}
}
// update sync time for unchanged targets
for (String artifactKey : fileState.getNewState().keySet()) {
TargetKey targetKey = state.ideInfoToTargetKey.get(artifactKey);
TargetIdeInfo target = targetKey != null ? targetMap.get(targetKey) : null;
if (target != null) {
targetMap.put(targetKey, target.updateSyncTime(syncTime));
}
}
ignoredLanguages.retainAll(LanguageSupport.availableAdditionalLanguages(languageSettings.getWorkspaceType()));
warnIgnoredLanguages(project, context, ignoredLanguages);
return Result.of(new TargetMapAndInterfaceState(new TargetMap(ImmutableMap.copyOf(targetMap)), state.build()));
});
if (result.error != null) {
logger.error(result.error);
return null;
}
return result.result;
}
Aggregations