diff --git a/backend/codegen/build.gradle.kts b/backend/codegen/build.gradle.kts index 78077df4a..3b254c046 100644 --- a/backend/codegen/build.gradle.kts +++ b/backend/codegen/build.gradle.kts @@ -22,4 +22,5 @@ kotlin { dependencies { api(kotlin("stdlib-jdk8")) + implementation(kotlin("reflect")) } \ No newline at end of file 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 5292c566e..9e2cf69a7 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 @@ -56,13 +56,14 @@ class CodegenScope : MutableList by mutableListOf() { content.replace(Regex("""//// region $regionName CODEGEN ////([\s\S]*?)( *)//// endregion $regionName CODEGEN ////""")) { result -> val indent = result.groups[2]!!.value val indentedCode = CodegenScope() - .apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) } - .applyTo("") - .mapLine { "${indent}$it" } + .apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) } // add codegen task + .applyTo("") // perform codegen + .lines().dropLastWhile(String::isBlank).joinToString("\n") // remove blank following lines + .mapLine { "${indent}$it" } // indent """ |//// region $regionName CODEGEN //// | - |$indentedCode + |${indentedCode} | |${indent}//// endregion $regionName CODEGEN //// """.trimMargin() 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 index b8c195462..b1cecf118 100644 --- 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 @@ -11,6 +11,10 @@ package net.mamoe.mirai.console.codegen +import kotlin.reflect.full.functions +import kotlin.reflect.full.hasAnnotation +import kotlin.reflect.full.isSubclassOf + object ValueSettingCodegen { /** * The interface @@ -109,6 +113,11 @@ internal fun Setting.valueImpl(default: ${ktType.standardName}): SerializerAware return object : ${ktType.standardName}ValueImpl(default) { override fun onChanged() = this@valueImpl.onValueChanged(this) } +} +internal fun Setting.${ktType.lowerCaseName}ValueImpl(): SerializerAwareValue<${ktType.standardName}> { + return object : ${ktType.standardName}ValueImpl() { + override fun onChanged() = this@${ktType.lowerCaseName}ValueImpl.onValueChanged(this) + } } """ ) @@ -125,11 +134,27 @@ internal fun Setting.valueImpl(default: ${ktType.standardName}): SerializerAware override fun StringBuilder.apply(ktType: KtType) { appendKCode( """ - ${ktType.standardName}::class -> valueImpl(default as ${ktType.standardName}) + ${ktType.standardName}::class -> ${ktType.lowerCaseName}ValueImpl() """.trimIndent() ) } } - + /** + * 运行本 object 中所有嵌套 object Codegen + */ + @OptIn(ExperimentalStdlibApi::class) + @JvmStatic + fun main(args: Array) { + ValueSettingCodegen::class.nestedClasses + .filter { it.isSubclassOf(RegionCodegen::class) } + .associateWith { kClass -> kClass.functions.find { it.name == "main" && it.hasAnnotation() } } + .filter { it.value != null } + .forEach { (kClass, entryPoint) -> + println("---------------------------------------------") + println("Running Codegen: ${kClass.simpleName}") + entryPoint!!.call(kClass.objectInstance, arrayOf()) + println("---------------------------------------------") + } + } } \ No newline at end of file