|
| 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