Search in sources :

Example 6 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class ComponentAttributeMatcher method doMatchCandidate.

private void doMatchCandidate(AttributesSchemaInternal consumerAttributeSchema, AttributesSchemaInternal producerAttributeSchema, HasAttributes candidate, AttributeContainer requested, MatchDetails details) {
    Set<Attribute<Object>> requestedAttributes = Cast.uncheckedCast(requested.keySet());
    AttributeContainer candidateAttributesContainer = candidate.getAttributes();
    Set<Attribute<Object>> candidateAttributes = Cast.uncheckedCast(candidateAttributesContainer.keySet());
    Set<Attribute<Object>> allAttributes = Sets.union(requestedAttributes, candidateAttributes);
    for (Attribute<Object> attribute : allAttributes) {
        AttributeValue<Object> requestedValue = attributeValue(attribute, consumerAttributeSchema, requested);
        AttributeValue<Object> actualValue = attributeValue(attribute, producerAttributeSchema, candidateAttributesContainer);
        if (!requestedValue.isPresent() && ignoreAdditionalProducerAttributes) {
            details.matchesByAttribute.put(attribute, actualValue.get());
            continue;
        }
        if (!actualValue.isPresent() && ignoreAdditionalConsumerAttributes) {
            continue;
        }
        details.update(attribute, consumerAttributeSchema, producerAttributeSchema, requestedValue, actualValue);
    }
}
Also used : Attribute(org.gradle.api.attributes.Attribute) AttributeContainer(org.gradle.api.attributes.AttributeContainer)

Example 7 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class AmbiguousConfigurationSelectionException method formatConfiguration.

static void formatConfiguration(TreeFormatter formatter, AttributeContainer fromConfigurationAttributes, AttributesSchema consumerSchema, List<? extends ConfigurationMetadata> matches, Set<String> requestedAttributes, final String conf) {
    Optional<? extends ConfigurationMetadata> match = Iterables.tryFind(matches, new Predicate<ConfigurationMetadata>() {

        @Override
        public boolean apply(ConfigurationMetadata input) {
            return conf.equals(input.getName());
        }
    });
    if (match.isPresent()) {
        AttributeContainer producerAttributes = match.get().getAttributes();
        Set<Attribute<?>> targetAttributes = producerAttributes.keySet();
        Set<String> targetAttributeNames = Sets.newTreeSet(Iterables.transform(targetAttributes, ATTRIBUTE_NAME));
        Set<Attribute<?>> allAttributes = Sets.union(fromConfigurationAttributes.keySet(), producerAttributes.keySet());
        Set<String> commonAttributes = Sets.intersection(requestedAttributes, targetAttributeNames);
        Set<String> consumerOnlyAttributes = Sets.difference(requestedAttributes, targetAttributeNames);
        formatter.node("Configuration '");
        formatter.append(conf);
        formatter.append("'");
        formatter.startChildren();
        List<Attribute<?>> sortedAttributes = Ordering.usingToString().sortedCopy(allAttributes);
        formatAttributes(formatter, fromConfigurationAttributes, consumerSchema, producerAttributes, commonAttributes, consumerOnlyAttributes, sortedAttributes);
        formatter.endChildren();
    }
}
Also used : Attribute(org.gradle.api.attributes.Attribute) AttributeContainer(org.gradle.api.attributes.AttributeContainer) ConfigurationMetadata(org.gradle.internal.component.model.ConfigurationMetadata)

Example 8 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class ComponentState method desugarAttributes.

/**
 * Desugars attributes so that what we're going to serialize consists only of String or Boolean attributes,
 * and not their original types.
 * @param selected the selected component
 * @return desugared attributes
 */
private ImmutableAttributes desugarAttributes(NodeState selected) {
    ImmutableAttributes attributes = selected.getMetadata().getAttributes();
    if (attributes.isEmpty()) {
        return attributes;
    }
    AttributeContainerInternal mutable = selected.getAttributesFactory().mutable();
    Set<Attribute<?>> keySet = attributes.keySet();
    for (Attribute<?> attribute : keySet) {
        Object value = attributes.getAttribute(attribute);
        Attribute<Object> desugared = Cast.uncheckedCast(attribute);
        if (attribute.getType() == Boolean.class || attribute.getType() == String.class) {
            mutable.attribute(desugared, value);
        } else {
            desugared = Cast.uncheckedCast(Attribute.of(attribute.getName(), String.class));
            mutable.attribute(desugared, value.toString());
        }
    }
    return mutable.asImmutable();
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Attribute(org.gradle.api.attributes.Attribute) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal)

Example 9 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class MultipleCandidateMatcher method collectExtraAttributes.

/**
 * Collects attributes that were present on the candidates, but which the consumer did
 * not ask for. Some of these attributes might be weakly typed, e.g. coming as Strings
 * from an artifact repository. We always check whether the schema has a more strongly
 * typed version of an attribute and use that one instead to apply its disambiguation
 * rules.
 */
private void collectExtraAttributes() {
    Set<Attribute<?>> extraAttributes = Sets.newLinkedHashSet();
    for (ImmutableAttributes attributes : candidateAttributeSets) {
        extraAttributes.addAll(attributes.keySet());
    }
    extraAttributes.removeAll(requested.keySet());
    this.extraAttributes = extraAttributes.toArray(new Attribute[0]);
    for (int i = 0; i < this.extraAttributes.length; i++) {
        Attribute<?> extraAttribute = this.extraAttributes[i];
        Attribute<?> schemaAttribute = schema.getAttribute(extraAttribute.getName());
        if (schemaAttribute != null) {
            this.extraAttributes[i] = schemaAttribute;
        }
    }
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Attribute(org.gradle.api.attributes.Attribute)

Example 10 with Attribute

use of org.gradle.api.attributes.Attribute in project gradle by gradle.

the class PublicationErrorChecker method checkForUnpublishableAttributes.

/**
 * Checks that the given component does not have any attributes that are not allowed to be published.
 *
 * @param component the component to check
 * @param documentationRegistry for creating helpful links in error messages upon failing the check
 * @throws PublishException if the component uses attributes invalid for publication
 */
public static void checkForUnpublishableAttributes(SoftwareComponentInternal component, DocumentationRegistry documentationRegistry) {
    for (final UsageContext usageContext : component.getUsages()) {
        Optional<Attribute<?>> category = usageContext.getAttributes().keySet().stream().filter(a -> Category.CATEGORY_ATTRIBUTE.getName().equals(a.getName())).findFirst();
        category.ifPresent(c -> {
            Object value = usageContext.getAttributes().getAttribute(c);
            if (value != null && Category.VERIFICATION.equals(value.toString())) {
                throw new PublishException("Cannot publish module metadata for component '" + component.getName() + "' which would include a variant '" + usageContext.getName() + "' that contains a '" + Category.CATEGORY_ATTRIBUTE.getName() + "' attribute with a value of '" + Category.VERIFICATION + "'.  This attribute is reserved for test verification output and is not publishable.  See: " + documentationRegistry.getDocumentationFor("variant_attributes.html", "sec:verification_category"));
            }
        });
    }
}
Also used : SoftwareComponentInternal(org.gradle.api.internal.component.SoftwareComponentInternal) Attribute(org.gradle.api.attributes.Attribute) Category(org.gradle.api.attributes.Category) Optional(java.util.Optional) DocumentationRegistry(org.gradle.api.internal.DocumentationRegistry) UsageContext(org.gradle.api.internal.component.UsageContext) PublishException(org.gradle.api.artifacts.PublishException) UsageContext(org.gradle.api.internal.component.UsageContext) Attribute(org.gradle.api.attributes.Attribute) PublishException(org.gradle.api.artifacts.PublishException)

Aggregations

Attribute (org.gradle.api.attributes.Attribute)12 ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)6 TreeMap (java.util.TreeMap)2 AttributeContainer (org.gradle.api.attributes.AttributeContainer)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Named (org.gradle.api.Named)1 PublishException (org.gradle.api.artifacts.PublishException)1 Category (org.gradle.api.attributes.Category)1 DocumentationRegistry (org.gradle.api.internal.DocumentationRegistry)1 AttributeContainerInternal (org.gradle.api.internal.attributes.AttributeContainerInternal)1 AttributeDescriber (org.gradle.api.internal.attributes.AttributeDescriber)1 SoftwareComponentInternal (org.gradle.api.internal.component.SoftwareComponentInternal)1 UsageContext (org.gradle.api.internal.component.UsageContext)1 ConfigurationMetadata (org.gradle.internal.component.model.ConfigurationMetadata)1