Skip to content

Commit

Permalink
Use 'extraProperties' instead of a normal extension (#92)
Browse files Browse the repository at this point in the history
* register extra properties

* Test guarantees back-compat
  • Loading branch information
iamdanfox authored Apr 20, 2018
1 parent fecd5cc commit 5436b39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

public class GitVersionPlugin implements Plugin<Project> {
public void apply(final Project project) {
project.getExtensions().add("gitVersion", new Closure<String>(this, this) {

// intentionally not using .getExtension() here for back-compat
project.getExtensions().getExtraProperties().set("gitVersion", new Closure<String>(this, this) {
public String doCall(Object args) {
return versionDetails(project, GitVersionArgs.fromGroovyClosure(args)).getVersion();
}
});

project.getExtensions().add("versionDetails", new Closure<VersionDetails>(this, this) {
project.getExtensions().getExtraProperties().set("versionDetails", new Closure<VersionDetails>(this, this) {
public VersionDetails doCall(Object args) {
return versionDetails(project, GitVersionArgs.fromGroovyClosure(args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,30 @@ class GitVersionPluginTests extends Specification {
buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\n[a-z0-9]{40}\nmaster\ntrue\n"
}

def 'version details can be accessed using extra properties method' () {
given:
buildFile << '''
plugins {
id 'com.palantir.git-version'
}
version gitVersion()
task printVersionDetails() << {
println project.getExtensions().getExtraProperties().get('versionDetails')().lastTag
println project.getExtensions().getExtraProperties().get('gitVersion')()
}
'''.stripIndent()
gitIgnoreFile << 'build'
Git git = Git.init().setDirectory(projectDir).call();
git.add().addFilepattern('.').call()
String sha = git.commit().setMessage('initial commit').call().getName().subSequence(0, 7)

when:
BuildResult buildResult = with('printVersionDetails').build()

then:
buildResult.output =~ ":printVersionDetails\n${sha}\n${sha}\n"
}

def 'version details when commit distance to tag is > 0' () {
given:
buildFile << '''
Expand Down

0 comments on commit 5436b39

Please sign in to comment.