From fae77000156227b3a1067d852455774cd595ff92 Mon Sep 17 00:00:00 2001 From: iamdanfox Date: Tue, 26 Feb 2019 16:11:43 +0000 Subject: [PATCH] [improvement] getVersion(String, String) signature allows easier adoption (#27) ## Before this PR It was tricky to excavate out the change from nebula -> gradle-consistent-versions because of the `dependencyRecommendations.getRecommendedVersion` function. In nebula it took two args, but in our plugin it took one arg. ## After this PR our plugin now supports the two-arg signature, so we can excavate people using a simple sed. --- README.md | 2 +- .../palantir/gradle/versions/GetVersionPlugin.java | 12 ++++++++++++ .../gradle/versions/GetVersionPluginSpec.groovy | 14 +++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c5062d6..14664876 100644 --- a/README.md +++ b/README.md @@ -285,7 +285,7 @@ Alternatives: ```diff task copySomething(type: Copy) { -- from "$buildDir/foo/bar-${dependencyRecommendations.getRecommendedVersion('group:bar')}" +- from "$buildDir/foo/bar-${dependencyRecommendations.getRecommendedVersion('group', 'bar')}" + from { "$buildDir/foo/bar-${getVersion('group:bar')}" } ... ``` diff --git a/src/main/java/com/palantir/gradle/versions/GetVersionPlugin.java b/src/main/java/com/palantir/gradle/versions/GetVersionPlugin.java index 8dc92864..82864434 100644 --- a/src/main/java/com/palantir/gradle/versions/GetVersionPlugin.java +++ b/src/main/java/com/palantir/gradle/versions/GetVersionPlugin.java @@ -36,12 +36,17 @@ public final class GetVersionPlugin implements Plugin { public void apply(Project project) { project.getExtensions().getExtraProperties().set("getVersion", new Closure(project, project) { + /** + * Groovy will invoke this method if they just supply one arg, e.g. 'com.google.guava:guava'. + * This is the preferred signature because it's shortest. + */ public String doCall(Object moduleVersion) { return doCall(moduleVersion, project.getRootProject() .getConfigurations() .getByName(VersionsLockPlugin.UNIFIED_CLASSPATH_CONFIGURATION_NAME)); } + /** Find a version from another configuration, e.g. from the gradle-docker plugin. */ public String doCall(Object moduleVersion, Configuration configuration) { List strings = Splitter.on(':').splitToList(moduleVersion.toString()); Preconditions.checkState( @@ -51,6 +56,13 @@ public String doCall(Object moduleVersion, Configuration configuration) { return getVersion(project, strings.get(0), strings.get(1), configuration); } + + /** This matches the signature of nebula's dependencyRecommendations.getRecommendedVersion. */ + public String doCall(String group, String name) { + return getVersion(project, group, name, project.getRootProject() + .getConfigurations() + .getByName(VersionsLockPlugin.UNIFIED_CLASSPATH_CONFIGURATION_NAME)); + } }); } diff --git a/src/test/groovy/com/palantir/gradle/versions/GetVersionPluginSpec.groovy b/src/test/groovy/com/palantir/gradle/versions/GetVersionPluginSpec.groovy index fba1803e..b71d8cc1 100644 --- a/src/test/groovy/com/palantir/gradle/versions/GetVersionPluginSpec.groovy +++ b/src/test/groovy/com/palantir/gradle/versions/GetVersionPluginSpec.groovy @@ -40,7 +40,7 @@ class GetVersionPluginSpec extends ProjectSpec { noExceptionThrown() } - def 'function is callable from groovy with two args'() { + def 'function is callable from groovy with string & configuration args'() { when: project.apply plugin: GetVersionPlugin project.apply plugin: JavaPlugin @@ -50,4 +50,16 @@ class GetVersionPluginSpec extends ProjectSpec { def ex = thrown(GradleException) ex.message.contains "Unable to find 'com.google.guava:guava' in configuration ':compile'" } + + def 'function is callable from groovy with two string args'() { + when: + project.apply plugin: GetVersionPlugin + project.apply plugin: JavaPlugin + project.getConfigurations().create(VersionsLockPlugin.UNIFIED_CLASSPATH_CONFIGURATION_NAME) + project.ext.getVersion('com.google.guava', 'guava') + + then: + def ex = thrown(GradleException) + ex.message.contains "Unable to find 'com.google.guava:guava' in configuration ':unifiedClasspath'" + } }