diff --git a/build.gradle b/build.gradle index 5541a0954cc1c9257a2c88a01e00d3b299f352cb..eeecbb17366e9988b590dee0e1a8d69f49ed5168 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'edu.wisc.doit.gradle' -version '0.2.1' +version '0.2.2' apply plugin: 'groovy' apply plugin: 'idea' diff --git a/src/main/groovy/edu/wisc/doit/gradle/CompareVersions.groovy b/src/main/groovy/edu/wisc/doit/gradle/CompareVersions.groovy index 77d2f2735d3e1d0a674a5ec5e96f5ca8e725d3ef..1526a54b102e4243973e6ec512ea2be7d27462d7 100644 --- a/src/main/groovy/edu/wisc/doit/gradle/CompareVersions.groovy +++ b/src/main/groovy/edu/wisc/doit/gradle/CompareVersions.groovy @@ -3,6 +3,9 @@ package edu.wisc.doit.gradle import com.github.zafarkhaja.semver.Version import org.gradle.api.GradleException +import java.util.regex.Matcher +import java.util.regex.Pattern + /** * Encapsulates logic for comparing version strings. * @@ -10,6 +13,7 @@ import org.gradle.api.GradleException */ class CompareVersions { + static final Pattern PATTERN = Pattern.compile(".*(\\d+\\.\\d+\\.\\d+).*") /** * * @param fromGitDescribe the version string from git (e.g. 'git describe') @@ -22,12 +26,7 @@ class CompareVersions { println "git describe is empty - there are no releases yet. You must have at least one tag on the repository to use this task." return } - def lastVersion = Version.valueOf(fromGitDescribe) - if(!''.equals(lastVersion.getPreReleaseVersion())) { - // git describe produced something like '0.20.7-1-gfbe61fd' - // split on '-' and recreate last as the first token - lastVersion = Version.valueOf(fromGitDescribe.split('-')[0]) - } + def lastVersion = parseVersionFromGitDescribe(fromGitDescribe) // read 'version' field from build.gradle def currentVersion = Version.valueOf(inBuildGradle); @@ -38,4 +37,20 @@ class CompareVersions { throw new GradleException("version field in build.gradle with current value " + currentVersion + " must be incremented past " + lastVersion) } } + + /** + * + * @param gitDescribe the version string from git + * @return a {@link Version} + * @throws IllegalArgumentException if a version could not be extracted + */ + static Version parseVersionFromGitDescribe(String gitDescribe) { + Matcher m = PATTERN.matcher(gitDescribe); + if(m.matches()) { + String substring = m.group(1); + println "Extracted ${substring} from git describe output of ${gitDescribe}" + return Version.valueOf(substring); + } + throw new IllegalArgumentException("cannot parse version from " + gitDescribe) + } } diff --git a/src/test/groovy/edu/wisc/doit/gradle/CompareVersionsTest.groovy b/src/test/groovy/edu/wisc/doit/gradle/CompareVersionsTest.groovy index 1c725ea9ead468c22b01c394952894edcde48ae8..31205f3537738506e1e31b030a79beb03ae98f5a 100644 --- a/src/test/groovy/edu/wisc/doit/gradle/CompareVersionsTest.groovy +++ b/src/test/groovy/edu/wisc/doit/gradle/CompareVersionsTest.groovy @@ -1,8 +1,11 @@ package edu.wisc.doit.gradle +import com.github.zafarkhaja.semver.Version import org.gradle.api.GradleException import org.junit.Test +import static org.junit.Assert.assertEquals + /** * Unit tests for {@link CompareVersions}. * @@ -30,4 +33,22 @@ class CompareVersionsTest { public void describe_includes_hash_same_version_expects_failure() { CompareVersions.compare("0.20.7-1-gfbe61fd", "0.20.7") } + + @Test + public void describe_maven_release_plugin_style_tags() { + CompareVersions.compare("my-awesome-project-0.2.0-13-g98171dc", "0.2.1") + } + + @Test + void "parseVersionFromGitDescribe control"() { + assertEquals(Version.valueOf("0.0.1"), CompareVersions.parseVersionFromGitDescribe("0.0.1")) + } + @Test + void "parseVersionFromGitDescribe with preReleaseVersion"() { + assertEquals(Version.valueOf("0.20.7"), CompareVersions.parseVersionFromGitDescribe("0.20.7-1-gfbe61fd")) + } + @Test + void "parseVersionFromGitDescribe maven release plugin formatted tag"() { + assertEquals(Version.valueOf("0.2.0"), CompareVersions.parseVersionFromGitDescribe("my-awesome-project-0.2.0-13-g98171dc")) + } }