From 9af8d74f365749d7899c4459177b88a2df18370e Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 24 Jun 2020 02:05:04 +0800 Subject: [PATCH] Simplify codegen launcher --- .../mamoe/mirai/console/codegen/Codegen.kt | 8 +- .../mirai/console/codegen/ValueKtCodegen.kt | 43 ------ .../console/codegen/ValueSettingCodegen.kt | 135 ++++++++++++++++++ .../codegen/_Setting.value.kt codegen.kt | 37 ----- 4 files changed, 142 insertions(+), 81 deletions(-) delete mode 100644 backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueKtCodegen.kt create mode 100644 backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt delete mode 100644 backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/_Setting.value.kt codegen.kt diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt index 35cc397ce..5292c566e 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt @@ -89,10 +89,16 @@ abstract class Codegen { protected abstract fun StringBuilder.apply(ktType: KtType) } -abstract class RegionCodegen(regionName: String? = null) : Codegen() { +abstract class RegionCodegen(private val targetFile: String, regionName: String? = null) : Codegen() { val regionName: String by lazy { regionName ?: this::class.simpleName!!.substringBefore("Codegen") } + + fun startIndependent() { + codegen(targetFile) { + this@RegionCodegen.invoke() + } + } } abstract class PrimitiveCodegen : Codegen() { diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueKtCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueKtCodegen.kt deleted file mode 100644 index 57fa7e63e..000000000 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueKtCodegen.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("PRE_RELEASE_CLASS") - -package net.mamoe.mirai.console.codegen - - -object ValueKtCodegen { - object SettingCodegen { - object PrimitiveValuesCodegen : RegionCodegen(), DefaultInvoke { - override val defaultInvokeArgs: List - get() = KtPrimitives + KtString - - override fun StringBuilder.apply(ktType: KtType) { - @Suppress("ClassName") - appendKCode( - """ - /** - * Represents a non-null [$ktType] value. - */ - interface ${ktType}Value : PrimitiveValue<$ktType> - """ - ) - } - } - - } - - @JvmStatic - fun main(args: Array) { - codegen("Value.kt") { - SettingCodegen.PrimitiveValuesCodegen() - } - } -} - diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt new file mode 100644 index 000000000..b8c195462 --- /dev/null +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt @@ -0,0 +1,135 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:Suppress("PRE_RELEASE_CLASS", "ClassName") + +package net.mamoe.mirai.console.codegen + +object ValueSettingCodegen { + /** + * The interface + */ + object PrimitiveValuesCodegen : RegionCodegen("Value.kt"), DefaultInvoke { + @JvmStatic + fun main(args: Array) = super.startIndependent() + + override val defaultInvokeArgs: List + get() = KtPrimitives + KtString + + override fun StringBuilder.apply(ktType: KtType) { + @Suppress("ClassName") + appendKCode( + """ + /** + * Represents a non-null [$ktType] value. + */ + interface ${ktType}Value : PrimitiveValue<$ktType> + """ + ) + } + } + + object BuiltInSerializerConstantsPrimitivesCodegen : RegionCodegen("_Setting.value.kt"), DefaultInvoke { + @JvmStatic + fun main(args: Array) = super.startIndependent() + + override val defaultInvokeArgs: List = KtPrimitives + KtString + + override fun StringBuilder.apply(ktType: KtType) { + appendLine( + kCode( + """ + @JvmStatic + val ${ktType.standardName}SerializerDescriptor = ${ktType.standardName}.serializer().descriptor + """ + ).lines().joinToString("\n") { " $it" } + ) + } + } + + object PrimitiveValuesImplCodegen : RegionCodegen("_PrimitiveValueDeclarations.kt"), DefaultInvoke { + @JvmStatic + fun main(args: Array) = super.startIndependent() + + override val defaultInvokeArgs: List + get() = KtPrimitives + KtString + + override fun StringBuilder.apply(ktType: KtType) { + appendKCode( + """ +internal abstract class ${ktType.standardName}ValueImpl : ${ktType.standardName}Value, SerializerAwareValue<${ktType.standardName}>, KSerializer { + constructor() + constructor(default: ${ktType.standardName}) { + _value = default + } + + private var _value: ${ktType.standardName}? = null + + final override var value: ${ktType.standardName} + get() = _value ?: error("${ktType.standardName}Value.value should be initialized before get.") + set(v) { + if (v != this._value) { + this._value = v + onChanged() + } + } + + protected abstract fun onChanged() + + final override val serializer: KSerializer get() = this + final override val descriptor: SerialDescriptor get() = BuiltInSerializerConstants.${ktType.standardName}SerializerDescriptor + final override fun serialize(encoder: Encoder, value: Unit) = ${ktType.standardName}.serializer().serialize(encoder, this.value) + final override fun deserialize(decoder: Decoder) { + value = ${ktType.standardName}.serializer().deserialize(decoder) + } +} + """ + ) + } + + } + + object Setting_value_PrimitivesImplCodegen : RegionCodegen("_Setting.value.kt"), DefaultInvoke { + @JvmStatic + fun main(args: Array) = super.startIndependent() + + override val defaultInvokeArgs: List + get() = KtPrimitives + KtString + + override fun StringBuilder.apply(ktType: KtType) { + appendKCode( + """ +internal fun Setting.valueImpl(default: ${ktType.standardName}): SerializerAwareValue<${ktType.standardName}> { + return object : ${ktType.standardName}ValueImpl(default) { + override fun onChanged() = this@valueImpl.onValueChanged(this) + } +} + """ + ) + } + } + + object Setting_valueImplPrimitiveCodegen : RegionCodegen("_Setting.value.kt"), DefaultInvoke { + @JvmStatic + fun main(args: Array) = super.startIndependent() + + override val defaultInvokeArgs: List + get() = KtPrimitives + KtString + + override fun StringBuilder.apply(ktType: KtType) { + appendKCode( + """ + ${ktType.standardName}::class -> valueImpl(default as ${ktType.standardName}) + """.trimIndent() + ) + } + } + + +} \ No newline at end of file diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/_Setting.value.kt codegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/_Setting.value.kt codegen.kt deleted file mode 100644 index 21398fb9a..000000000 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/_Setting.value.kt codegen.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("ClassName") - -package net.mamoe.mirai.console.codegen - -object _Setting_value_ktCodegen { - object BuiltInSerializerConstantsPrimitivesCodegen : RegionCodegen("BuiltInSerializerConstants primitives"), - DefaultInvoke { - override val defaultInvokeArgs: List = KtPrimitives + KtString - - override fun StringBuilder.apply(ktType: KtType) { - appendLine( - kCode( - """ - @JvmStatic - val ${ktType.standardName}SerializerDescriptor = ${ktType.standardName}.serializer().descriptor - """ - ).lines().joinToString("\n") { " $it" } - ) - } - } - - @JvmStatic - fun main(args: Array) { - codegen("_Setting.value.kt") { - BuiltInSerializerConstantsPrimitivesCodegen() - } - } -} \ No newline at end of file