Skip to content

Instantly share code, notes, and snippets.

@michaellihs
Created April 12, 2017 21:42
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save michaellihs/a6621376393821d6d206ccfc8dbf86ec to your computer and use it in GitHub Desktop.
Save michaellihs/a6621376393821d6d206ccfc8dbf86ec to your computer and use it in GitHub Desktop.
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
*/
@akomakom
Copy link

akomakom commented Jul 6, 2021

That was very useful.

Fork that supports any length versions with/without suffix:

https://gist.github.com/akomakom/fd8bad8f4d80d973c2146b4903d2b989

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment