-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.gradle
130 lines (104 loc) · 3.93 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
// 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
Version(String version) {
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
}
@SuppressWarnings("unused")
void bumpMinor() {
minor += 1
patch = 0
}
@SuppressWarnings("unused")
void bumpPatch() {
patch += 1
}
String getName() { "$major.$minor.$patch" }
}
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")
if(oldVersionName.endsWith("-SNAPSHOT")) {
def versionWithoutSnapshot = oldVersionName.substring(0, oldVersionName.length() - 9)
version = new Version(versionWithoutSnapshot)
} else {
version = new Version(oldVersionName)
}
version."bump$type"()
if(!oldVersionName.endsWith("-SNAPSHOT")) {
props.setProperty("version", version.getName())
} else {
props.setProperty("version", version.getName() + "-SNAPSHOT")
}
props.store(propertyFile.newWriter(), null)
println "Bumped ${type.toLowerCase()} version, now at ${props.getProperty('version')}"
}
}
}
}
// 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
}
}