Skip to content

Commit

Permalink
[improvement] getVersion(String, String) signature allows easier adop…
Browse files Browse the repository at this point in the history
…tion (#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.
  • Loading branch information
iamdanfox authored and bulldozer-bot[bot] committed Feb 26, 2019
1 parent 5fb105f commit fae7700
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')}" }
...
```
12 changes: 12 additions & 0 deletions src/main/java/com/palantir/gradle/versions/GetVersionPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ public final class GetVersionPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getExtensions().getExtraProperties().set("getVersion", new Closure<String>(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<String> strings = Splitter.on(':').splitToList(moduleVersion.toString());
Preconditions.checkState(
Expand All @@ -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));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'"
}
}

0 comments on commit fae7700

Please sign in to comment.