Search in sources :

Example 26 with ImmutableAttributes

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;
}
Also used : AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) Action(org.gradle.api.Action) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) VariantVersionMappingStrategy(org.gradle.api.publish.VariantVersionMappingStrategy)

Example 27 with ImmutableAttributes

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;
}
Also used : AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) Action(org.gradle.api.Action) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) DefaultVariantVersionMappingStrategy(org.gradle.api.publish.internal.versionmapping.DefaultVariantVersionMappingStrategy) DefaultVariantVersionMappingStrategy(org.gradle.api.publish.internal.versionmapping.DefaultVariantVersionMappingStrategy) VariantVersionMappingStrategy(org.gradle.api.publish.VariantVersionMappingStrategy)

Example 28 with ImmutableAttributes

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);
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Exclusion(org.apache.maven.model.Exclusion) MavenDependency(org.gradle.api.publish.maven.MavenDependency) Dependency(org.apache.maven.model.Dependency) ExcludeRule(org.gradle.api.artifacts.ExcludeRule)

Example 29 with ImmutableAttributes

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);
        }
    }
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Transformer(org.gradle.api.Transformer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) VariantTransformRegistry(org.gradle.api.internal.artifacts.VariantTransformRegistry)

Example 30 with ImmutableAttributes

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();
}
Also used : ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Attribute(org.gradle.api.attributes.Attribute) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal)

Aggregations

ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)63 CoercingStringValueSnapshot (org.gradle.internal.snapshot.impl.CoercingStringValueSnapshot)9 AttributeContainerInternal (org.gradle.api.internal.attributes.AttributeContainerInternal)8 ArrayList (java.util.ArrayList)7 Attribute (org.gradle.api.attributes.Attribute)7 ConfigurationMetadata (org.gradle.internal.component.model.ConfigurationMetadata)7 ImmutableCapabilities (org.gradle.internal.component.external.model.ImmutableCapabilities)6 AttributeMatcher (org.gradle.internal.component.model.AttributeMatcher)6 ImmutableList (com.google.common.collect.ImmutableList)5 File (java.io.File)5 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)4 CapabilitiesMetadata (org.gradle.api.capabilities.CapabilitiesMetadata)4 ExcludeMetadata (org.gradle.internal.component.model.ExcludeMetadata)4 List (java.util.List)3 VersionConstraint (org.gradle.api.artifacts.VersionConstraint)3 DefaultImmutableVersionConstraint (org.gradle.api.internal.artifacts.dependencies.DefaultImmutableVersionConstraint)3 ModuleDependencyMetadata (org.gradle.internal.component.external.model.ModuleDependencyMetadata)3 MutableComponentVariant (org.gradle.internal.component.external.model.MutableComponentVariant)3 RealisedConfigurationMetadata (org.gradle.internal.component.external.model.RealisedConfigurationMetadata)3 VariantResolveMetadata (org.gradle.internal.component.model.VariantResolveMetadata)3