diff --git a/src/main/kotlin/app/revanced/meta/readme/Generator.kt b/src/main/kotlin/app/revanced/meta/readme/Generator.kt index e01bf70a03..34d8477ca7 100644 --- a/src/main/kotlin/app/revanced/meta/readme/Generator.kt +++ b/src/main/kotlin/app/revanced/meta/readme/Generator.kt @@ -1,5 +1,7 @@ package app.revanced.meta.readme +import SemanticVersion +import SemanticVersionComparator import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages import app.revanced.patcher.extensions.PatchExtensions.description import app.revanced.patcher.extensions.PatchExtensions.patchName @@ -21,7 +23,8 @@ class Generator { for (patch in bundle) { val patchName = patch.patchName val compatiblePackage = patch.compatiblePackages?.first() - val latestVersion = compatiblePackage?.versions?.maxByOrNull { it.replace(".", "").toInt() } ?: "all" + val latestVersion = + compatiblePackage?.versions?.map { SemanticVersion.fromString(it) }?.maxWithOrNull(SemanticVersionComparator) ?: "all" patches.appendLine("| `$patchName` | ${patch.description} | `${compatiblePackage?.name}` | $latestVersion |") } diff --git a/src/main/kotlin/app/revanced/meta/readme/Semver.kt b/src/main/kotlin/app/revanced/meta/readme/Semver.kt new file mode 100644 index 0000000000..4100d929e2 --- /dev/null +++ b/src/main/kotlin/app/revanced/meta/readme/Semver.kt @@ -0,0 +1,22 @@ +data class SemanticVersion(val major: Int, val minor: Int, val patch: Int) { + companion object { + fun fromString(version: String): SemanticVersion { + var parts = version.split(".") + + if (parts.count() != 3) throw IllegalArgumentException("Invalid semantic version") + + val versionNumbers = parts.map { it.toInt() } + return SemanticVersion(versionNumbers[0], versionNumbers[1], versionNumbers[2]) + } + } + + override fun toString(): String = "$major.$minor.$patch" +} + +object SemanticVersionComparator : Comparator { + override fun compare(a: SemanticVersion, b: SemanticVersion): Int = when { + a.major != b.major -> a.major - b.major + a.minor != b.minor -> a.minor - b.minor + else -> a.patch - b.patch + } +}