-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreleaseInfo.gradle
83 lines (80 loc) · 2.45 KB
/
releaseInfo.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
import groovy.xml.MarkupBuilder
ext{
versionFile = file('releases.xml')
if (!versionFile.exists()) {
versionFile.createNewFile()
}
}
this.afterEvaluate { Project project ->
def buildTask = project.tasks.getByName('build')
buildTask.dependsOn('writeInfoTask')
buildTask.doLast {
println 'do last now'
// writeInfoTask.execute()
}
}
task writeInfoTask {
inputs.property('versionName',rootProject.ext.versionName)
inputs.property('versionCode',rootProject.ext.versionCode)
inputs.property('versionInfo',rootProject.ext.versionInfo)
outputs.file versionFile
doLast {
def versions = inputs.getProperties()
File file = outputs.getFiles().getSingleFile()
VersionManager versionMsg = new VersionManager(versions)
//将实体写入到xml文件中
StringWriter sw = new StringWriter()
def builder = new MarkupBuilder(sw)
if (file.text != null && file.text.size() <= 0) {
//没有版本内容
builder.releases{
release {
versionCode(versionMsg.versionCode)
versionName(versionMsg.versionName)
versionInfo(versionMsg.versionInfo)
}
}
file.withWriter {
it.append(sw.toString())
}
}else {
//已有其它版本内容
builder.release {
versionCode(versionMsg.versionCode)
versionName(versionMsg.versionName)
versionInfo(versionMsg.versionInfo)
}
//插入到最后一行前面
def lines = file.readLines()
def lengths = lines.size() - 1
file.withWriter {
lines.eachWithIndex { String entry, int index ->
if(index != lengths){
file.append(entry+'\r\n')
}else {
file.append('\r\r\n' + sw.toString() + '\r\n')
file.append(lines.get(lengths))
}
}
}
}
}
}
class VersionManager {
def versionName
def versionCode
def versionInfo
}
task readInfoTask {
inputs.file versionFile
doLast {
File file = inputs.getFiles().getSingleFile()
println file.text
}
}
task taskZ {
dependsOn writeInfoTask, readInfoTask
doLast {
println '输入输出任务结束'
}
}