diff --git a/docs/operations/bukkit-workflow.md b/docs/operations/bukkit-workflow.md index f3f0c51..f6ac7d6 100644 --- a/docs/operations/bukkit-workflow.md +++ b/docs/operations/bukkit-workflow.md @@ -166,3 +166,7 @@ runtime.help-console-navigation-title、admin.inspect-*-title 及 admin.inspect- ## 2026-10-29 Doctor 語系值與色彩 健康檢查的一般欄位與已知狀態透過 `admin.doctor-value.*` 翻譯,沿用 config 的預設/客戶端語系政策。選配整合未啟用顯示紅色說明,不等於插件健康失敗;未知代碼中性灰保留原值。0 獎勵數量灰色,正數待執行黃/執行中青/結果待確認金。Console 使用黑底可讀的 legacy 色與每行 prefix,玩家標題 prefix 一次。僅 Folia 更新,驗收及資料保留證據見 `../testing/evidence/2026-10-29-doctor-localization.json`。 + +## 查詢 JAR 來源 + +/ic admin doctor 顯示 Git 提交前 7 碼、封裝時工作目錄是否有變更,以及 UTC 打包時間。完整提交存於 JAR 的 META-INF/MANIFEST.MF。打包時間以 ISO 8601 UTC 格式呈現,例如 `2026-11-03T10:17:44.517Z`。這是封裝任務執行時寫入的時間。重用既有產物時沿用原時間。Git 不可用時顯示未知,舊 JAR 缺少欄位時也顯示未知。工作目錄變更包含 Git 看得到的未追蹤檔案。 diff --git a/invitecode-plugin/build.gradle.kts b/invitecode-plugin/build.gradle.kts index 3d2b3ad..d9b9bc1 100644 --- a/invitecode-plugin/build.gradle.kts +++ b/invitecode-plugin/build.gradle.kts @@ -8,6 +8,8 @@ import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.testing.Test import java.nio.ByteBuffer import java.nio.charset.StandardCharsets +import java.time.Instant +import java.util.jar.Manifest import java.util.zip.ZipFile plugins { @@ -29,6 +31,11 @@ abstract class VerifyPluginJar : DefaultTask() { val archive = archiveFile.get().asFile ZipFile(archive).use { zip -> + val metadata = zip.getInputStream(requireNotNull(zip.getEntry("META-INF/MANIFEST.MF"))) + .use { Manifest(it).mainAttributes } + check(metadata.getValue("InviteCode-Git-Commit").matches(Regex("[0-9a-f]{40}|[0-9a-f]{64}|unknown"))) + check(metadata.getValue("InviteCode-Git-Dirty") in setOf("true", "false", "unknown")) + Instant.parse(metadata.getValue("InviteCode-Built-At")) requiredEntries.get().forEach { entry -> check(zip.getEntry(entry) != null) { "${archive.name} is missing required entry $entry" @@ -115,8 +122,35 @@ tasks.processResources { } } +fun gitOutput(vararg args: String): String? = try { + if (!rootDir.resolve(".git").exists()) null else providers.exec { + workingDir(rootDir) + commandLine("git", *args) + isIgnoreExitValue = true + }.let { if (it.result.get().exitValue == 0) it.standardOutput.asText.get().trim() else null } +} catch (_: org.gradle.process.internal.ExecException) { + // Source archives and machines without Git can still build the plugin. + null +} + +val gitCommit = providers.provider { gitOutput("rev-parse", "--verify", "HEAD") ?: "unknown" } +val gitDirty = providers.provider { + gitOutput("status", "--porcelain", "--untracked-files=normal")?.isNotEmpty()?.toString() ?: "unknown" +} + val shadowJar = tasks.named("shadowJar") { inputs.property("java8CompatibilityLayout", "v1") + inputs.property("inviteCodeGitCommit", gitCommit) + inputs.property("inviteCodeGitDirty", gitDirty) + doFirst { + manifest.attributes( + mapOf( + "InviteCode-Git-Commit" to inputs.properties.getValue("inviteCodeGitCommit"), + "InviteCode-Git-Dirty" to inputs.properties.getValue("inviteCodeGitDirty"), + "InviteCode-Built-At" to Instant.now().toString(), + ), + ) + } archiveBaseName.set("InviteCode") archiveClassifier.set("") exclude("module-info.class", "META-INF/versions/**") diff --git a/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/BuildProvenance.kt b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/BuildProvenance.kt new file mode 100644 index 0000000..d1ab91b --- /dev/null +++ b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/BuildProvenance.kt @@ -0,0 +1,51 @@ +package io.github.command1264.invitecode.plugin + +import java.io.IOException +import java.nio.file.Files +import java.nio.file.Path +import java.time.Instant +import java.time.format.DateTimeParseException +import java.util.jar.Attributes +import java.util.jar.JarFile +import java.util.logging.Logger + +internal data class BuildProvenance( + val commit: String? = null, + val dirty: Boolean? = null, + val builtAt: Instant? = null, +) { + fun doctorValues(): Map = mapOf( + "commit" to (commit?.take(7) ?: "unknown"), + "dirty" to (dirty?.toString() ?: "unknown"), + "built_at" to (builtAt?.toString() ?: "unknown"), + ) + + companion object { + private val gitHash = Regex("[0-9a-f]{40}|[0-9a-f]{64}") + + fun read(jar: Path, logger: Logger): BuildProvenance { + if (!Files.isRegularFile(jar)) return BuildProvenance() + return try { + JarFile(jar.toFile()).use { fromAttributes(it.manifest?.mainAttributes) } + } catch (error: IOException) { + logger.warning("Cannot read InviteCode build metadata: ${error.javaClass.simpleName}") + BuildProvenance() + } + } + + fun fromAttributes(attributes: Attributes?): BuildProvenance { + val builtAt = attributes?.getValue("InviteCode-Built-At")?.let { + try { + Instant.parse(it) + } catch (_: DateTimeParseException) { + null + } + } + return BuildProvenance( + attributes?.getValue("InviteCode-Git-Commit")?.takeIf(gitHash::matches), + attributes?.getValue("InviteCode-Git-Dirty")?.toBooleanStrictOrNull(), + builtAt, + ) + } + } +} diff --git a/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayouts.kt b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayouts.kt index 6130d34..1095abf 100644 --- a/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayouts.kt +++ b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayouts.kt @@ -19,6 +19,8 @@ internal class DoctorMessageLayouts( val body = presenter.render(locale, key, values, audience, PrefixPlacement.NONE) val fragments = values.mapNotNull { (name, value) -> val suffix = when (name) { + "commit", "built_at" -> if (value == "unknown") "build-unknown" else null + "dirty" -> if (value == "unknown") "build-unknown" else known("enabled", value, setOf("true", "false")) "phase" -> known("phase", value, setOf("ACTIVE", "STARTING", "MAINTENANCE", "STOPPING", "STOPPED")) "executor" -> known("executor", value, setOf("ACCEPTING", "STOPPED")) "placeholder" -> known("enabled", value, setOf("true", "false")) diff --git a/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/InviteCodePlugin.kt b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/InviteCodePlugin.kt index 7e0a6d6..2a8577a 100644 --- a/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/InviteCodePlugin.kt +++ b/invitecode-plugin/src/main/kotlin/io/github/command1264/invitecode/plugin/InviteCodePlugin.kt @@ -53,6 +53,7 @@ import java.util.UUID import java.util.logging.Level class InviteCodePlugin : JavaPlugin() { + private val buildProvenance by lazy { BuildProvenance.read(file.toPath(), logger) } private val runtime = PluginRuntimeState() private val configuration = AtomicRuntimeSnapshot() private val storage = AtomicReference() @@ -376,6 +377,7 @@ class InviteCodePlugin : JavaPlugin() { val store = storage.get() val capabilities = modernIntegration?.capabilities val sections = mutableListOf( + "admin.doctor-build" to buildProvenance.doctorValues(), "admin.doctor-runtime" to mapOf("phase" to runtime.phase.name, "generation" to snapshot.generation.toString(), "executor" to if (executor?.acceptingOperations == true) "ACCEPTING" else "STOPPED"), diff --git a/invitecode-plugin/src/main/resources/config/messages/en_US.yml b/invitecode-plugin/src/main/resources/config/messages/en_US.yml index dac1071..5137bb6 100644 --- a/invitecode-plugin/src/main/resources/config/messages/en_US.yml +++ b/invitecode-plugin/src/main/resources/config/messages/en_US.yml @@ -307,6 +307,10 @@ admin: &fRuntime: {phase} &fConfiguration generation: {generation} &fBackground tasks: {executor} + doctor-build: |- + &7Git commit: &f{commit} + &7Working tree has changes: &f{dirty} + &7Packaging time (UTC): &f{built_at} doctor-integrations: |- &fPlaceholderAPI: {placeholder} &fVault economy service: {vault} @@ -323,6 +327,7 @@ admin: doctor-storage-failed: "&cThe storage health check failed, so check the server log." doctor-executor-stopping: "&eThe executor is stopping and cannot query pending reward actions." doctor-value: + build-unknown: "&7Unknown" phase: active: "&aActive" starting: "&eStarting" diff --git a/invitecode-plugin/src/main/resources/config/messages/zh_TW.yml b/invitecode-plugin/src/main/resources/config/messages/zh_TW.yml index a16a121..4c995fb 100644 --- a/invitecode-plugin/src/main/resources/config/messages/zh_TW.yml +++ b/invitecode-plugin/src/main/resources/config/messages/zh_TW.yml @@ -311,6 +311,10 @@ admin: &f執行階段:{phase} &f設定世代:{generation} &f背景工作:{executor} + doctor-build: |- + &7Git 提交:&f{commit} + &7工作目錄有變更:&f{dirty} + &7打包時間(UTC):&f{built_at} doctor-integrations: |- &fPlaceholderAPI:{placeholder} &fVault 經濟服務:{vault} @@ -329,6 +333,7 @@ admin: doctor-storage-failed: "&c儲存健康檢查失敗,請檢查伺服器紀錄。" doctor-executor-stopping: "&e背景工作正在停止,無法查詢待處理的獎勵動作。" doctor-value: + build-unknown: "&7未知" phase: active: "&a運作中" starting: "&e啟動中" diff --git a/invitecode-plugin/src/test/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayoutsTest.kt b/invitecode-plugin/src/test/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayoutsTest.kt index 85b28c0..c8af109 100644 --- a/invitecode-plugin/src/test/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayoutsTest.kt +++ b/invitecode-plugin/src/test/kotlin/io/github/command1264/invitecode/plugin/DoctorMessageLayoutsTest.kt @@ -9,6 +9,32 @@ import java.nio.file.Path class DoctorMessageLayoutsTest { @TempDir lateinit var directory: Path + @Test + fun `doctor shows packaged provenance and localizes missing fields`() { + val jar = directory.resolve("plugin.jar") + val manifest = java.util.jar.Manifest() + val commit = "0123456789abcdef0123456789abcdef01234567" + val builtAt = "2026-11-03T09:30:00Z" + manifest.mainAttributes.apply { + putValue("Manifest-Version", "1.0") + putValue("InviteCode-Git-Commit", commit) + putValue("InviteCode-Git-Dirty", "true") + putValue("InviteCode-Built-At", builtAt) + } + java.util.jar.JarOutputStream(java.nio.file.Files.newOutputStream(jar), manifest).close() + val messages = BukkitConfigurationLoader(directory.resolve("config")) {}.load().messages + val provenance = BuildProvenance.read(jar, java.util.logging.Logger.getAnonymousLogger()) + assertEquals(commit, provenance.commit) + val english = DoctorMessageLayouts(messages, "en_US", MessageAudience.CONSOLE) + val report = english.render("admin.doctor-build", provenance.doctorValues()).plainText + assertEquals("Git commit: 0123456", report.lineSequence().first()) + assertTrue(report.contains("Packaging time (UTC): " + builtAt)) + + val chinese = DoctorMessageLayouts(messages, "zh_TW", MessageAudience.RGB_PLAYER) + val unknown = chinese.render("admin.doctor-build", BuildProvenance.fromAttributes(null).doctorValues()).plainText + assertEquals(listOf("Git 提交:未知", "工作目錄有變更:未知", "打包時間(UTC):未知"), unknown.lines()) + } + @Test fun `formatted doctor reaches console with one prefix per line and value colors intact`() { val messages = BukkitConfigurationLoader(directory) {}.load().messages