Skip to content

Commit d002c34

Browse files
authored
Add version data extraction (#6235)
Will allow me to dynamically fetch the version for the site making it so we don't have to update it every mc release
1 parent ff7c952 commit d002c34

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

.github/workflows/upload-plugin-data.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
run: |
2929
chmod +x gradlew
3030
./gradlew commandData
31+
./gradlew versionData
3132
3233
- name: Upload Data
3334
uses: ryand56/r2-upload-action@v1

Essentials/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ test {
4040
testLogging.showStandardStreams = true
4141
}
4242

43+
tasks.register('versionData', VersionDataTask) {
44+
destination.set(rootProject.layout.projectDirectory.dir("generated").file("version-data.json"))
45+
}
46+
4347
shadowJar {
4448
dependencies {
4549
include (dependency('io.papermc:paperlib'))
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import com.google.gson.GsonBuilder
2+
import org.gradle.api.DefaultTask
3+
import org.gradle.api.tasks.OutputFile
4+
import org.gradle.api.tasks.TaskAction
5+
import java.util.regex.Pattern
6+
7+
abstract class VersionDataTask : DefaultTask() {
8+
@OutputFile
9+
val destination = project.objects.fileProperty()
10+
11+
@TaskAction
12+
fun generateVersionData() {
13+
val versionUtilFile =
14+
project.rootProject.file("Essentials/src/main/java/com/earth2me/essentials/utils/VersionUtil.java")
15+
if (!versionUtilFile.exists()) {
16+
logger.warn("VersionUtil not found")
17+
return
18+
}
19+
20+
val content = versionUtilFile.readText()
21+
val supportedVersions = extractSupportedVersions(content)
22+
23+
if (supportedVersions.isEmpty()) {
24+
logger.warn("No supported versions found in VersionUtil")
25+
return
26+
}
27+
28+
val versionData = mapOf("supportedVersions" to supportedVersions)
29+
val json = GsonBuilder().create().toJson(versionData)
30+
31+
val output = project.file("build/generated/version-data.json")
32+
output.parentFile.mkdirs()
33+
output.writeText(json)
34+
destination.get().asFile.parentFile.mkdirs()
35+
output.copyTo(destination.get().asFile, overwrite = true)
36+
}
37+
38+
private fun extractSupportedVersions(content: String): List<String> {
39+
val supportedVersions = mutableListOf<String>()
40+
41+
val supportedVersionsPattern = Pattern.compile(
42+
"supportedVersions\\s*=\\s*ImmutableSet\\.of\\(([^)]+)\\)",
43+
Pattern.DOTALL
44+
)
45+
val matcher = supportedVersionsPattern.matcher(content)
46+
47+
if (matcher.find()) {
48+
val versionsString = matcher.group(1)
49+
val versionNames = versionsString.split(",")
50+
.map { it.trim() }
51+
.filter { it.isNotEmpty() }
52+
53+
for (versionName in versionNames) {
54+
val versionString = extractVersionString(content, versionName)
55+
if (versionString != null) {
56+
supportedVersions.add(versionString)
57+
}
58+
}
59+
}
60+
61+
return supportedVersions
62+
}
63+
64+
private fun extractVersionString(content: String, versionName: String): String? {
65+
val versionPattern = Pattern.compile(
66+
"BukkitVersion\\s+$versionName\\s*=\\s*BukkitVersion\\.fromString\\(\"([^\"]+)\"\\)"
67+
)
68+
val matcher = versionPattern.matcher(content)
69+
70+
return if (matcher.find()) {
71+
matcher.group(1)
72+
} else {
73+
null
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)