From e6029d671673003c7b578fea81c60e78c53c474a Mon Sep 17 00:00:00 2001 From: bogadana <30848157+bogadana@users.noreply.github.com> Date: Mon, 11 Jul 2022 21:08:47 +0200 Subject: [PATCH] docs: properly handle semantic versions (#156) [skip ci] --- .../app/revanced/meta/readme/Generator.kt | 5 ++++- .../kotlin/app/revanced/meta/readme/Semver.kt | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/app/revanced/meta/readme/Semver.kt 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 + } +}