-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.gradle
302 lines (256 loc) · 13.9 KB
/
build.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
allprojects {
repositories {
jcenter()
}
project.ext {
productionVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/productionVersion.properties').absolutePath
productionErrorVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/productionErrorVersion.properties').absolutePath
qaVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/qaVersion.properties').absolutePath
qaErrorVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/qaErrorVersion.properties').absolutePath
developmentVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/developmentVersion.properties').absolutePath
developmentErrorVersionFilePath = file(rootProject.projectDir.getAbsolutePath() + '/developmentErrorVersion.properties').absolutePath
productionFlavor = "production"
qaFlavor = "qa"
developmentFlavor = "development"
}
/***
* Increment version code for application according to Build variant, store the updated value back to respective version file
* @param buildType Select Build Variant for displaying version name
*
*/
project.ext.incrementVersionCode = {
String buildType = "buildVariant" ->
println "-----------------------------------------------------------------------------"
println " Executing VersionCode increment "
println "-----------------------------------------------------------------------------"
def versionPropsFile;
def errorVersionFile;
if (buildType == productionFlavor) {
versionPropsFile = file(project.ext.productionVersionFilePath)
errorVersionFile = file(project.ext.productionErrorVersionFilePath)
} else if (buildType == qaFlavor) {
versionPropsFile = file(project.ext.qaVersionFilePath)
errorVersionFile = file(project.ext.qaErrorVersionFilePath)
} else if (buildType == developmentFlavor) {
versionPropsFile = file(project.ext.developmentVersionFilePath)
errorVersionFile = file(project.ext.developmentErrorVersionFilePath)
} else
throw new GradleException("Unknown app variant type")
if (versionPropsFile.canRead()) {
def version = project.ext.readVersionFile(buildType)
def errorVersion = project.ext.readErrorVersionFile(buildType)
def build = version['VERSION_CODE'] as int
errorVersion['VERSION_CODE'] = build.toString()
build++
version['VERSION_CODE'] = build.toString()
def stream = new FileOutputStream(versionPropsFile)
try {
version.store(stream, null);
} finally {
stream.close()
}
def errorStream = new FileOutputStream(errorVersionFile)
try {
errorVersion.store(errorStream, null);
} finally {
errorStream.close()
}
println "Version Code is now... " + build
} else {
throw new GradleException("Could not read version.properties!")
}
}
/***
* Increment version code for application according to Build variant, store the updated value back to respective version file
* @param buildType Select Build Variant for displaying version name
* @param versionType Select version type i.e (Major,Minor,Patch)
*
*/
project.ext.incrementVersionName = {
String buildType = "buildVariant", String versionType = "Version i.e(Major,Minor,Patch,Build)" ->
println "-----------------------------------------------------------------------------"
println " Executing VersionName increment "
println "-----------------------------------------------------------------------------"
def versionPropsFile;
def errorVersionFile;
if (buildType == productionFlavor) {
versionPropsFile = file(project.ext.productionVersionFilePath)
errorVersionFile = file(project.ext.productionErrorVersionFilePath)
} else if (buildType == qaFlavor) {
versionPropsFile = file(project.ext.qaVersionFilePath)
errorVersionFile = file(project.ext.qaErrorVersionFilePath)
} else if (buildType == developmentFlavor) {
versionPropsFile = file(project.ext.developmentVersionFilePath)
errorVersionFile = file(project.ext.developmentErrorVersionFilePath)
} else
throw new GradleException("Unknown app variant type")
if (versionPropsFile.canRead()) {
def version = project.ext.readVersionFile(buildType)
def errorVersion = project.ext.readErrorVersionFile(buildType)
def currentMajorVersion = version['VERSION_MAJOR'] as int
def currentMinorVersion = version['VERSION_MINOR'] as int
def currentPatchVersion = version['VERSION_PATCH'] as int
def currentBuildNumber = version['VERSION_BUILD'] as int
currentBuildNumber = System.getenv("BUILD_NUMBER") as Integer ?: currentBuildNumber + 1
if (errorVersionFile.canRead()) {
errorVersion['VERSION_MAJOR'] = currentMajorVersion.toString()
errorVersion['VERSION_MINOR'] = currentMinorVersion.toString()
errorVersion['VERSION_PATCH'] = currentPatchVersion.toString()
errorVersion['VERSION_BUILD'] = currentBuildNumber.toString()
}
if (versionType == 'Major') {
currentMajorVersion++
currentMinorVersion = 0
currentPatchVersion = 0
} else if (versionType == 'Minor') {
currentMinorVersion++
currentPatchVersion = 0
} else if (versionType == 'Patch') {
currentPatchVersion++
} else if (versionType == 'Build') {
//Do nothing as we need to increment build number locally or read from jenkins
} else {
throw new GradleException("version Type not recognized, Please write (Major,Minor,Patch,Build)")
}
version['VERSION_MAJOR'] = currentMajorVersion.toString()
version['VERSION_MINOR'] = currentMinorVersion.toString()
version['VERSION_PATCH'] = currentPatchVersion.toString()
version['VERSION_BUILD'] = currentBuildNumber.toString()
def stream = new FileOutputStream(versionPropsFile)
try {
version.store(stream, null);
} finally {
stream.close()
}
def errorStream = new FileOutputStream(errorVersionFile)
try {
errorVersion.store(errorStream, null);
} finally {
errorStream.close()
}
println "Version is now... " + currentMajorVersion + "." + currentMinorVersion + "." + currentPatchVersion + "(" + currentBuildNumber + ")"
return currentMajorVersion + "." + currentMinorVersion + "." + currentPatchVersion + "(" + currentBuildNumber + ")"
}
}
project.ext.readVersionFile = {
String buildType = "buildVariant" ->
def versionPropsFile;
if (buildType == productionFlavor)
versionPropsFile = file(project.ext.productionVersionFilePath)
else if (buildType == qaFlavor)
versionPropsFile = file(project.ext.qaVersionFilePath)
else if (buildType == developmentFlavor)
versionPropsFile = file(project.ext.developmentVersionFilePath)
else
throw new GradleException("Unknown app variant type")
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
return versionProps;
} else {
throw new GradleException("Could not read version.properties!")
}
}
project.ext.revertVersionCodeAndName = {
String buildVariantType = "buildVariant" ->
println "-----------------------------------------------------------------------------"
println " Reverting VersionCode And Version Name "
println "-----------------------------------------------------------------------------"
def versionPropsFile;
//def errorVersionFile;
if (buildVariantType == productionFlavor) {
versionPropsFile = file(project.ext.productionVersionFilePath)
//errorVersionFile = file(project.ext.productionErrorVersionFilePath)
} else if (buildVariantType == qaFlavor) {
versionPropsFile = file(project.ext.qaVersionFilePath)
//errorVersionFile = file(project.ext.qaErrorVersionFilePath)
} else if (buildVariantType == developmentFlavor) {
versionPropsFile = file(project.ext.developmentVersionFilePath)
//errorVersionFile = file(project.ext.developmentErrorVersionFilePath)
} else
throw new GradleException("Unknown app variant type")
if (versionPropsFile.canRead()) {
def version = project.ext.readVersionFile(buildVariantType)
def errorVersion = project.ext.readErrorVersionFile(buildVariantType)
def currentBuildCode = version['VERSION_CODE'] as int
def currentMajorVersion = version['VERSION_MAJOR'] as int
def currentMinorVersion = version['VERSION_MINOR'] as int
def currentPatchVersion = version['VERSION_PATCH'] as int
def currentBuildNumber = version['VERSION_BUILD'] as int
println "Current Version Code is: " + currentBuildCode
println "Current Version is now... " + currentMajorVersion + "." + currentMinorVersion + "." + currentPatchVersion + "(" + currentBuildNumber + ")"
println "-----------------------------------------------------------------------------"
//Get All values from Property
def oldBuildNumber = errorVersion['VERSION_CODE'] as int
def oldMajorVersion = errorVersion['VERSION_MAJOR'] as int
def oldMinorVersion = errorVersion['VERSION_MINOR'] as int
def oldPatchVersion = errorVersion['VERSION_PATCH'] as int
def currentErrorBuildNumber = errorVersion['VERSION_BUILD'] as int
//Set All values back to Property File
version['VERSION_CODE'] = oldBuildNumber.toString()
version['VERSION_MAJOR'] = oldMajorVersion.toString()
version['VERSION_MINOR'] = oldMinorVersion.toString()
version['VERSION_PATCH'] = oldPatchVersion.toString()
version['VERSION_BUILD'] = currentBuildNumber.toString()
println "Reverted Version Code is: " + oldBuildNumber
println "Reverted Version is now... " + oldMajorVersion + "." + oldMinorVersion + "." + oldPatchVersion + "(" + currentErrorBuildNumber + ")"
println "-----------------------------------------------------------------------------"
def stream = new FileOutputStream(versionPropsFile)
try {
version.store(stream, null);
} finally {
stream.close()
}
} else {
throw new GradleException("Could not read version.properties!")
}
}
/***
* Load Error Version Property File
*/
project.ext.readErrorVersionFile = {
String buildType = "buildVariant" ->
def versionPropsFile;
if (buildType == productionFlavor)
versionPropsFile = file(project.ext.productionErrorVersionFilePath)
else if (buildType == qaFlavor)
versionPropsFile = file(project.ext.qaErrorVersionFilePath)
else if (buildType == developmentFlavor)
versionPropsFile = file(project.ext.developmentErrorVersionFilePath)
else
throw new GradleException("Unknown app variant type")
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
return versionProps;
} else {
throw new GradleException("Could not read error version.properties!")
}
}
/***
* This is used to write updated version name to jenkins file which will be used by jenkins prefix variable
*/
project.ext.writeToJenkinFile = {
String messageToWrite = "version number to be written" ->
def versionPropsFile = file('jenkinversion.txt')
if (versionPropsFile.canRead()) {
versionPropsFile.write(messageToWrite)
println "Version number which is written in Jenkins file is: " + messageToWrite
} else {
println "File does not exist"
//throw new GradleException("Could not read version.properties!")
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}