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

netflix.nebula.dependency.recommender.provider.RecommendationProviderContainer Maven / Gradle / Ivy

Go to download

Allows projects to leave off version numbers in dependencies section and have versions recommended by other sources

There is a newer version: 12.5.1
Show newest version
package netflix.nebula.dependency.recommender.provider;

import groovy.lang.Closure;
import netflix.nebula.dependency.recommender.RecommendationStrategies;
import org.gradle.api.Action;
import org.gradle.api.Namer;
import org.gradle.api.Project;
import org.gradle.api.internal.ClosureBackedAction;
import org.gradle.api.internal.ConfigureByMapAction;
import org.gradle.api.internal.DefaultNamedDomainObjectList;

import java.util.HashMap;
import java.util.Map;

public class RecommendationProviderContainer extends DefaultNamedDomainObjectList {

    private Project project;
    private RecommendationStrategies strategy = RecommendationStrategies.ConflictResolved;
    
    // Make strategies available without import
    public static final RecommendationStrategies OverrideTransitives = RecommendationStrategies.OverrideTransitives;
    public static final RecommendationStrategies ConflictResolved = RecommendationStrategies.ConflictResolved;

    private final Action addLastAction = new Action() {
        public void execute(RecommendationProvider r) {
            RecommendationProviderContainer.super.add(r);
        }
    };

    public RecommendationProviderContainer(Project project) {
        super(RecommendationProvider.class, null, new RecommendationProviderNamer());
        this.project = project;
    }

    private static class RecommendationProviderNamer implements Namer {
        public String determineName(RecommendationProvider r) {
            return r.getName();
        }
    }

    public  T add(T provider, Action configureAction) {
        configureAction.execute(provider);
        assertCanAdd(provider.getName());
        addLastAction.execute(provider);
        return provider;
    }

    public  T addFirst(T provider) {
        remove(provider);
        super.add(0, provider);
        return provider;
    }

    public PropertyFileRecommendationProvider propertiesFile(Map args) {
        Map modifiedArgs = new HashMap(args);
        return add(new PropertyFileRecommendationProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public PropertyFileRecommendationProvider propertiesFile(Closure closure) {
        return add(new PropertyFileRecommendationProvider(project), new ClosureBackedAction(closure));
    }

    public MavenBomRecommendationProvider mavenBom(Map args) {
        Map modifiedArgs = new HashMap(args);
        return add(new MavenBomRecommendationProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public MavenBomRecommendationProvider mavenBom(Closure closure) {
        return add(new MavenBomRecommendationProvider(project), new ClosureBackedAction(closure));
    }

    public IvyRecommendationProvider ivyXml(Map args) {
        Map modifiedArgs = new HashMap(args);
        return add(new IvyRecommendationProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public IvyRecommendationProvider ivyXml(Closure closure) {
        return add(new IvyRecommendationProvider(project), new ClosureBackedAction(closure));
    }

    public DependencyLockProvider dependencyLock(Map args) {
        Map modifiedArgs = new HashMap(args);
        return add(new DependencyLockProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public DependencyLockProvider dependencyLock(Closure closure) {
        return add(new DependencyLockProvider(project), new ClosureBackedAction(closure));
    }

    public MapRecommendationProvider map(Map args) {
        Map modifiedArgs = new HashMap(args);
        return add(new MapRecommendationProvider(), new ConfigureByMapAction(modifiedArgs));
    }

    public MapRecommendationProvider map(Closure closure) {
        return add(new MapRecommendationProvider(), new ClosureBackedAction(closure));
    }

    public CustomRecommendationProvider add(Closure closure) {
        return add(new CustomRecommendationProvider(closure), new Action() {
            @Override
            public void execute(CustomRecommendationProvider customRecommendationProvider) {
            }
        });
    }

    public String getRecommendedVersion(String group, String name) {
        // providers are queried in LIFO order
        for (int i = size()-1; i >= 0; i--) {
            try {
                String version = get(i).getVersion(group, name);
                if(version != null)
                    return version;
            } catch(Exception e) {
                project.getLogger().error("Exception while polling provider " + get(i).getName() + " for version", e);
            }
        }
        return null;
    }

    public RecommendationStrategies getStrategy() {
        return strategy;
    }

    public void setStrategy(RecommendationStrategies strategy) {
        this.strategy = strategy;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy