Skip to content

Commit

Permalink
包装 ForgedVersion,添加 TacoVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Taskeren committed Aug 16, 2022
1 parent 21f2578 commit 230acf8
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 96 deletions.
58 changes: 0 additions & 58 deletions cli/pom.xml

This file was deleted.

19 changes: 12 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
<groupId>cn.taskeren</groupId>
<artifactId>forged-version</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>cli</module>
</modules>
<packaging>pom</packaging>
<packaging>jar</packaging>

<name>cn.taskeren forged-version</name>
<name>forged-version</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.7.0-Beta</kotlin.version>
<kotlin.version>1.7.10</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<junit.version>4.13.1</junit.version>
</properties>
Expand Down Expand Up @@ -51,7 +48,7 @@
</dependencies>

<build>
<sourceDirectory>src/cn.taskeren.version.cli.main/kotlin</sourceDirectory>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>

<plugins>
Expand All @@ -76,6 +73,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.taskeren.version.cli

import cn.taskeren.version.ForgedVersion
import cn.taskeren.version.ForgedVersionChannel
import cn.taskeren.version.forge.ForgedVersion
import cn.taskeren.version.forge.ForgedVersionChannel
import com.google.gson.GsonBuilder
import java.io.File

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package cn.taskeren.version
package cn.taskeren.version.forge

import com.google.gson.JsonObject
import java.util.*
import org.apache.maven.artifact.versioning.ComparableVersion

class ForgedVersion(
val homepage: String? = null,
val channels: List<ForgedVersionChannel> = mutableListOf()
) {

/**
* Check version status of the current application.
*
* @param channel the update channel, like `release`, `snapshot`, `nightly`, etc.
* @param current the version of the current application
*/
fun check(channel: String, current: ComparableVersion): VersionCheckResult {
var status = VersionStatus.PENDING
var target: ComparableVersion? = null
val changes = linkedMapOf<ComparableVersion, String>()

runCatching {
val ch = channels.find { it.name == channel } ?: error("Can't find channel[$channel]")

val latest = ch.latest
val recommended = ch.recommended
if(recommended != null) {
if(current == recommended.version) {
status = VersionStatus.UP_TO_DATE
} else if(current > recommended.version) {
status = VersionStatus.AHEAD
if(current < latest.version) {
status = VersionStatus.OUTDATED
target = latest.version
}
} else {
status = VersionStatus.OUTDATED
target = recommended.version
}
} else {
if(current < latest.version) {
status = VersionStatus.BETA_OUTDATED
target = latest.version
} else {
status = VersionStatus.BETA
}
}

ch.forEach { ver ->
if(ver.version > current && (target == null || ver.version.compareTo(target) < 1)) {
changes[ver.version] = ver.changelog
}
}
}.onFailure {
status = VersionStatus.FAILED
}

return VersionCheckResult(status, current, target, changes, homepage)
}

/**
* Convert this [ForgedVersion] to MinecraftForge-styled [JsonObject].
*
Expand Down Expand Up @@ -66,8 +116,16 @@ class ForgedVersion(
fun build(): ForgedVersion = ForgedVersion(homepage, channels)
}

@Suppress("DEPRECATION")
companion object {
@JvmStatic
fun build(homepage: String? = null, func: Builder.() -> Unit) = Builder(homepage).apply(func).build()

@JvmStatic
fun fromJson(jsonStr: String) = ForgedVersionChecker.fromString(jsonStr)

@JvmStatic
fun fromJson(jsonObject: JsonObject) = ForgedVersionChecker.fromJson(jsonObject)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package cn.taskeren.version
package cn.taskeren.version.forge

import org.apache.maven.artifact.versioning.ComparableVersion

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
@file:Suppress("UNCHECKED_CAST")

package cn.taskeren.version
package cn.taskeren.version.forge

import cn.taskeren.version.VersionStatus.*
import cn.taskeren.version.forge.VersionStatus.*
import com.google.gson.*
import org.apache.maven.artifact.versioning.ComparableVersion

@Deprecated("deprecated")
@Suppress("DEPRECATION")
object ForgedVersionChecker {

@JvmStatic
@Deprecated("deprecated", replaceWith = ReplaceWith("ForgedVersion.fromJson(versionJsonStr)"))
fun fromString(versionJsonStr: String): ForgedVersion = fromJson(JsonParser.parseString(versionJsonStr))

@JvmStatic
@Deprecated("deprecated", replaceWith = ReplaceWith("ForgedVersion.fromJson(versionJson)"))
fun fromJson(versionJson: JsonElement): ForgedVersion {

val json = Gson().fromJson<Map<String, Any>>(versionJson, Map::class.java)
Expand Down Expand Up @@ -57,7 +61,14 @@ object ForgedVersionChecker {
}

@JvmStatic
fun check(channelVer: String, versionJsonStr: String, currentVersionStr: String): Result {
@Deprecated(
"deprecated",
replaceWith = ReplaceWith(
expression = "ForgedVersion.fromJson(versionJsonStr).check(channelVer, ComparableVersion(currentVersionStr))",
imports = ["org.apache.maven.artifact.versioning.ComparableVersion"]
)
)
fun check(channelVer: String, versionJsonStr: String, currentVersionStr: String): VersionCheckResult {

var status = PENDING
val curVer = ComparableVersion(currentVersionStr)
Expand Down Expand Up @@ -121,17 +132,7 @@ object ForgedVersionChecker {
status = FAILED
}

return Result(status, curVer, targetVer, changes, homepage)
}

data class Result(
val status: VersionStatus,
val current: ComparableVersion?,
val target: ComparableVersion?,
val changes: LinkedHashMap<ComparableVersion, String>,
val homepage: String?
) {
fun printForgeLog() = println("Found status: $status Current: $current Target: $target")
return VersionCheckResult(status, curVer, targetVer, changes, homepage)
}

}
13 changes: 13 additions & 0 deletions src/main/kotlin/cn/taskeren/version/forge/VersionCheckResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.taskeren.version.forge

import org.apache.maven.artifact.versioning.ComparableVersion

data class VersionCheckResult(
val status: VersionStatus,
val current: ComparableVersion?,
val target: ComparableVersion?,
val changes: LinkedHashMap<ComparableVersion, String>,
val homepage: String?
) {
fun printForgeLog() = println("Found status: $status Current: $current Target: $target")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.taskeren.version
package cn.taskeren.version.forge

enum class VersionStatus {

Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/cn/taskeren/version/taco/TacoConst.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.taskeren.version.taco

object TacoConst {

var tacoNameValidator: String.() -> Boolean = { !contains('-') }

}
43 changes: 43 additions & 0 deletions src/main/kotlin/cn/taskeren/version/taco/TacoVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cn.taskeren.version.taco

import org.apache.maven.artifact.versioning.ComparableVersion

/**
* Taco Version V1
*/
data class TacoVersion(
val name: String,
val versions: LinkedHashSet<TacoVersionEntry> = linkedSetOf(),
val subVersions: LinkedHashMap<String, TacoVersion> = linkedMapOf()
): Iterable<TacoVersion.TacoVersionEntry> {

var parent: TacoVersion? = null

val fullName: String get() = if(parent != null) "${parent!!.fullName}/${name}" else name

data class TacoVersionEntry(val version: ComparableVersion, val properties: Map<String, Any> = mapOf()) {
override fun hashCode(): Int {
return version.hashCode()
}

override fun equals(other: Any?): Boolean {
return other is TacoVersionEntry && version == other.version && properties == other.properties
}
}

fun addVersion(version: ComparableVersion, properties: Map<String, Any>) {
versions += TacoVersionEntry(version, properties)
}

fun removeVersion(version: ComparableVersion): Boolean {
return versions.removeAll { it.version == version }
}

fun getOrCreateSub(subName: String): TacoVersion {
return subVersions.computeIfAbsent(subName) { createSub(subName) }
}

internal fun createSub(subName: String) = TacoVersion(subName).apply { this.parent = this@TacoVersion; this@TacoVersion.subVersions[subName] = this }

override fun iterator(): Iterator<TacoVersionEntry> = versions.iterator()
}
Loading

0 comments on commit 230acf8

Please sign in to comment.