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