Skip to content

Commit 213f74c

Browse files
author
Arkadiusz Pałka
committed
build: versioning via git tags
1 parent 009b687 commit 213f74c

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

scripts/versioning.gradle

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1-
// Library version definition
2-
def isSnapshot = false
3-
def (major, minor, patch) = [2,0,0]
4-
def libraryVersionTag = isSnapshot ? 'SNAPSHOT' : ''
1+
task printVersionName() {
2+
println(getProductionVersionName() + " (" + getProductionVersionCode() + ")")
3+
}
54

6-
// Define project library version name
7-
def libraryVersionName = "$major.$minor.$patch"
8-
def libraryVersionNameWithTag = isSnapshot ? "$libraryVersionName-$libraryVersionTag" : libraryVersionName
5+
@SuppressWarnings("GroovyUnusedDeclaration")
6+
static def getProductionVersionName() {
7+
def (major, minor, patch, build, sha, isSnapshot) = getLastMasterGitTagVersion()
8+
def versionName = "${major}.${minor}.${patch}"
9+
if (isSnapshot) {
10+
return "$versionName-SNAPSHOT"
11+
}
12+
return versionName
13+
}
14+
15+
@SuppressWarnings("GroovyUnusedDeclaration")
16+
static def getProductionVersionCode() {
17+
def (major, minor, patch, build, sha, isSnapshot) = getLastMasterGitTagVersion()
18+
return major.toInteger() * 1_000_000 + minor.toInteger() * 1_000 + patch.toInteger()
19+
}
20+
21+
static def getCurrentBranch() {
22+
return "git rev-parse --abbrev-ref HEAD".execute().text.trim()
23+
}
24+
25+
static def getLastMasterGitTagVersion() {
26+
def name = "git describe --tags ${getCurrentBranch()} --long".execute().text.replace("v", "").trim()
27+
def (tag, build, sha, snapshot) = name.tokenize('-')
28+
def isSnapshot = snapshot != null
29+
if (sha == null) {
30+
sha = "git rev-parse --short HEAD".execute().text.trim()
31+
} else {
32+
sha = sha.substring(1) // to remove git's g prefix
33+
}
34+
def (major, minor, patch) = (tag != null) ? tag.tokenize('.') : [1, 0, 0]
35+
return [major, minor, patch, build, sha, isSnapshot]
36+
}
937

38+
// Export methods.
39+
ext.getProductionVersionName = this.&getProductionVersionName
40+
ext.getProductionVersionCode = this.&getProductionVersionCode
41+
42+
// Define project library version name
43+
def libraryVersionName = getProductionVersionName()
1044
// Generate library version code
11-
def libraryVersionCode = major.toInteger() * 1_000_000 + minor.toInteger() * 1_000 + patch.toInteger()
45+
def libraryVersionCode = getProductionVersionCode()
46+
47+
def isSnapshot = libraryVersionName.endsWith("SNAPSHOT")
48+
def libraryVersionTag = isSnapshot ? 'SNAPSHOT' : ''
1249

1350
// Set project extra properties
1451
ext.islibraryVersionSnapshot = isSnapshot
1552
ext.libraryVersionTag = libraryVersionTag
16-
ext.libraryVersionNameWithTag = libraryVersionNameWithTag
1753
ext.libraryVersionName = libraryVersionName
1854
ext.libraryVersionCode = libraryVersionCode
19-
println "Version of ${project.name} is $libraryVersionNameWithTag ($libraryVersionCode)"

0 commit comments

Comments
 (0)