-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
232 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package netflix.nebula.dependency.recommender; | ||
|
||
public enum RecommendationStrategies { | ||
ConflictResolved(RecommendationsConflictResolvedStrategy.class), | ||
OverrideTransitives(RecommendationsOverrideTransitivesStrategy.class); | ||
|
||
private Class<? extends RecommendationStrategy> strategyClass; | ||
|
||
RecommendationStrategies(Class<? extends RecommendationStrategy> strategyClass) { | ||
this.strategyClass = strategyClass; | ||
} | ||
|
||
public Class<? extends RecommendationStrategy> getStrategyClass() { | ||
return strategyClass; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package netflix.nebula.dependency.recommender; | ||
|
||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.artifacts.DependencyResolveDetails; | ||
import org.gradle.api.artifacts.ModuleVersionSelector; | ||
|
||
/** | ||
* Defines in which cases recommendations are applied | ||
*/ | ||
public abstract class RecommendationStrategy { | ||
|
||
/** | ||
* This hook is called for each dependency in a project. It collects the dependencies we are interested in for determining if a recommendation should be applied. | ||
* @param dependency the dependency to inspect. | ||
*/ | ||
public abstract void inspectDependency(Dependency dependency); | ||
|
||
/** | ||
* Puts the recommended version on details.useVersion depending on the strategy used | ||
* @param details the details to recommend a version for | ||
* @param version the version to be potentially recommended for the requested artifact | ||
* @return <code>true</code> if a version has been recommended, <code>false</code> otherwise | ||
*/ | ||
public abstract boolean recommendVersion(DependencyResolveDetails details, String version); | ||
|
||
/** | ||
* @param details the details to get coordinates from | ||
* @return the coordinates in the form of "<group>:<name>", taken from details.requested. | ||
*/ | ||
protected String getCoord(DependencyResolveDetails details) { | ||
ModuleVersionSelector requested = details.getRequested(); | ||
return requested.getGroup() + ":" + requested.getName(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package netflix.nebula.dependency.recommender; | ||
|
||
import netflix.nebula.dependency.recommender.provider.RecommendationProviderContainer; | ||
import org.gradle.api.Project; | ||
|
||
/** | ||
* Creates RecommendationStrategy lazily on demand and caches it. | ||
* This is used to allow for scoped recommendationStrategies (e.g. per configuration as in DependencyRecommendationsPlugin) | ||
*/ | ||
public class RecommendationStrategyFactory { | ||
private final Project project; | ||
private RecommendationStrategy recommendationStrategy; | ||
|
||
public RecommendationStrategyFactory(Project project) { | ||
this.project = project; | ||
} | ||
|
||
public RecommendationStrategy getRecommendationStrategy() { | ||
if(recommendationStrategy == null) { | ||
try { | ||
RecommendationProviderContainer recommendationProviderContainer = project.getExtensions().getByType(RecommendationProviderContainer.class); | ||
recommendationStrategy = recommendationProviderContainer.getStrategy().getStrategyClass().newInstance(); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
return recommendationStrategy; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...groovy/netflix/nebula/dependency/recommender/RecommendationsConflictResolvedStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package netflix.nebula.dependency.recommender; | ||
|
||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.artifacts.DependencyResolveDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Recommendations are conflict resolved against transitive dependencies. In the event that a transitive dependency | ||
* has a higher version of a library than a recommendation, the recommendation only wins if there is a first order | ||
* recommend-able dependency. | ||
*/ | ||
public class RecommendationsConflictResolvedStrategy extends RecommendationStrategy { | ||
|
||
private List<String> firstOrderDepsWithoutVersions = new ArrayList<>(); | ||
|
||
@Override | ||
public void inspectDependency(Dependency dependency) { | ||
if (dependency.getVersion() == null || dependency.getVersion().isEmpty()) { | ||
firstOrderDepsWithoutVersions.add(dependency.getGroup() + ":" + dependency.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean recommendVersion(DependencyResolveDetails details, String version) { | ||
if (version != null && firstOrderDepsWithoutVersions.contains(getCoord(details))) { | ||
details.useVersion(version); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ovy/netflix/nebula/dependency/recommender/RecommendationsOverrideTransitivesStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package netflix.nebula.dependency.recommender; | ||
|
||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.artifacts.DependencyResolveDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RecommendationsOverrideTransitivesStrategy extends RecommendationStrategy { | ||
|
||
private List<String> firstOrderDepsWithVersions = new ArrayList<>(); | ||
|
||
@Override | ||
public void inspectDependency(Dependency dependency) { | ||
if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) { | ||
firstOrderDepsWithVersions.add(dependency.getGroup() + ":" + dependency.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean recommendVersion(DependencyResolveDetails details, String version) { | ||
if (version != null && !firstOrderDepsWithVersions.contains(getCoord(details))) { | ||
details.useVersion(version); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.