Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Semantic Versioning class for Groovy
enum PatchLevel {
MAJOR, MINOR, PATCH
}
class SemVer implements Serializable {
private int major, minor, patch
SemVer(String version) {
def versionParts = version.tokenize('.')
println versionParts
if (versionParts.size != 3) {
throw new IllegalArgumentException("Wrong version format - expected MAJOR.MINOR.PATCH - got ${version}")
}
this.major = versionParts[0].toInteger()
this.minor = versionParts[1].toInteger()
this.patch = versionParts[2].toInteger()
}
SemVer(int major, int minor, int patch) {
this.major = major
this.minor = minor
this.patch = patch
}
SemVer bump(PatchLevel patchLevel) {
switch (patchLevel) {
case PatchLevel.MAJOR:
return new SemVer(major + 1, 0, 0)
break
case PatchLevel.MINOR:
return new SemVer(major, minor + 1, 0)
break
case PatchLevel.PATCH:
return new SemVer(major, minor, patch + 1)
break
}
return new SemVer()
}
String toString() {
return "${major}.${minor}.${patch}"
}
}
def version = new SemVer("0.0.1")
println(version.bump(PatchLevel.MAJOR).toString())
println(version.bump(PatchLevel.MINOR).toString())
println(version.bump(PatchLevel.PATCH).toString())
/*
will output
1.0.0
0.1.0
0.0.2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.