Search in sources :

Example 1 with Flavored

use of com.facebook.buck.model.Flavored in project buck by facebook.

the class AuditFlavorsCommand method printFlavors.

private void printFlavors(ImmutableList<TargetNode<?, ?>> targetNodes, CommandRunnerParams params) {
    DirtyPrintStreamDecorator stdout = params.getConsole().getStdOut();
    for (TargetNode<?, ?> node : targetNodes) {
        Description<?> description = node.getDescription();
        stdout.println(node.getBuildTarget().getFullyQualifiedName());
        if (description instanceof Flavored) {
            Optional<ImmutableSet<FlavorDomain<?>>> flavorDomains = ((Flavored) description).flavorDomains();
            if (flavorDomains.isPresent()) {
                for (FlavorDomain<?> domain : flavorDomains.get()) {
                    ImmutableSet<UserFlavor> userFlavors = RichStream.from(domain.getFlavors().stream()).filter(UserFlavor.class).collect(MoreCollectors.toImmutableSet());
                    if (userFlavors.isEmpty()) {
                        continue;
                    }
                    stdout.printf(" %s\n", domain.getName());
                    for (UserFlavor flavor : userFlavors) {
                        String flavorLine = String.format("  %s", flavor.getName());
                        String flavorDescription = flavor.getDescription();
                        if (flavorDescription.length() > 0) {
                            flavorLine += String.format(" -> %s", flavorDescription);
                        }
                        flavorLine += "\n";
                        stdout.printf(flavorLine);
                    }
                }
            } else {
                stdout.println(" unknown");
            }
        } else {
            stdout.println(" no flavors");
        }
    }
}
Also used : Flavored(com.facebook.buck.model.Flavored) ImmutableSet(com.google.common.collect.ImmutableSet) UserFlavor(com.facebook.buck.model.UserFlavor) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator)

Example 2 with Flavored

use of com.facebook.buck.model.Flavored in project buck by facebook.

the class DefaultParserTargetNodeFactory method createTargetNode.

