Last active
March 15, 2023 17:14
-
-
Save nightm4re94/e882186fa457b5c5a1f1e1e2902c86ee to your computer and use it in GitHub Desktop.
Gradle semantic versioning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tasks.named("printGitSemVer") { | |
group = "versioning" | |
description = "Prints the Gradle project version resolved from git tags." | |
} | |
tasks.register("incrementMajorVersion") { | |
group = "versioning" | |
doLast { | |
String newTag | |
if (grgit.tag.list().isEmpty()) { | |
newTag = "1.0.0" | |
} else { | |
def versions = getVersions(getLatestTag()) | |
// Increment major version | |
versions[0] += 1 | |
// Increment minor version | |
versions[1] = 0 | |
// Increment patch version | |
versions[2] = 0 | |
newTag = versions.join(".") | |
} | |
grgit.tag.add { | |
name = newTag | |
message = "Release of $newTag" | |
} | |
println("Tagged version $newTag") | |
} | |
} | |
tasks.register("incrementMinorVersion") { | |
group = "versioning" | |
doLast { | |
String newTag | |
if (grgit.tag.list().isEmpty()) { | |
newTag = "0.1.0" | |
} else { | |
def versions = getVersions(getLatestTag()) | |
// Increment minor version | |
versions[1] += 1 | |
// Increment patch version | |
versions[2] = 0 | |
newTag = versions.join(".") | |
} | |
grgit.tag.add { | |
name = newTag | |
message = "Release of $newTag" | |
} | |
println("Tagged version $newTag") | |
} | |
} | |
tasks.register("incrementPatchVersion") { | |
group = "versioning" | |
doLast { | |
String newTag | |
if (grgit.tag.list().isEmpty()) { | |
newTag = "0.0.1" | |
} else { | |
def versions = getVersions(getLatestTag()) | |
// Increment patch version | |
versions[2] += 1 | |
newTag = versions.join(".") | |
} | |
grgit.tag.add { | |
name = newTag | |
message = "Release of $newTag" | |
} | |
println("Tagged version $newTag") | |
} | |
} | |
tasks.register("listAllGitTags") { | |
group = "versioning" | |
println("Git tags in repository:") | |
println(getSortedGitTags()*.name) | |
} | |
tasks.register("pushTags") { | |
group = "versioning" | |
doLast { | |
grgit.push { tags = true } | |
} | |
} | |
def getSortedGitTags() { | |
return grgit.tag.list().sort { x, y -> | |
def xVersions = getVersions(x.name) | |
def yVersions = getVersions(y.name) | |
xVersions[0] <=> yVersions[0] ?: | |
xVersions[1] <=> yVersions[1] ?: | |
xVersions[2] <=> yVersions[2] | |
} | |
} | |
def getVersions(var tag) { | |
tag.tokenize('.')*.asType(Integer) | |
} | |
def getLatestTag() { | |
getSortedGitTags().last().name | |
} | |
ext.isSnapshotVersion = { -> | |
project.version.toString().contains("dev") || project.version.toString().contains("noTag") | |
} | |
gitSemVer { | |
developmentIdentifier = "dev" | |
noTagIdentifier = "noTag" | |
enforceSemanticVersioning = true | |
// ensure that other tasks can already access the computed SemVer during evaluation | |
assignGitSemanticVersion() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment