Skip to content

Instantly share code, notes, and snippets.

@friederbluemle
Created August 8, 2014 06:08
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 friederbluemle/0e15718dd84b5d34400c to your computer and use it in GitHub Desktop.
Save friederbluemle/0e15718dd84b5d34400c to your computer and use it in GitHub Desktop.
Gradle versioning using Git tags
def getGitDescribe = { ->
return 'git describe --tags --dirty'.execute().text.trim()
}
def isSnapshotBuild = { ->
def hasModifiedDeletedOrOtherFiles = !'git ls-files -mdo --exclude-standard'.execute().text.trim().isEmpty()
def hasStagedFiles = !'git diff-index --no-ext-diff --name-only --cached HEAD'.execute().text.trim().isEmpty()
def hasAdditionalCommits = getGitDescribe().contains('-')
hasModifiedDeletedOrOtherFiles || hasStagedFiles || hasAdditionalCommits
}
class Version {
Integer major, minor, patch, commits
Boolean isSnapshot
static def from(gitDescribe, isSnapshot) {
def matcher = gitDescribe =~ '(\\d+)\\.(\\d+)\\.(\\d+)(-\\d+)?'
if (matcher) {
def major = matcher.group(1).toInteger()
def minor = matcher.group(2).toInteger()
def patch = matcher.group(3).toInteger()
def commits = matcher.group(4) ? matcher.group(4).toInteger().abs() : 0
new Version(major:major, minor:minor, patch:patch, commits:commits, isSnapshot:isSnapshot)
} else {
new Version(major:0, minor:0, patch:0, commits:1, isSnapshot:false)
}
}
def Integer getCode() {
major * 100000 + minor * 10000 + patch * 1000 + commits
}
def String getName() {
def adjustedPatch = isSnapshot ? patch + 1 : patch
def suffix = isSnapshot ? '-SNAPSHOT' : ''
String.format('%d.%d.%d%s', major, minor, adjustedPatch, suffix)
}
def String toString() {
def suffix = isSnapshot ? '-SNAPSHOT' : ''
String.format('%d.%d.%d-%d%s', major, minor, patch, commits, suffix)
}
}
subprojects {
ext.androidVersion = Version.from(getGitDescribe(), isSnapshotBuild())
version = ext.androidVersion.getName()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment