Skip to content

Commit

Permalink
docs: properly handle semantic versions (#156) [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
bogadana authored Jul 11, 2022
1 parent c06e9b0 commit e6029d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/kotlin/app/revanced/meta/readme/Generator.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 |")
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/app/revanced/meta/readme/Semver.kt
Original file line number Diff line number Diff line change
@@ -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<SemanticVersion> {
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
}
}

0 comments on commit e6029d6

Please sign in to comment.