-
Notifications
You must be signed in to change notification settings - Fork 912
/
versioning.gradle
140 lines (121 loc) · 4.34 KB
/
versioning.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import javax.inject.Inject
import org.gradle.api.provider.ValueSourceParameters
import java.nio.charset.Charset
ext {
buildVersionCode = {
def versionCode = buildNumberCode()
def versionName = getVersionName()
def (major, minor, patch) = versionName.toLowerCase().tokenize('.')
(major, minor, patch) = [major, minor, patch].collect { it.toInteger() }
(major * 10_000_000) + (minor * 10_000) + (patch * 1_000) + versionCode
}
getVersionName = {
def props = new Properties()
file("$rootDir/app/version/version.properties").withInputStream { props.load(it) }
return props.getProperty("VERSION")
}
getVersionNameSuffix = {
if (project.hasProperty("versionNameSuffix")) {
return project["versionNameSuffix"]
} else {
return ""
}
}
buildVersionName = {
def suffix = getVersionNameSuffix()
if (suffix?.trim()) {
return getVersionName() + "." + buildNumberCode() + getVersionNameSuffix()
} else {
return getVersionName()
}
}
getStoredTag = {
if(project.hasProperty("latestTag")) {
return project["latestTag"]
} else {
def latestGitTagProvider = providers.of(LatestGitTagValueSource.class) {}
return latestGitTagProvider.get()
}
}
buildNumberCode = {
def suffix = getVersionNameSuffix()
if (suffix?.trim()) {
def tag = getStoredTag()
if (tag.contains('nightly')) {
def (major, minor, patch, nightly) = tag.toLowerCase().tokenize('.')
(major, minor, patch, nightly) = [major, minor, patch, nightly].collect { it.toString() }
def (code, sufix) = nightly.toLowerCase().tokenize('-')
(code, sufix) = [code, sufix].collect { it.toString() }
def newCode = Integer.parseInt(code) + 1
return newCode
} else {
return 1
}
} else {
return 0
}
}
}
tasks.register("setBuildNumber") {
doLast {
if(!project.hasProperty("number")) {
throw new IllegalArgumentException("Missing argument: 'number' must be provided")
}
filePath = "${CI_HOME_DIR}/build_number.properties"
if(!new File(filePath).exists()) {
throw new IllegalStateException("build number file could not be found at: $filePath")
}
String buildNumberString = project["number"]
Integer buildNumberInt = Integer.parseInt(buildNumberString)
if(buildNumberInt >= 1000) {
throw new IllegalStateException("Build number limited to 3 digits; cannot exceed 1,000")
}
println("Setting build number to: $buildNumberInt")
def props = new Properties()
props.load(new FileInputStream(filePath))
props["build"] = buildNumberInt.toString()
def os = new FileOutputStream(filePath)
props.store(os, null)
}
}
tasks.register('incrementBuildNumber') {
doLast {
filePath = "${CI_HOME_DIR}/build_number.properties"
if(!new File(filePath).exists()) {
return
}
def props = new Properties()
props.load(new FileInputStream(filePath))
String currentBuildNumber = props["build"]
Integer buildNumber = Integer.parseInt(currentBuildNumber)
buildNumber++
if(buildNumber >= 1000) {
throw new IllegalStateException("Build number limited to 3 digits; cannot exceed 1,000")
}
props["build"] = buildNumber.toString()
def os = new FileOutputStream(filePath)
props.store(os, null)
}
}
tasks.register('getBuildVersionName') {
doLast {
print buildVersionName()
}
}
tasks.register('getBuildVersionCode') {
doLast {
print buildNumberCode()
}
}
abstract class LatestGitTagValueSource implements ValueSource<String, ValueSourceParameters.None> {
@Inject
abstract ExecOperations getExecOperations()
String obtain() {
ByteArrayOutputStream output = new ByteArrayOutputStream()
execOperations.exec {
it.commandLine "git", "describe", "--tags", "--abbrev=0"
it.standardOutput = output
}
return new String(output.toByteArray(), Charset.defaultCharset())
}
}