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
+ }
5
4
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
+ }
9
37
38
+ // Export methods.
39
+ ext. getProductionVersionName = this . &getProductionVersionName
40
+ ext. getProductionVersionCode = this . &getProductionVersionCode
41
+
42
+ // Define project library version name
43
+ def libraryVersionName = getProductionVersionName()
10
44
// 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' : ' '
12
49
13
50
// Set project extra properties
14
51
ext. islibraryVersionSnapshot = isSnapshot
15
52
ext. libraryVersionTag = libraryVersionTag
16
- ext. libraryVersionNameWithTag = libraryVersionNameWithTag
17
53
ext. libraryVersionName = libraryVersionName
18
54
ext. libraryVersionCode = libraryVersionCode
19
- println " Version of ${ project.name} is $libraryVersionNameWithTag ($libraryVersionCode )"
0 commit comments