Skip to content

Commit

Permalink
Flexver for version comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jul 26, 2024
1 parent e929543 commit 0882a22
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class KordExPlugin : Plugin<Project> {
private val kordExReleases = mavenResolver.getKordExReleases()
private val kordExSnapshots = mavenResolver.getKordExSnapshots()

private val latestKordVersion = maxOf(kordReleases, kordSnapshots) { left, right ->
left.versioning.lastUpdated.toLong().compareTo(right.versioning.lastUpdated.toLong())
private val latestKordMetadata = maxOf(kordReleases, kordSnapshots) { left, right ->
left.getCurrentVersion().compareTo(right.getCurrentVersion())
}

private val latestKordExVersion = maxOf(kordExReleases, kordExSnapshots) { left, right ->
left.versioning.lastUpdated.toLong().compareTo(right.versioning.lastUpdated.toLong())
private val latestKordExMetadata = maxOf(kordExReleases, kordExSnapshots) { left, right ->
left.getCurrentVersion().compareTo(right.getCurrentVersion())
}

override fun apply(target: Project) {
Expand All @@ -65,13 +65,11 @@ class KordExPlugin : Plugin<Project> {
addDependencies(target, extension, kordExVersion, kordVersion)
}

private fun calculateVersions(extension: KordExExtension): Triple<String, String?, GradleMetadata> {
private fun calculateVersions(extension: KordExExtension): Triple<Version, Version?, GradleMetadata> {
val kordExVersion = if (extension.kordExVersion == null || extension.kordExVersion == "latest") {
latestKordExVersion.versioning.latest
?: latestKordVersion.versioning.version
?: latestKordVersion.version
latestKordExMetadata.getCurrentVersion()
} else {
extension.kordExVersion
extension.kordExVersion?.let { Version(it) }
}!!

val kordExGradle = gradleResolver.kordEx(kordExVersion)
Expand All @@ -83,13 +81,12 @@ class KordExPlugin : Plugin<Project> {
.dependencies
.first { it.module == "kord-core-voice" }
.version["requires"]
?.let { Version(it) }
}

"latest" -> latestKordVersion.versioning.latest
?: latestKordVersion.versioning.version
?: latestKordVersion.version
"latest" -> latestKordMetadata.getCurrentVersion()

else -> extension.kordVersion
else -> extension.kordVersion?.let { Version(it) }
}

return Triple(kordExVersion, kordVersion, kordExGradle)
Expand All @@ -115,8 +112,8 @@ class KordExPlugin : Plugin<Project> {
private fun addDependencies(
target: Project,
extension: KordExExtension,
kordExVersion: String,
kordVersion: String?
kordExVersion: Version,
kordVersion: Version?
) {
target.afterEvaluate {
dependencies {
Expand Down Expand Up @@ -279,8 +276,8 @@ class KordExPlugin : Plugin<Project> {
private fun addGeneratedFiles(
target: Project,
extension: KordExExtension,
kordVersion: String?,
kordExVersion: String
kordVersion: Version?,
kordExVersion: Version
) {
val outputDir = target.layout.buildDirectory.dir("generated")
val outputFile = target.layout.buildDirectory.file("generated/kordex.properties")
Expand All @@ -296,8 +293,8 @@ class KordExPlugin : Plugin<Project> {

properties.setProperty("settings.dataCollection", extension.dataCollection.readable)
properties.setProperty("modules", extension.modules.joinToString())
properties.setProperty("versions.kordEx", kordExVersion)
properties.setProperty("versions.kord", kordVersion)
properties.setProperty("versions.kordEx", kordExVersion.version)
properties.setProperty("versions.kord", kordVersion?.version)

properties.store(outputFile.get().asFile.writer(), null)
}
Expand Down
50 changes: 50 additions & 0 deletions kordex/src/main/kotlin/dev/kordex/gradle/plugins/kordex/Version.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.kordex.gradle.plugins.kordex

import com.unascribed.flexver.FlexVerComparator

class Version(val version: String) {
override fun toString(): String =
version
}

operator fun Version?.compareTo(other: Version?): Int {
if ((this == null) xor ( other == null)) {
if (this == null) {
return -1
}

return 1
} else if (this == null && other == null) {
return 0
}

return FlexVerComparator.compare(this!!.version, other!!.version)
}

operator fun String?.compareTo(other: Version?): Int {
if ((this == null) xor ( other == null)) {
if (this == null) {
return -1
}

return 1
} else if (this == null && other == null) {
return 0
}

return FlexVerComparator.compare(this!!, other!!.version)
}

operator fun Version?.compareTo(other: String?): Int {
if ((this == null) xor ( other == null)) {
if (this == null) {
return -1
}

return 1
} else if (this == null && other == null) {
return 0
}

return FlexVerComparator.compare(this!!.version, other!!)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

package dev.kordex.gradle.plugins.kordex.resolvers

import dev.kordex.gradle.plugins.kordex.kordExReleasesUrl
import dev.kordex.gradle.plugins.kordex.kordExSnapshotUrl
import dev.kordex.gradle.plugins.kordex.kordReleasesUrl
import dev.kordex.gradle.plugins.kordex.kordSnapshotUrl
import dev.kordex.gradle.plugins.kordex.*
import dev.kordex.gradle.plugins.kordex.resolvers.gradle.GradleMetadata
import io.ktor.client.*
import io.ktor.client.call.*
Expand All @@ -36,13 +33,19 @@ class GradleMetadataResolver {
}
}

fun kordEx(version: Version) =
kordEx(version.version)

fun kordEx(version: String) =
if (version.endsWith("-SNAPSHOT")) {
getKordExSnapshot(version)
} else {
getKordExRelease(version)
}

fun kord(version: Version) =
kord(version.version)

fun kord(version: String) =
if (version.endsWith("-SNAPSHOT")) {
getKordSnapshot(version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.kordex.gradle.plugins.kordex.resolvers.gradle

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
import dev.kordex.gradle.plugins.kordex.Version as KXVersion

@Serializable
data class GradleMetadata(
Expand All @@ -23,7 +24,9 @@ data class GradleMetadata(
val module: String,
val version: String,
val attributes: Map<String, String> = mapOf()
)
) {
fun version(): KXVersion = KXVersion(version)
}

@Serializable
data class Variant(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@

package dev.kordex.gradle.plugins.kordex.resolvers.maven

import org.gradle.kotlin.dsl.closureOf
import dev.kordex.gradle.plugins.kordex.Version
import dev.kordex.gradle.plugins.kordex.compareTo

class MavenMetadata(private val xml: XMLMavenMetadata) {
val artifactId: String get() = xml.artifactId.value
val groupId: String get() = xml.groupId.value
val version: String? get() = xml.version?.value
val version: Version? get() = xml.version?.value?.let { Version(it) }

val versioning: Versioning by lazy { Versioning(xml.versioning) }

fun getCurrentVersion(): Version? {
val versions = versioning.snapshotVersions?.map { it.value }
?: versioning.versions
?: return null

return versions.maxWithOrNull(Version::compareTo)
}

override fun toString(): String =
"MavenMetadata(artifactId=\"$artifactId\", groupId=\"$groupId\", versioning=\"$versioning\")"

class Versioning(private val xml: XMLMavenMetadata.XMLVersioning) {
val lastUpdated: String get() = xml.lastUpdated.value
val latest: String? get() = xml.latest?.value
val release: String? get() = xml.release?.value
val version: String? get() = xml.version?.value
val latest: Version? get() = xml.latest?.value?.let { Version(it) }
val release: Version? get() = xml.release?.value?.let { Version(it) }
val version: Version? get() = xml.version?.value?.let { Version(it) }

val snapshot by lazy {
xml.snapshot?.let { Snapshot(it) }
Expand All @@ -32,22 +41,22 @@ class MavenMetadata(private val xml: XMLMavenMetadata) {
xml.snapshotVersions?.value?.map { SnapshotVersion(it) }
}

val versions: List<String>? by lazy {
xml.versions?.value?.map { it.value }
val versions: List<Version>? by lazy {
xml.versions?.value?.map { Version(it.value) }
}

override fun toString(): String =
"Versioning(lastUpdated=\"$lastUpdated\", latest=\"$latest\", versions=\"$versions\")"

class Snapshot(private val xml: XMLMavenMetadata.XmlSnapshot) {
class Snapshot(xml: XMLMavenMetadata.XmlSnapshot) {
val timestamp: String = xml.timestamp.value
val buildNumber: Long = xml.buildNumber.value
}

class SnapshotVersion(private val xml: XMLMavenMetadata.XmlSnapshotVersions.XmlSnapshotVersion) {
val classifier: String? get() = xml.classifier?.value
val extension: String get() = xml.extension.value
val value: String get() = xml.value.value
val value: Version get() = Version(xml.value.value)
val updated: Long get() = xml.updated.value
}
}
Expand Down

0 comments on commit 0882a22

Please sign in to comment.