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
/*
 * Copyright 2014-2017 Netflix, Inc.
 *
 * 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 netflix.nebula.dependency.recommender.provider;

import com.netflix.nebula.dependencybase.DependencyManagement;
import groovy.lang.Closure;
import netflix.nebula.dependency.recommender.DependencyRecommendationsPlugin;
import netflix.nebula.dependency.recommender.RecommendationStrategies;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
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;
    private MavenBomRecommendationProvider mavenBomProvider;
    private DependencyManagement insight;
    private Boolean strictMode = false;
    
    // 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, DependencyManagement dependencyInsight) {
        super(RecommendationProvider.class, null, new RecommendationProviderNamer());
        this.project = project;
        this.insight = dependencyInsight;
        this.mavenBomProvider = new MavenBomRecommendationProvider(this.project, DependencyRecommendationsPlugin.NEBULA_RECOMMENDER_BOM, this.insight);
        this.add(this.mavenBomProvider);
    }

    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) {
        insight.addPluginMessage("nebula.dependency-recommender uses a properties file: " + args.get("file"));
        Map modifiedArgs = new HashMap(args);
        return add(new PropertyFileRecommendationProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public PropertyFileRecommendationProvider propertiesFile(Closure closure) {
        insight.addPluginMessage("nebula.dependency-recommender uses a properties file");
        return add(new PropertyFileRecommendationProvider(project), new ClosureBackedAction(closure));
    }

    public MavenBomRecommendationProvider mavenBom(Map args) {
        Object dependencyNotation = args.get("module");
        if(dependencyNotation == null) {
            throw new IllegalArgumentException("Module may not be null");
        }

        if(Map.class.isAssignableFrom(dependencyNotation.getClass())) {
            ((Map) dependencyNotation).put("ext", "pom");
        } else if(!dependencyNotation.toString().endsWith("@pom")) {
            dependencyNotation = dependencyNotation.toString() + "@pom";
        }
        project.getDependencies().add(DependencyRecommendationsPlugin.NEBULA_RECOMMENDER_BOM, dependencyNotation);

        return mavenBomProvider;
    }

    public IvyRecommendationProvider ivyXml(Map args) {
        insight.addPluginMessage("nebula.dependency-recommender uses a ivyXml: " + args.get("module"));
        Map modifiedArgs = new HashMap(args);
        return add(new IvyRecommendationProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public IvyRecommendationProvider ivyXml(Closure closure) {
        insight.addPluginMessage("nebula.dependency-recommender uses a ivyXml");
        return add(new IvyRecommendationProvider(project), new ClosureBackedAction(closure));
    }

    public DependencyLockProvider dependencyLock(Map args) {
        insight.addPluginMessage("nebula.dependency-recommender uses a dependency lock: " + args.get("module"));
        Map modifiedArgs = new HashMap(args);
        return add(new DependencyLockProvider(project), new ConfigureByMapAction(modifiedArgs));
    }

    public DependencyLockProvider dependencyLock(Closure closure) {
        insight.addPluginMessage("nebula.dependency-recommender uses a dependency lock for recommendations");
        return add(new DependencyLockProvider(project), new ClosureBackedAction(closure));
    }

    public MapRecommendationProvider map(Map args) {
        insight.addPluginMessage("nebula.dependency-recommender uses a provided map for recommendations");
        Map modifiedArgs = new HashMap(args);
        return add(new MapRecommendationProvider(), new ConfigureByMapAction(modifiedArgs));
    }

    public MapRecommendationProvider map(Closure closure) {
        insight.addPluginMessage("nebula.dependency-recommender uses a provided map for recommendations");
        return add(new MapRecommendationProvider(), new ClosureBackedAction(closure));
    }

    public CustomRecommendationProvider add(Closure closure) {
        insight.addPluginMessage("nebula.dependency-recommender uses a CustomRecommendationProvider");
        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;
    }

    public Boolean isStrictMode() {
        return strictMode;
    }

    public void setStrictMode(Boolean strict) {
        strictMode = strict;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy