-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcommon.gradle
More file actions
64 lines (56 loc) · 2.16 KB
/
common.gradle
File metadata and controls
64 lines (56 loc) · 2.16 KB
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
ext.modifyModVersion = { File file, int delta ->
def versionPattern = ~/mod\.version\s*=\s*(\d+)\.(\d+)\.(\d+)(-.+)?/
def content = file.text
// Detect line ending from the file
def lineEnding = content.contains('\r\n') ? '\r\n' : '\n'
def lines = content.split(/\r?\n/)
def matches = lines.findAll { line -> versionPattern.matcher(line).find() }
if (matches.size() != 1) {
throw new GradleException("Expected exactly one mod.version in ${file}, found ${matches.size()}")
}
def updatedLines = lines.collect { line ->
def matcher = versionPattern.matcher(line)
if (matcher.find()) {
def major = matcher.group(1).toInteger()
def minor = matcher.group(2).toInteger()
def patch = matcher.group(3).toInteger()
def suffix = matcher.group(4) ?: ''
def newPatch = patch + delta
if (newPatch < 0) {
throw new GradleException("Patch version underflow in ${file}: cannot decrement below zero")
}
def newVersion = "${major}.${minor}.${newPatch}${suffix}"
println "Updated ${file} from ${matcher.group(0)} to mod.version=${newVersion}"
return line.replaceFirst(versionPattern, "mod.version=${newVersion}")
} else {
return line
}
}
file.text = updatedLines.join(lineEnding)
}
tasks.register('incrementModVersions') {
group = 'publishing'
description = 'Increments the patch version of mod.version in gradle.properties.'
doLast {
def file = rootProject.file('gradle.properties')
try {
modifyModVersion(file, 1)
} catch (Exception e) {
println "Error processing ${file}: ${e.message}"
throw e
}
}
}
tasks.register('decrementModVersions') {
group = 'publishing'
description = 'Decrements the patch version of mod.version in gradle.properties.'
doLast {
def file = rootProject.file('gradle.properties')
try {
modifyModVersion(file, -1)
} catch (Exception e) {
println "Error processing ${file}: ${e.message}"
throw e
}
}
}