@Override
public TargetNode<?, ?> createTargetNode(Cell cell, Path buildFile, BuildTarget target, Map<String, Object> rawNode, Function<PerfEventId, SimplePerfEvent.Scope> perfEventScope) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);
    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);
    UnflavoredBuildTarget unflavoredBuildTarget = target.withoutCell().getUnflavoredBuildTarget();
    if (target.isFlavored()) {
        if (description instanceof Flavored) {
            if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
                throw UnexpectedFlavorException.createWithSuggestions(cell, target);
            }
        } else {
            LOG.warn("Target %s (type %s) must implement the Flavored interface " + "before we can check if it supports flavors: %s", unflavoredBuildTarget, buildRuleType, target.getFlavors());
            throw new HumanReadableException("Target %s (type %s) does not currently support flavors (tried %s)", unflavoredBuildTarget, buildRuleType, target.getFlavors());
        }
    }
    UnflavoredBuildTarget unflavoredBuildTargetFromRawData = RawNodeParsePipeline.parseBuildTargetFromRawRule(cell.getRoot(), rawNode, buildFile);
    if (!unflavoredBuildTarget.equals(unflavoredBuildTargetFromRawData)) {
        throw new IllegalStateException(String.format("Inconsistent internal state, target from data: %s, expected: %s, raw data: %s", unflavoredBuildTargetFromRawData, unflavoredBuildTarget, Joiner.on(',').withKeyValueSeparator("->").join(rawNode)));
    }
    Cell targetCell = cell.getCell(target);
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<VisibilityPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("MarshalledConstructorArg"))) {
            marshaller.populate(targetCell.getCellPathResolver(), targetCell.getFilesystem(), target, constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("CreatedTargetNode"))) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            TargetNode<?, ?> node = targetNodeFactory.createFromObject(hasher.hash(), description, constructorArg, targetCell.getFilesystem(), target, declaredDeps.build(), visibilityPatterns.build(), targetCell.getCellPathResolver());
            if (buildFileTrees.isPresent() && cell.isEnforcingBuckPackageBoundaries(target.getBasePath())) {
                enforceBuckPackageBoundaries(target, buildFileTrees.get().getUnchecked(targetCell), node.getInputs());
            }
            nodeListener.onCreate(buildFile, node);
            return node;
        }
    } catch (NoSuchBuildTargetException e) {
        throw new HumanReadableException(e);
    } catch (ParamInfoException e) {
        throw new HumanReadableException(e, "%s: %s", target, e.getMessage());
    } catch (IOException e) {
        throw new HumanReadableException(e.getMessage(), e);
    }
}
Also used : UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) IOException(java.io.IOException) Flavored(com.facebook.buck.model.Flavored) VisibilityPattern(com.facebook.buck.rules.VisibilityPattern) ParamInfoException(com.facebook.buck.rules.ParamInfoException) Hasher(com.google.common.hash.Hasher) ImmutableSet(com.google.common.collect.ImmutableSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRuleType(com.facebook.buck.rules.BuildRuleType) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Cell(com.facebook.buck.rules.Cell)

Example 3 with Flavored

use of com.facebook.buck.model.Flavored in project buck by facebook.

the class AuditFlavorsCommand method printJsonFlavors.

private void printJsonFlavors(ImmutableList<TargetNode<?, ?>> targetNodes, CommandRunnerParams params) throws IOException {
    DirtyPrintStreamDecorator stdout = params.getConsole().getStdOut();
    SortedMap<String, SortedMap<String, SortedMap<String, String>>> targetsJson = new TreeMap<>();
    for (TargetNode<?, ?> node : targetNodes) {
        Description<?> description = node.getDescription();
        SortedMap<String, SortedMap<String, String>> flavorDomainsJson = new TreeMap<>();
        if (description instanceof Flavored) {
            Optional<ImmutableSet<FlavorDomain<?>>> flavorDomains = ((Flavored) description).flavorDomains();
            if (flavorDomains.isPresent()) {
                for (FlavorDomain<?> domain : flavorDomains.get()) {
                    ImmutableSet<UserFlavor> userFlavors = RichStream.from(domain.getFlavors().stream()).filter(UserFlavor.class).collect(MoreCollectors.toImmutableSet());
                    if (userFlavors.isEmpty()) {
                        continue;
                    }
                    SortedMap<String, String> flavorsJson = userFlavors.stream().collect(MoreCollectors.toImmutableSortedMap(UserFlavor::getName, UserFlavor::getDescription));
                    flavorDomainsJson.put(domain.getName(), flavorsJson);
                }
            } else {
                flavorDomainsJson.put("unknown", new TreeMap<>());
            }
        }
        String targetName = node.getBuildTarget().getFullyQualifiedName();
        targetsJson.put(targetName, flavorDomainsJson);
    }
    params.getObjectMapper().writeValue(stdout, targetsJson);
}
Also used : TreeMap(java.util.TreeMap) Flavored(com.facebook.buck.model.Flavored) ImmutableSet(com.google.common.collect.ImmutableSet) SortedMap(java.util.SortedMap) UserFlavor(com.facebook.buck.model.UserFlavor) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator)

Aggregations

Flavored (com.facebook.buck.model.Flavored)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 UserFlavor (com.facebook.buck.model.UserFlavor)2 DirtyPrintStreamDecorator (com.facebook.buck.util.DirtyPrintStreamDecorator)2 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)1 BuildRuleType (com.facebook.buck.rules.BuildRuleType)1 Cell (com.facebook.buck.rules.Cell)1 ParamInfoException (com.facebook.buck.rules.ParamInfoException)1 VisibilityPattern (com.facebook.buck.rules.VisibilityPattern)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 Hasher (com.google.common.hash.Hasher)1 IOException (java.io.IOException)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1