Skip to content

Commit

Permalink
Merge pull request #114 from chali/RemoveInternalGradleAPI
Browse files Browse the repository at this point in the history
Remove extending internal Gradle class and replace it with delegation…
  • Loading branch information
chali authored Nov 11, 2019
2 parents 79f19db + 5a081a1 commit 0c4e88d
Showing 1 changed file with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@
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.*;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.internal.ConfigureByMapAction;
import org.gradle.api.internal.DefaultNamedDomainObjectList;
import org.gradle.api.model.ObjectFactory;
import org.gradle.util.ConfigureUtil;
import org.gradle.util.GradleVersion;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

import static netflix.nebula.dependency.recommender.DependencyRecommendationsPlugin.CORE_BOM_SUPPORT_ENABLED;

public class RecommendationProviderContainer extends DefaultNamedDomainObjectList<RecommendationProvider> {
public class RecommendationProviderContainer {

private Project project;
private NamedDomainObjectList<RecommendationProvider> providers;
private RecommendationStrategies strategy = RecommendationStrategies.ConflictResolved;
private MavenBomRecommendationProvider mavenBomProvider;
private Boolean strictMode = false;
Expand All @@ -46,17 +48,32 @@ public class RecommendationProviderContainer extends DefaultNamedDomainObjectLis
public static final RecommendationStrategies OverrideTransitives = RecommendationStrategies.OverrideTransitives;
public static final RecommendationStrategies ConflictResolved = RecommendationStrategies.ConflictResolved;

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

public RecommendationProviderContainer(Project project) {
super(RecommendationProvider.class, null, new RecommendationProviderNamer());
if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.1")) >= 0) {
createList(project);
} else {
providers = new DefaultNamedDomainObjectList<RecommendationProvider>(RecommendationProvider.class, null, new RecommendationProviderNamer());
}
this.project = project;
this.mavenBomProvider = getMavenBomRecommendationProvider();
this.add(this.mavenBomProvider);
providers.add(this.mavenBomProvider);
}

private void createList(Project project) {
ObjectFactory objects = project.getObjects();
try {
Method factoryMethod = objects.getClass().getDeclaredMethod("namedDomainObjectList", Class.class);
providers = (NamedDomainObjectList<RecommendationProvider>) factoryMethod.invoke(objects, RecommendationProvider.class);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("NamedDomainObjectList couldn't be created", e);
}

}

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

private MavenBomRecommendationProvider getMavenBomRecommendationProvider() {
Expand All @@ -69,25 +86,22 @@ private MavenBomRecommendationProvider getMavenBomRecommendationProvider() {
return mavenBomRecommendationProvider;
}

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

public <T extends RecommendationProvider> T addProvider(T provider, Action<? super T> configureAction) {
configureAction.execute(provider);
assertCanAdd(provider.getName());
addLastAction.execute(provider);
providers.add(provider);
return provider;
}

public <T extends RecommendationProvider> T addFirst(T provider) {
remove(provider);
super.add(0, provider);
providers.remove(provider);
providers.add(0, provider);
return provider;
}

public RecommendationProvider getByName(String name) {
return providers.getByName(name);
}

public PropertyFileRecommendationProvider propertiesFile(Map<String, ?> args) {
ensureCoreBomSupportNotEnabled("propertiesFile");
String message = "nebula.dependency-recommender uses a properties file: " + args.get("file");
Expand Down Expand Up @@ -191,14 +205,14 @@ public MavenBomRecommendationProvider getMavenBomProvider() {

public String getRecommendedVersion(String group, String name) {
// providers are queried in LIFO order
for (int i = size()-1; i >= 0; i--) {
for (int i = providers.size()-1; i >= 0; i--) {
try {
String version = get(i).getVersion(group, name);
String version = providers.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);
project.getLogger().error("Exception while polling provider " + providers.get(i).getName() + " for version", e);
}
}
return null;
Expand Down Expand Up @@ -265,5 +279,4 @@ Set<File> getFilesOnConfiguration() {
rawPomDependencies.toArray(new org.gradle.api.artifacts.Dependency[0])).resolve();
}
}

}

0 comments on commit 0c4e88d

Please sign in to comment.