All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.internalivyservice.ivyresolve.ComponentSelectionRulesProcessor Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.artifacts.ivyservice.ivyresolve;

import org.gradle.api.InvalidUserCodeException;
import org.gradle.api.artifacts.ComponentMetadata;
import org.gradle.api.artifacts.ComponentSelection;
import org.gradle.api.artifacts.ivy.IvyModuleDescriptor;
import org.gradle.api.internal.artifacts.ComponentSelectionInternal;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.gradle.internal.rules.SpecRuleAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ComponentSelectionRulesProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(ComponentSelectionRulesProcessor.class);

    private final Spec> withNoInputs = new Spec>() {
        public boolean isSatisfiedBy(SpecRuleAction element) {
            return element.getAction().getInputTypes().isEmpty();
        }
    };
    private final Spec> withInputs = Specs.negate(withNoInputs);

    void apply(ComponentSelectionInternal selection, Collection> specRuleActions, MetadataProvider metadataProvider) {
        if (processRules(specRuleActions, withNoInputs, selection, metadataProvider)) {
            processRules(specRuleActions, withInputs, selection, metadataProvider);
        }
    }

    private boolean processRules(Collection> specRuleActions, Spec> filter, ComponentSelectionInternal selection, MetadataProvider metadataProvider) {
        for (SpecRuleAction rule : specRuleActions) {
            if (filter.isSatisfiedBy(rule)) {
                processRule(rule, selection, metadataProvider);

                if (selection.isRejected()) {
                    LOGGER.info("Selection of {} rejected by component selection rule: {}", selection.getCandidate().getDisplayName(), selection.getRejectionReason());
                    return false;
                }
            }
        }
        return true;
    }

    private void processRule(SpecRuleAction rule, ComponentSelection selection, MetadataProvider metadataProvider) {
        if (!rule.getSpec().isSatisfiedBy(selection)) {
            return;
        }

        List inputValues = getInputValues(rule.getAction().getInputTypes(), metadataProvider);

        if (inputValues == null) {
            // Broken meta-data, bail
            return;
        }

        if (inputValues.contains(null)) {
            // If any of the input values are not available for this selection, ignore the rule
            return;
        }

        try {
            rule.getAction().execute(selection, inputValues);
        } catch (Exception e) {
            throw new InvalidUserCodeException(String.format("There was an error while evaluating a component selection rule for %s.", selection.getCandidate().getDisplayName()), e);
        }
    }

    private List getInputValues(List> inputTypes, MetadataProvider metadataProvider) {
        if (inputTypes.size() == 0) {
            return Collections.emptyList();
        }

        if (!metadataProvider.isUsable()) {
            return null;
        }

        List inputs = new ArrayList(inputTypes.size());
        for (Class inputType : inputTypes) {
            if (inputType == ComponentMetadata.class) {
                inputs.add(metadataProvider.getComponentMetadata());
                continue;
            }
            if (inputType == IvyModuleDescriptor.class) {
                inputs.add(metadataProvider.getIvyModuleDescriptor());
                continue;
            }
            // We've already validated the inputs: should never get here.
            throw new IllegalStateException();
        }
        return inputs;
    }
}