Skip to content

Instantly share code, notes, and snippets.

@dekalo-stanislav
Created November 23, 2018 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekalo-stanislav/197f3dbefcde2a05e3d7459357818977 to your computer and use it in GitHub Desktop.
Save dekalo-stanislav/197f3dbefcde2a05e3d7459357818977 to your computer and use it in GitHub Desktop.
Gradle script for version generation for tag
ext {
/**
* Gets the version name from the latest Git tag
*/
getGitVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
ensureBuildTaggedProperly = { String versionName ->
gitVersionName = getGitVersionName()
if (!gitVersionName.startsWith(versionName)) {
throw new IllegalStateException("versionName = " + versionName + " gitVersionName = " + gitVersionName)
}
}
/**
* Build version number based on semver version name.
*/
buildVersionNumber = { String versionName ->
// let's reserve last digit for flavors and other staff.
int patchDigit = 10
int minorDigit = patchDigit * 1000
int majorDigit = minorDigit * 1000
def (major, minor, patch) = versionName.tokenize('.')
if (major == null) major = 0
if (minor == null) minor = 0
if (patch == null) patch = 0
return (major.toInteger() * majorDigit) + (minor.toInteger() * minorDigit) + (patch.toInteger() * patchDigit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment