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);
}
}
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();
}
}
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();
}
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;
}
}
}
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"));
}
});
}
}
Aggregations