use of org.gradle.api.internal.attributes.ImmutableAttributes in project gradle by gradle.
the class DefaultVersionMappingStrategy method findStrategyForVariant.
@Override
public VariantVersionMappingStrategyInternal findStrategyForVariant(ImmutableAttributes variantAttributes) {
DefaultVariantVersionMappingStrategy strategy = createDefaultMappingStrategy(variantAttributes);
// Apply strategies for "all variants"
for (Action<? super VariantVersionMappingStrategy> action : mappingsForAllVariants) {
action.execute(strategy);
}
// Then use attribute specific mapping
if (!attributeBasedMappings.isEmpty()) {
AttributeMatcher matcher = schema.matcher();
Set<ImmutableAttributes> candidates = attributeBasedMappings.keySet();
List<ImmutableAttributes> matches = matcher.matches(candidates, variantAttributes, AttributeMatchingExplanationBuilder.NO_OP);
if (matches.size() == 1) {
Collection<Action<? super VariantVersionMappingStrategy>> actions = attributeBasedMappings.get(matches.get(0));
for (Action<? super VariantVersionMappingStrategy> action : actions) {
action.execute(strategy);
}
} else if (matches.size() > 1) {
throw new InvalidUserCodeException("Unable to find a suitable version mapping strategy for " + variantAttributes);
}
}
return strategy;
}
use of org.gradle.api.internal.attributes.ImmutableAttributes in project gradle by gradle.
the class DefaultVersionMappingStrategy method findStrategyForVariant.
@Override
public VariantVersionMappingStrategyInternal findStrategyForVariant(ImmutableAttributes variantAttributes) {
DefaultVariantVersionMappingStrategy strategy = createDefaultMappingStrategy(variantAttributes);
// Apply strategies for "all variants"
for (Action<? super VariantVersionMappingStrategy> action : mappingsForAllVariants) {
action.execute(strategy);
}
// Then use attribute specific mapping
if (!attributeBasedMappings.isEmpty()) {
AttributeMatcher matcher = schema.matcher();
Set<ImmutableAttributes> candidates = attributeBasedMappings.keySet();
List<ImmutableAttributes> matches = matcher.matches(candidates, variantAttributes, AttributeMatchingExplanationBuilder.NO_OP);
if (matches.size() == 1) {
Collection<Action<? super VariantVersionMappingStrategy>> actions = attributeBasedMappings.get(matches.get(0));
for (Action<? super VariantVersionMappingStrategy> action : actions) {
action.execute(strategy);
}
} else if (matches.size() > 1) {
throw new InvalidUserCodeException("Unable to find a suitable version mapping strategy for " + variantAttributes);
}
}
return strategy;
}
use of org.gradle.api.internal.attributes.ImmutableAttributes in project gradle by gradle.
the class MavenPomFileGenerator method addDependency.
private void addDependency(MavenDependencyInternal dependency, String artifactId, String scope, String type, String classifier, boolean optional) {
Dependency mavenDependency = new Dependency();
String groupId = dependency.getGroupId();
String dependencyVersion = dependency.getVersion();
String projectPath = dependency.getProjectPath();
ImmutableAttributes attributes = attributesForScope(scope);
ModuleVersionIdentifier resolvedVersion = versionMappingStrategy.findStrategyForVariant(attributes).maybeResolveVersion(groupId, artifactId, projectPath);
mavenDependency.setGroupId(resolvedVersion != null ? resolvedVersion.getGroup() : groupId);
mavenDependency.setArtifactId(resolvedVersion != null ? resolvedVersion.getName() : artifactId);
mavenDependency.setVersion(resolvedVersion != null ? resolvedVersion.getVersion() : mapToMavenSyntax(dependencyVersion));
mavenDependency.setType(type);
mavenDependency.setScope(scope);
mavenDependency.setClassifier(classifier);
if (optional) {
// Not using setOptional(optional) in order to avoid <optional>false</optional> in the common case
mavenDependency.setOptional(true);
}
for (ExcludeRule excludeRule : dependency.getExcludeRules()) {
Exclusion exclusion = new Exclusion();
exclusion.setGroupId(GUtil.elvis(excludeRule.getGroup(), "*"));
exclusion.setArtifactId(GUtil.elvis(excludeRule.getModule(), "*"));
mavenDependency.addExclusion(exclusion);
}
model.addDependency(mavenDependency);
}
use of org.gradle.api.internal.attributes.ImmutableAttributes in project gradle by gradle.
the class VariantAttributeMatchingCache method findProducersFor.
private void findProducersFor(AttributeContainerInternal actual, AttributeContainerInternal requested, ConsumerVariantMatchResult result) {
// Prefer direct transformation over indirect transformation
List<VariantTransformRegistry.Registration> candidates = new ArrayList<VariantTransformRegistry.Registration>();
for (VariantTransformRegistry.Registration transform : variantTransforms.getTransforms()) {
if (matchAttributes(transform.getTo(), requested, false)) {
if (matchAttributes(actual, transform.getFrom(), true)) {
ImmutableAttributes variantAttributes = attributesFactory.concat(actual.asImmutable(), transform.getTo().asImmutable());
result.matched(variantAttributes, transform.getArtifactTransform(), 1);
}
candidates.add(transform);
}
}
if (result.hasMatches()) {
return;
}
for (final VariantTransformRegistry.Registration candidate : candidates) {
ConsumerVariantMatchResult inputVariants = new ConsumerVariantMatchResult();
collectConsumerVariants(actual, candidate.getFrom(), inputVariants);
if (!inputVariants.hasMatches()) {
continue;
}
for (final ConsumerVariantMatchResult.ConsumerVariant inputVariant : inputVariants.getMatches()) {
ImmutableAttributes variantAttributes = attributesFactory.concat(inputVariant.attributes.asImmutable(), candidate.getTo().asImmutable());
Transformer<List<File>, File> transformer = new Transformer<List<File>, File>() {
@Override
public List<File> transform(File file) {
List<File> result = new ArrayList<File>();
for (File intermediate : inputVariant.transformer.transform(file)) {
result.addAll(candidate.getArtifactTransform().transform(intermediate));
}
return result;
}
};
result.matched(variantAttributes, transformer, inputVariant.depth + 1);
}
}
}
use of org.gradle.api.internal.attributes.ImmutableAttributes 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();
}
Aggregations