From 7ac1d94db9dbfe7cceaefc372606ab95ee9d3454 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 21 Jun 2020 14:24:17 +0800 Subject: [PATCH] Add build constant auto-fill --- backend/mirai-console/build.gradle.kts | 29 +++++++++++++++++++ .../net/mamoe/mirai/console/MiraiConsole.kt | 26 +++++++++++------ .../mirai/console/MiraiConsoleFrontEnd.kt | 13 --------- .../console/pure/MiraiConsoleFrontEndPure.kt | 3 -- .../mirai/console/pure/MiraiConsolePure.kt | 3 -- 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/backend/mirai-console/build.gradle.kts b/backend/mirai-console/build.gradle.kts index 14d759c33..94a66ed5e 100644 --- a/backend/mirai-console/build.gradle.kts +++ b/backend/mirai-console/build.gradle.kts @@ -1,4 +1,6 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import upload.Bintray +import java.text.SimpleDateFormat import java.util.* plugins { @@ -74,6 +76,33 @@ tasks { "test"(Test::class) { useJUnitPlatform() } + + val compileKotlin by getting {} + + val fillBuildConstants by registering { + doLast { + (compileKotlin as KotlinCompile).source.filter { it.name == "MiraiConsole.kt" }.single().let { file -> + file.writeText(file.readText() + .replace(Regex("""val buildDate: Date = Date\((.*)\) //(.*)""")) { + """ + val buildDate: Date = Date(${System.currentTimeMillis()}) // ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").apply { + timeZone = TimeZone.getTimeZone("GMT+8") + }.format(Date())} + """.trimIndent() + } + .replace(Regex("""const val version: String = "(.*)"""")) { + """ + const val version: String = "${Versions.console}" + """.trimIndent() + } + ) + } + } + } + + "compileKotlin" { + dependsOn(fillBuildConstants) + } } // region PUBLISHING diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 5b9fe51fc..168d2d568 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -13,6 +13,8 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.io.charsets.Charset import net.mamoe.mirai.Bot +import net.mamoe.mirai.console.center.CuiPluginCenter +import net.mamoe.mirai.console.center.PluginCenter import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.utils.DefaultLogger @@ -21,6 +23,7 @@ import net.mamoe.mirai.utils.MiraiLogger import java.io.ByteArrayOutputStream import java.io.File import java.io.PrintStream +import java.util.* import kotlin.coroutines.CoroutineContext internal object MiraiConsoleInitializer { @@ -33,26 +36,34 @@ internal object MiraiConsoleInitializer { } +internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants) + @JvmStatic + val buildDate: Date = Date(1592720608995) // 2020-06-21 14:23:28 + const val version: String = "0.5.1" +} + /** * mirai 控制台实例. */ object MiraiConsole : CoroutineScope, IMiraiConsole { + val pluginCenter: PluginCenter get() = CuiPluginCenter + private val instance: IMiraiConsole get() = MiraiConsoleInitializer.instance /** * `mirai-console` build 号 + * + * UTC+8 时间 */ - @MiraiExperimentalAPI - override val build: String - get() = instance.build + @JvmStatic + val buildDate: Date + get() = MiraiConsoleBuildConstants.buildDate /** * `mirai-console` 版本 */ - @MiraiExperimentalAPI - override val version: String - get() = instance.version + const val version: String = MiraiConsoleBuildConstants.version /** * Console 运行路径 @@ -89,9 +100,6 @@ object MiraiConsole : CoroutineScope, IMiraiConsole { // 前端使用 internal interface IMiraiConsole : CoroutineScope { - val build: String - val version: String - /** * Console 运行路径 */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt index f94a8eaea..48043cb76 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt @@ -10,8 +10,6 @@ package net.mamoe.mirai.console import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.center.CuiPluginCenter -import net.mamoe.mirai.console.center.PluginCenter import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.MiraiLogger @@ -22,11 +20,6 @@ import net.mamoe.mirai.utils.MiraiLogger */ @MiraiInternalAPI interface MiraiConsoleFrontEnd { - /** - * 提供 [PluginCenter] - */ - val pluginCenter: PluginCenter get() = CuiPluginCenter - fun loggerFor(identity: String?): MiraiLogger /** @@ -43,12 +36,6 @@ interface MiraiConsoleFrontEnd { bot: Bot ) - fun pushVersion( - consoleVersion: String, - consoleBuild: String, - coreVersion: String - ) - /** * 让 UI 层提供一个输入, 相当于 [readLine] */ diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleFrontEndPure.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleFrontEndPure.kt index 39f329382..850bff7a5 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleFrontEndPure.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleFrontEndPure.kt @@ -60,9 +60,6 @@ object MiraiConsoleFrontEndPure : MiraiConsoleFrontEnd { override fun pushBot(bot: Bot) { } - override fun pushVersion(consoleVersion: String, consoleBuild: String, coreVersion: String) { - } - override suspend fun requestInput(hint: String): String { if (hint.isNotEmpty()) { ConsoleUtils.lineReader.printAbove( diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePure.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePure.kt index 5e0ab01a9..0cb890027 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePure.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePure.kt @@ -35,15 +35,12 @@ import kotlin.coroutines.EmptyCoroutineContext private val delegateScope = CoroutineScope(EmptyCoroutineContext) object MiraiConsolePure : IMiraiConsole { - override val build: String = "UNKNOWN" override val builtInPluginLoaders: List> = LinkedList() override val frontEnd: MiraiConsoleFrontEnd = MiraiConsoleFrontEndPure override val mainLogger: MiraiLogger = DefaultLogger("Console") override val rootDir: File = File("./test/console").also { it.mkdirs() } - override val version: String - get() = "UNKNOWN" override val coroutineContext: CoroutineContext get() = delegateScope.coroutineContext } \ No newline at end of file