-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathversion.gradle
144 lines (114 loc) · 4.37 KB
/
version.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
// Version modifications for android + java multi-module gradle builds.
//
// Increments 'version' and 'versionCode' in gradle.properties in root directory.
// then copies them to the root build configuration (assumes common android configuration in the root)
//
// Example use:
// ./gradlew -b version.gradle toReleaseVersion
// ./gradlew -b version.gradle nextPatchSnapshotVersion
//
// See also https://stackoverflow.com/a/58899378/459579
class Version {
private int major
private int minor
private int patch
private int code
Version(int code, String version) {
this.code = code
def (major, minor, patch) = version.tokenize('.')
this.major = major.toInteger()
this.minor = minor.toInteger()
this.patch = patch.toInteger()
}
@SuppressWarnings("unused")
void bumpMajor() {
major += 1
minor = 0
patch = 0
code += 1
}
@SuppressWarnings("unused")
void bumpMinor() {
minor += 1
patch = 0
code += 1
}
@SuppressWarnings("unused")
void bumpPatch() {
patch += 1
code += 1
}
String getName() { "$major.$minor.$patch" }
int getCode() { code }
}
task toReleaseVersion {
doFirst {
def props = new Properties()
def propertyFile = file("gradle.properties")
propertyFile.withInputStream { props.load(it) }
String value = props.getProperty("version")
if(value.endsWith("-SNAPSHOT")) {
def versionWithoutSnapshot = value.substring(0, value.length() - 9)
props.setProperty("version", versionWithoutSnapshot)
props.store(propertyFile.newWriter(), null)
println "Now at version $versionWithoutSnapshot"
} else {
println "Already at version $value"
}
}
}
task toSnapshotVersion {
doFirst {
def props = new Properties()
def propertyFile = file("gradle.properties")
propertyFile.withInputStream { props.load(it) }
String value = props.getProperty("version")
if(value.endsWith("-SNAPSHOT")) {
println "Already at version $value"
} else {
def versionWithSnapshot = value + "-SNAPSHOT";
props.setProperty("version", versionWithSnapshot)
props.store(propertyFile.newWriter(), null)
println "Now at version $versionWithSnapshot"
}
}
}
// handles both snapshot and release versions
tasks.addRule("Pattern: bump<TYPE>Version") { String taskName ->
if (taskName.matches("bump(Major|Minor|Patch)Version")) {
task(taskName) {
doLast {
def props = new Properties()
def propertyFile = file("gradle.properties")
propertyFile.withInputStream { props.load(it) }
String type = (taskName - 'bump' - 'Version')
String oldVersionName = props.getProperty("version")
int oldVersionCode = Integer.parseInt(props.getProperty("versionCode"))
if(oldVersionName.endsWith("-SNAPSHOT")) {
def versionWithoutSnapshot = oldVersionName.substring(0, oldVersionName.length() - 9)
version = new Version(oldVersionCode, versionWithoutSnapshot)
} else {
version = new Version(oldVersionCode, oldVersionName)
}
version."bump$type"()
String newVersionName = version.getName()
String newVersionCode = version.getCode()
if(!oldVersionName.endsWith("-SNAPSHOT")) {
props.setProperty("version", newVersionName)
} else {
props.setProperty("version", newVersionName + "-SNAPSHOT")
}
props.setProperty("versionCode", newVersionCode)
props.store(propertyFile.newWriter(), null)
println "Bumped ${type.toLowerCase()} version, now at ${props.getProperty('version')} / $version.code"
}
}
}
}
// handles both snapshot and release versions
tasks.addRule("Pattern: next<TYPE>SnapshotVersion") { String taskName ->
if (taskName.matches("next(Major|Minor|Patch)SnapshotVersion")) {
String type = (taskName - 'next' - 'SnapshotVersion')
task(taskName).dependsOn "bump${type}Version", toSnapshotVersion
}
}