Search in sources :

Example 6 with ResolvedVariant

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant in project gradle by gradle.

the class AttributeMatchingVariantSelector method doSelect.

private ResolvedArtifactSet doSelect(ResolvedVariantSet producer, Factory factory, AttributeMatchingExplanationBuilder explanationBuilder) {
    AttributeMatcher matcher = schema.withProducer(producer.getSchema());
    ImmutableAttributes componentRequested = attributesFactory.concat(requested, producer.getOverriddenAttributes());
    List<? extends ResolvedVariant> matches = matcher.matches(producer.getVariants(), componentRequested, explanationBuilder);
    if (matches.size() == 1) {
        return matches.get(0).getArtifacts();
    }
    if (matches.size() > 1) {
        if (explanationBuilder instanceof TraceDiscardedVariants) {
            Set<ResolvedVariant> discarded = Cast.uncheckedCast(((TraceDiscardedVariants) explanationBuilder).discarded);
            AttributeDescriber describer = DescriberSelector.selectDescriber(componentRequested, schema);
            throw new AmbiguousVariantSelectionException(describer, producer.asDescribable().getDisplayName(), componentRequested, matches, matcher, discarded);
        } else {
            // because we're going to fail, we can afford a second run with details
            return doSelect(producer, factory, new TraceDiscardedVariants());
        }
    }
    List<Pair<ResolvedVariant, MutableConsumerVariantMatchResult.ConsumerVariant>> candidates = new ArrayList<>();
    for (ResolvedVariant variant : producer.getVariants()) {
        AttributeContainerInternal variantAttributes = variant.getAttributes().asImmutable();
        ConsumerVariantMatchResult matchResult = consumerProvidedVariantFinder.collectConsumerVariants(variantAttributes, componentRequested);
        for (MutableConsumerVariantMatchResult.ConsumerVariant consumerVariant : matchResult.getMatches()) {
            candidates.add(Pair.of(variant, consumerVariant));
        }
    }
    if (candidates.size() > 1) {
        candidates = tryDisambiguate(matcher, candidates, componentRequested, explanationBuilder);
    }
    if (candidates.size() == 1) {
        Pair<ResolvedVariant, MutableConsumerVariantMatchResult.ConsumerVariant> result = candidates.get(0);
        ResolvedVariant variant = result.getLeft();
        VariantDefinition definition = result.getRight();
        return factory.asTransformed(variant, definition, dependenciesResolver, transformedVariantFactory);
    }
    if (!candidates.isEmpty()) {
        throw new AmbiguousTransformException(producer.asDescribable().getDisplayName(), componentRequested, candidates);
    }
    if (ignoreWhenNoMatches) {
        return ResolvedArtifactSet.EMPTY;
    }
    throw new NoMatchingVariantSelectionException(producer.asDescribable().getDisplayName(), componentRequested, producer.getVariants(), matcher, DescriberSelector.selectDescriber(componentRequested, schema));
}
Also used : AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) NoMatchingVariantSelectionException(org.gradle.internal.component.NoMatchingVariantSelectionException) ResolvedVariant(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant) ArrayList(java.util.ArrayList) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal) AmbiguousVariantSelectionException(org.gradle.internal.component.AmbiguousVariantSelectionException) AttributeDescriber(org.gradle.api.internal.attributes.AttributeDescriber) Pair(org.gradle.internal.Pair)

Example 7 with ResolvedVariant

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant in project gradle by gradle.

the class AttributeMatchingVariantSelector method disambiguateWithSchema.

private List<Pair<ResolvedVariant, MutableConsumerVariantMatchResult.ConsumerVariant>> disambiguateWithSchema(AttributeMatcher matcher, List<Pair<ResolvedVariant, MutableConsumerVariantMatchResult.ConsumerVariant>> candidates, ImmutableAttributes componentRequested, AttributeMatchingExplanationBuilder explanationBuilder) {
    List<AttributeContainerInternal> candidateAttributes = candidates.stream().map(pair -> pair.getRight().attributes).collect(Collectors.toList());
    List<AttributeContainerInternal> matches = matcher.matches(candidateAttributes, componentRequested, explanationBuilder);
    if (matches.size() == 1) {
        AttributeContainerInternal singleMatch = matches.get(0);
        return candidates.stream().filter(pair -> pair.getRight().attributes.equals(singleMatch)).collect(Collectors.toList());
    } else if (matches.size() > 0 && matches.size() < candidates.size()) {
        // We know all are compatibles, so this is only possible if some disambiguation happens but not getting us to 1 candidate
        return candidates.stream().filter(pair -> matches.contains(pair.getRight().attributes)).collect(Collectors.toList());
    }
    return candidates;
}
Also used : BrokenResolvedArtifactSet(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.BrokenResolvedArtifactSet) AttributesSchemaInternal(org.gradle.api.internal.attributes.AttributesSchemaInternal) AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) DescriberSelector(org.gradle.internal.component.model.DescriberSelector) ArrayList(java.util.ArrayList) Pair(org.gradle.internal.Pair) Lists(com.google.common.collect.Lists) Attribute(org.gradle.api.attributes.Attribute) AttributeDescriber(org.gradle.api.internal.attributes.AttributeDescriber) ResolvedArtifactSet(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedArtifactSet) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) ResolvedVariant(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant) ResolvedVariantSet(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariantSet) ImmutableAttributesFactory(org.gradle.api.internal.attributes.ImmutableAttributesFactory) Set(java.util.Set) Cast(org.gradle.internal.Cast) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) NoMatchingVariantSelectionException(org.gradle.internal.component.NoMatchingVariantSelectionException) AttributeMatchingExplanationBuilder(org.gradle.internal.component.model.AttributeMatchingExplanationBuilder) AttributeValue(org.gradle.api.internal.attributes.AttributeValue) Optional(java.util.Optional) VariantSelectionException(org.gradle.internal.component.VariantSelectionException) Comparator(java.util.Comparator) HasAttributes(org.gradle.api.attributes.HasAttributes) Collections(java.util.Collections) AmbiguousVariantSelectionException(org.gradle.internal.component.AmbiguousVariantSelectionException) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal)

Example 8 with ResolvedVariant

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant in project gradle by gradle.

the class AmbiguousTransformException method format.

private static String format(String producerDisplayName, AttributeContainerInternal requested, List<Pair<ResolvedVariant, ConsumerVariantMatchResult.ConsumerVariant>> candidates) {
    TreeFormatter formatter = new TreeFormatter();
    formatter.node("Found multiple transforms that can produce a variant of " + producerDisplayName + " with requested attributes");
    formatAttributes(formatter, requested);
    formatter.node("Found the following transforms");
    Map<ResolvedVariant, List<ConsumerVariantMatchResult.ConsumerVariant>> variantToTransforms = candidates.stream().collect(Collectors.toMap(Pair::getLeft, candidate -> Lists.newArrayList(candidate.getRight()), (List<ConsumerVariantMatchResult.ConsumerVariant> orig, List<ConsumerVariantMatchResult.ConsumerVariant> add) -> {
        orig.addAll(add);
        return orig;
    }, LinkedHashMap::new));
    formatter.startChildren();
    for (Map.Entry<ResolvedVariant, List<ConsumerVariantMatchResult.ConsumerVariant>> entry : variantToTransforms.entrySet()) {
        formatter.node("From '" + entry.getKey().asDescribable().getDisplayName() + "'");
        formatter.startChildren();
        formatter.node("With source attributes");
        formatAttributes(formatter, entry.getKey().getAttributes());
        formatter.node("Candidate transform(s)");
        formatter.startChildren();
        for (ConsumerVariantMatchResult.ConsumerVariant transform : entry.getValue()) {
            formatter.node("Transform '" + transform.transformation.getDisplayName() + "' producing attributes:");
            formatAttributes(formatter, transform.attributes);
        }
        formatter.endChildren();
        formatter.endChildren();
    }
    formatter.endChildren();
    return formatter.toString();
}
Also used : ResolvedVariant(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant) Pair(org.gradle.internal.Pair) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Lists(com.google.common.collect.Lists) AmbiguousVariantSelectionException.formatAttributes(org.gradle.internal.component.AmbiguousVariantSelectionException.formatAttributes) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) Map(java.util.Map) VariantSelectionException(org.gradle.internal.component.VariantSelectionException) Collectors(java.util.stream.Collectors) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) ResolvedVariant(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with ResolvedVariant

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant in project gradle by gradle.

the class ProjectArtifactSetResolver method resolveArtifacts.

/**
 * Creates an {@link ArtifactSet} that represents the available artifacts for the given set of project variants.
 */
public ArtifactSet resolveArtifacts(ComponentIdentifier componentIdentifier, ModuleVersionIdentifier ownerId, ModuleSources moduleSources, ExcludeSpec exclusions, Set<? extends VariantResolveMetadata> variants, AttributesSchemaInternal schema, ArtifactTypeRegistry artifactTypeRegistry, ImmutableAttributes selectionAttributes) {
    ImmutableSet.Builder<ResolvedVariant> result = ImmutableSet.builderWithExpectedSize(variants.size());
    for (VariantResolveMetadata variant : variants) {
        ResolvedVariant resolvedVariant = mapVariant(ownerId, moduleSources, exclusions, artifactTypeRegistry, variant);
        result.add(resolvedVariant);
    }
    return DefaultArtifactSet.createFromVariants(componentIdentifier, result.build(), schema, selectionAttributes);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ResolvedVariant(org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant) VariantResolveMetadata(org.gradle.internal.component.model.VariantResolveMetadata)

Aggregations

ResolvedVariant (org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant)9 AttributeContainerInternal (org.gradle.api.internal.attributes.AttributeContainerInternal)4 Pair (org.gradle.internal.Pair)4 ArrayList (java.util.ArrayList)3 AmbiguousVariantSelectionException (org.gradle.internal.component.AmbiguousVariantSelectionException)3 NoMatchingVariantSelectionException (org.gradle.internal.component.NoMatchingVariantSelectionException)3 AttributeMatcher (org.gradle.internal.component.model.AttributeMatcher)3 TreeFormatter (org.gradle.internal.logging.text.TreeFormatter)3 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AttributeDescriber (org.gradle.api.internal.attributes.AttributeDescriber)2 ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)2 VariantSelectionException (org.gradle.internal.component.VariantSelectionException)2 TreeFormatter (org.gradle.internal.text.TreeFormatter)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 LinkedHashMap (java.util.LinkedHashMap)1