-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_json.gradle
101 lines (96 loc) · 4.07 KB
/
update_json.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
import groovy.json.JsonOutput
/**
* Code for automatic update JSON generation
* https://github.com/BluSunrize/ImmersiveEngineering/blob/ee3f452fe62f9efece0afa74a315ad0c341f0ec7/build.gradle#L285-L377
* @author BluSunrize
*/
class GenerateUpdateJSON extends DefaultTask {
static def VERSION_PREFIX = "##### Version "
static def BUILT_SUFFIX = " - BUILT"
static def RECOMMENDED = "-recommended"
static def LATEST = "-latest"
/**If the mod is in beta, no recommended release will be set*/
boolean beta = false
String versionScheme = '%2$s'
String curseforgeURL
File changelog
File output
static int compareVersions(String vA, String vB) {
String[] vPartsA = vA.split("[\\D]")
String[] vPartsB = vB.split("[\\D]")
if (vPartsA.length==0&&vPartsB.length==0)
return vA <=> vB
else if (vPartsA.length==0)
return -1
else if (vPartsB.length==0)
return 1
int length = Math.min(vPartsA.length, vPartsB.length)
for (int i = 0;i<length;i++) {
int pA = Integer.parseInt(vPartsA[i])
int pB = Integer.parseInt(vPartsB[i])
if (pA!=pB) {
return pA<=>pB
}
}
if (vPartsA.length != vPartsB.length)
return vPartsA.length <=> vPartsB.length
return vA <=> vB
}
@TaskAction
def generate() {
if (!changelog.exists())
println "Changelog does not exist! Aborting!"
else {
Map<String, Map<String, Object>> changelogForVersions = new HashMap<>()
Map<String, String> promos = new TreeMap<>({String s1, String s2-> compareVersions(s1, s2)})
String currentVersion = null
String currentChangelog = ""
changelog.eachLine {line ->
if (line.startsWith(VERSION_PREFIX)) {
if (currentVersion!=null) {
String[] split = currentVersion.split("-")
if (split.length != 2) {
throw new IllegalArgumentException("Invalid version ${currentVersion}, expected mcversion-modversion")
}
String mcVer = split[0]
String mainVer = split[1]
String modVersion = String.format(versionScheme, mcVer, mainVer)
if (!changelogForVersions.containsKey(mcVer)) {
if (!beta) {
promos.put(mcVer+RECOMMENDED, modVersion)
}
promos.put(mcVer+LATEST, modVersion)
changelogForVersions[mcVer] = new TreeMap<>({String s1, String s2-> compareVersions(s1, s2)})
}
changelogForVersions[mcVer][modVersion] = currentChangelog
}
if (line.endsWith(BUILT_SUFFIX)) {
currentVersion = line.substring(VERSION_PREFIX.length(), line.length()-BUILT_SUFFIX.length())
} else {
currentVersion = (String) null
}
currentChangelog = ""
} else if (!line.isEmpty()) {
if (currentChangelog.length()==0)
currentChangelog += line
else
currentChangelog += "\n"+line
}
}
Map<String, Object> mainMap = new TreeMap<>({String s1, String s2-> compareVersions(s1, s2)})
mainMap.putAll(changelogForVersions)
mainMap["homepage"] = curseforgeURL
mainMap["promos"] = promos
def outJson = JsonOutput.toJson(mainMap)
outJson = JsonOutput.prettyPrint(outJson)
File outF = output
outF.delete()
outF << outJson
}
}
}
task updateJson(type: GenerateUpdateJSON) {
curseforgeURL = project.findProperty("${project.name}URL")
changelog = project.file("changelog.md")
output = project.file("changelog.json")
}