-
Notifications
You must be signed in to change notification settings - Fork 18
/
versioninfo.gradle
50 lines (42 loc) · 1.95 KB
/
versioninfo.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// This now supports Semantic Versioning
// (see http://semver.org/ for more information)
def getVersionInfo() {
def longVersionName = ''
def hasVersionMarker = longVersionName.count('-') > 2
def fullVersionTag
def versionMarker = ''
def versionBuild
def gitSha
def versionBuildStr
def versionMarkerStr
try {
longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim()
if( longVersionName.length() == 0 ) {
throw new GradleException("No version information detected.")
}
} catch( Throwable e ) {
return [ null ];
}
if (hasVersionMarker) {
(fullVersionTag, versionMarker, versionBuild, gitSha) = longVersionName.tokenize('-')
} else {
(fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-')
}
def hasVersionBuild = versionBuild.toInteger() > 0
def (versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.')
gitSha = (gitSha.startsWith("g") ? gitSha.substring(1) : gitSha);
versionMajor = (versionMajor.startsWith("v") ? versionMajor.substring(1) : versionMajor );
versionBuildStr = hasVersionBuild ? ".$versionBuild" : ""
versionMarkerStr = hasVersionMarker ? "-$versionMarker" : ( hasVersionBuild ? "-NEXT" : "")
def versionName = "$versionMajor.$versionMinor.${versionPatch}${versionMarkerStr}${versionBuildStr}+$gitSha"
def versionNameShort = "$versionMajor.$versionMinor.$versionPatch"
def versionCode = versionMajor.toInteger() * 100000 + versionMinor.toInteger() * 10000 + versionPatch.toInteger() * 1000 + versionBuild.toInteger()
// println "versionName: " + versionName
// println "versionNameShort: " + versionNameShort
// println "versionCode: " + versionCode.toInteger()
// return ['1.0.5-NEXT.30+481669f', '1.0.5', 105030, 481669f]
return [versionName, versionNameShort, versionCode.toInteger(), gitSha]
}
ext {
getVersionInfo = this.&getVersionInfo
}