-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
87 lines (71 loc) · 2.55 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
allprojects {
repositories {
mavenCentral()
}
}
ext.versionFile = file('version.properties')
task loadVersion {
def baseVersion = readVersion()
allprojects {
version = baseVersion
}
}
ProjectVersion readVersion() {
logger.quiet 'Reading the version file.'
if (!versionFile.exists()) {
throw new GradleException("Required version file does not exit: $versionFile.canonicalPath")
}
Properties versionProps = new Properties()
versionFile.withInputStream { stream ->
versionProps.load(stream)
}
new ProjectVersion(versionProps.major.toInteger(), versionProps.minor.toInteger(), versionProps.patch.toInteger(), versionProps.release.toBoolean())
}
task makeReleaseVersion(group: 'versioning', description: 'Makes project a release version.') << {
version.release = true
ant.propertyfile(file: versionFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
tasks.addRule("Pattern: increment<Classifier>Version – Increments the project version classifier.") { String taskName ->
if (taskName.startsWith('increment') && taskName.endsWith('Version')) {
task(taskName) << {
String classifier = (taskName - 'increment' - 'Version').toLowerCase()
String currentVersion = version.toString()
switch (classifier) {
case 'major': ++version.major
break
case 'minor': ++version.minor
break
case 'patch': ++version.patch
break
default: throw new GradleException("Invalid version type '$classifier. Allowed types: ['Major', 'Minor', 'Patch']")
}
String newVersion = version.toString()
logger.info "Incrementing $classifier project version: $currentVersion -> $newVersion"
ant.propertyfile(file: versionFile) {
entry(key: classifier, type: 'int', operation: '+', value: 1)
}
}
}
}
class ProjectVersion {
Integer major
Integer minor
Integer patch
Boolean release
ProjectVersion(Integer major, Integer minor, Integer patch) {
this.major = major
this.minor = minor
this.patch = patch
this.release = Boolean.FALSE
}
ProjectVersion(Integer major, Integer minor, Integer patch, Boolean release) {
this(major, minor, patch)
this.release = release
}
@Override
String toString() {
"$major.$minor.$patch${release ? '' : '-SNAPSHOT'}"
}
}