-
Notifications
You must be signed in to change notification settings - Fork 5
/
publish.gradle
262 lines (223 loc) · 7.24 KB
/
publish.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
/**
* gradlew publish \n 打全量包
* gradlew publishDefault \n 打一个包
* gradlew chVar \n 同步aar
*/
apply from: './publish_config.gradle'
ext {
gradles = '/build.gradle'
rep = "/**rep*/"
aarPat = ".*implementation\\s*\\(name:\\s*[\"']\\s*([a-zA-Z0-9-]+?)_.*\\).*"
supportReplacePat = "\\/\\*\\*support:\\s*(.*)\\s*\\*\\/"
applicationIdPat = /\s*applicationId\s*['|"](.*)['|"]\s*/
// \s*applicationId\s*['|"](.*)['|"]\s*
garbageName = "BXN"
}
tasks.create(name: 'publish') << {
for (variant in variants) {
start variant
}
println "All finished, restore compile to default"
repFile variants[defaultVariantIndex]
}
tasks.create(name: 'publishDefault') << {
start variants[defaultVariantIndex]
}
tasks.create(name: 'chVar') << {
repFile variants[defaultVariantIndex]
}
def start(String variant) {
repFile variant
def gradlewCmd = isWindows() ? "./gradlew.bat" : "./gradlew"
def proc = Runtime.getRuntime().exec "${gradlewCmd} assembleDebug"
new StreamPrinter(proc.getInputStream(), "INFO").start()
new StreamPrinter(proc.getErrorStream(), "ERROR").start()
proc.waitFor()
rename "-debug", variant
println "--------SUCCESS--------"
}
def repFile(String v) {
repFile "./${gradles}", v
for (app in apps) {
def path = "./${app}/"
def current = file(path)
current.eachFile { file ->
if (file.isDirectory()) {
repFile file.path + gradles, v
}
}
}
}
def repFile(String f, String variant) {
def file = file(f)
if (!file.exists()) {
return
}
def txt = file.text
def lines = txt.readLines()
def size = lines.size()
for (def i = 0; i < size; i++) {
def line = lines[i]
def rep = repAAR line, variant
if (rep != "") {
txt = txt.replace line, rep
}
def matched = matchSupport(line, variant)
if (matched != 0 && i + 1 < size) {
def nextLine = lines[i + 1]
if (matched == 1) {
if (!nextLine.trim().startsWith("//")) {
txt = txt.replace nextLine, "//" + nextLine
}
} else if (matched == 2) {
if (nextLine.trim().startsWith("//")) {
txt = txt.replace nextLine, nextLine.replaceFirst("//", "")
}
}
}
}
file.write txt
}
/** return 0 no matched,
* 1 matched but not contains current variant,
* 2 matched and contains current variant*/
def matchSupport(String line, String variant) {
def pattern = java.util.regex.Pattern.compile supportReplacePat
def matcher = pattern.matcher line
if (matcher.find()) {
def vs = matcher.group 1
def vsArray = vs.split ","
if (inArray(variant, vsArray)) {
return 2
}
return 1
}
return 0
}
def repAAR(String line, String variant) {
if (!line.trim().startsWith(rep)) {
return ""
}
def pattern = java.util.regex.Pattern.compile aarPat
def matcher = pattern.matcher line
if (matcher.find()) {
def libName = matcher.group 1
return " /**rep*/implementation (name:'${libName}_${variantMapping variant.toLowerCase()}', ext:'aar')"
}
return ""
}
def rename(String oldSuffix, String variant) {
def roots = project.getSubprojects()
println "--geek--" + roots
for (item in roots) {
if (!projectInApps(item.getName())) {
continue
}
def children = item.getSubprojects()
if (children.isEmpty()) {
continue
}
for (module in children) {
def lineList = module.getBuildFile().readLines()
if (lineList.get(0).contains("com.android.application")) {
def oldName = module.getName() + oldSuffix
def newName = getAppName(module.buildFile, variant)
println "--geek--" + "---oldName--" + oldName + "--newName--" + newName
def path = module.getBuildDir().getPath()
println "--geek--" + path
def apk = file("${path}/outputs/apk/debug/${oldName}.apk")
def renameApk = file("${path}/outputs/apk/debug/${newName}.apk")
if (apk.exists()) {
if (renameApk.exists()) {
renameApk.delete()
}
println "--geek--" + "rename ${apk.getName()} to ${renameApk.getName()}"
apk.renameTo renameApk
}
}
}
}
}
def getAppName2(File buildFile, String variant) {
def appId = buildFile.filterLine { it =~ /^\s*applicationId/ }
// def appId = buildFile.filterLine { it =~ /applicationId/}
def pattern = java.util.regex.Pattern.compile applicationIdPat
def matcher = pattern.matcher appId.toString()
def apkName = ""
println "--geek-appId-" + appId.toString()
println "--geek-pattern-" + pattern
println "--geek-matcher-" + matcher
if (matcher.find()) {
def applicationId = matcher.group 1
def array = applicationId.split "\\."
apkName = array[array.length - 1]
}
println "--geek--" + apkName.toUpperCase() + apkNameMapping(variant)
return apkName.toUpperCase() + apkNameMapping(variant)
}
def getAppName(File buildFile, String variant) {
def appId = buildFile.filterLine { it =~ /^\s*applicationId/ }
def pattern = java.util.regex.Pattern.compile applicationIdPat
def matcher = pattern.matcher appId.toString()
def apkName = ""
if (matcher.find()) {
def applicationId = matcher.group 1
def array = applicationId.split "\\."
apkName = array[array.length - 1]
}
return apkName.toUpperCase() + apkNameMapping(variant)
}
def projectInApps(String name) {
for (app in apps) {
if (name.equals(app)) {
return true
}
}
return false
}
def isWindows() {
def osName = System.getProperty("os.name").toLowerCase()
def result = osName.startsWith "windows"
return result
}
def inArray(String str, String[] array) {
for (item in array) {
if (item.trim().equals(str)) {
return true
}
}
return false
}
class StreamPrinter extends Thread {
private InputStream inputStream
private String type
StreamPrinter(InputStream inputStream, String type) {
this.inputStream = inputStream
this.type = type
}
void run() {
BufferedReader br
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream)
br = new BufferedReader(inputStreamReader)
String line = null
while ((line = br.readLine()) != null) {
println type + " > " + line
if (line.toLowerCase().contains("build failed")) {
println "exit..."
System.exit 1
}
}
} catch (IOException e) {
e.printStackTrace()
} finally {
if (br != null) {
try {
br.close()
} catch (Exception e2) {
e2.printStackTrace()
}
}
}
}
}