Search in sources :

Example 1 with OutputArtifact

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);
}
Also used : TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ProjectViewManager(com.google.idea.blaze.base.projectview.ProjectViewManager) LocalArtifactCache(com.google.idea.blaze.android.filecache.LocalArtifactCache) FileCache(com.google.idea.blaze.base.filecache.FileCache) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ImmutableList(com.google.common.collect.ImmutableList) RenderResolveOutputGroupProvider(com.google.idea.blaze.android.sync.aspects.strategy.RenderResolveOutputGroupProvider) BlazeDataStorage(com.google.idea.blaze.base.sync.data.BlazeDataStorage) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Project(com.intellij.openapi.project.Project) BlazeBuildOutputs(com.google.idea.blaze.base.sync.aspects.BlazeBuildOutputs) Logger(com.intellij.openapi.diagnostic.Logger) Path(java.nio.file.Path) RenderJarClassFileFinder(com.google.idea.blaze.android.projectsystem.RenderJarClassFileFinder) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) BlazeImportSettingsManager(com.google.idea.blaze.base.settings.BlazeImportSettingsManager) BlazeImportUtil(com.google.idea.blaze.android.sync.importer.BlazeImportUtil) Collectors(java.util.stream.Collectors) BlazeProjectDataManager(com.google.idea.blaze.base.sync.data.BlazeProjectDataManager) File(java.io.File) BlazeArtifact(com.google.idea.blaze.base.command.buildresult.BlazeArtifact) BlazeImportSettings(com.google.idea.blaze.base.settings.BlazeImportSettings) ArtifactCache(com.google.idea.blaze.android.filecache.ArtifactCache) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) SyncMode(com.google.idea.blaze.base.sync.SyncMode) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact)

Example 2 with OutputArtifact

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());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ParsedBepOutput(com.google.idea.blaze.base.command.buildresult.ParsedBepOutput) IOException(java.io.IOException) Logger(com.intellij.openapi.diagnostic.Logger) FileInputStream(java.io.FileInputStream) GetArtifactsException(com.google.idea.blaze.base.command.buildresult.BuildResultHelper.GetArtifactsException) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) File(java.io.File)

Example 3 with OutputArtifact

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);
}
Also used : Path(java.nio.file.Path) LocalFileOutputArtifact(com.google.idea.blaze.base.command.buildresult.LocalFileOutputArtifact) LocalFileOutputArtifact(com.google.idea.blaze.base.command.buildresult.LocalFileOutputArtifact) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) File(java.io.File) Test(org.junit.Test)

Example 4 with OutputArtifact

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());
}
Also used : OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) BepArtifactData(com.google.idea.blaze.base.command.buildresult.BepArtifactData) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with OutputArtifact

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ArtifactState(com.google.idea.blaze.base.filecache.ArtifactState) PerformanceWarning(com.google.idea.blaze.base.scope.output.PerformanceWarning) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) OutputArtifact(com.google.idea.blaze.base.command.buildresult.OutputArtifact) ExecutionException(java.util.concurrent.ExecutionException) LanguageClass(com.google.idea.blaze.base.model.primitives.LanguageClass) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Instant(java.time.Instant) StatusOutput(com.google.idea.blaze.base.scope.output.StatusOutput) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) AspectStrategy(com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Nullable(javax.annotation.Nullable)

Aggregations

OutputArtifact (com.google.idea.blaze.base.command.buildresult.OutputArtifact)13 BlazeArtifact (com.google.idea.blaze.base.command.buildresult.BlazeArtifact)6 File (java.io.File)6 ArtifactState (com.google.idea.blaze.base.filecache.ArtifactState)5 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 ArtifactsDiff (com.google.idea.blaze.base.filecache.ArtifactsDiff)4 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)4 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)4 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)4 Path (java.nio.file.Path)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimingScope (com.google.idea.blaze.base.scope.scopes.TimingScope)3 Logger (com.intellij.openapi.diagnostic.Logger)3 HashSet (java.util.HashSet)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2