-
Notifications
You must be signed in to change notification settings - Fork 127
/
gitversioning.gradle
112 lines (100 loc) · 2.8 KB
/
gitversioning.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Checks if git is available to the Gradle environment and stops the build if not.
*/
def checkForGit() {
try {
def dummyOutput = new ByteArrayOutputStream()
exec {
commandLine 'git'
standardOutput = dummyOutput // prevent output being printed to stdout
ignoreExitValue = true // don't throw exception on exit code > 0
}
}
catch(ignored) {
throw new GradleException('git not found')
}
}
// Automatic git version numbering: http://stackoverflow.com/a/18021756
def getVersionCode = { ->
try {
def code = new ByteArrayOutputStream()
exec {
commandLine 'git', 'tag', '--list'
standardOutput = code
}
return code.toString().split("\n").size()
}
catch (ignored) {
return -1;
}
}
def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty', '--long', '--always'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
def getTagOnly = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--abbrev=0'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
def getDirtyTag = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
def getMavenVersionName = { ->
def version = getTagOnly()
def isTaggedAndClean = version == getDirtyTag()
if (version.startsWith("v")) {
version = version.substring(1)
}
if (!isTaggedAndClean) {
version = version + '-SNAPSHOT'
}
return version
}
android {
defaultConfig {
versionCode getVersionCode()
versionName getVersionName()
}
/* On a release build, assert that Git exists - otherwise the version code and name cannot be
* automatically set. This is done so complicated here because the Android tasks are built
* dynamically at execution time and cannot be accessed at configuration time. */
(project.isLibrary ? libraryVariants : applicationVariants).all { variant ->
//if(variant.name.equalsIgnoreCase('release')) {
variant.outputs.each { output ->
output.processManifestProvider.get().doFirst {
checkForGit()
}
}
//}
}
}
ext.gitVersionName = getVersionName()
ext.gitMavenVersionName = getMavenVersionName()