mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Pretty codegen
This commit is contained in:
parent
47e6328427
commit
70b9b012e1
@ -22,4 +22,5 @@ kotlin {
|
||||
|
||||
dependencies {
|
||||
api(kotlin("stdlib-jdk8"))
|
||||
implementation(kotlin("reflect"))
|
||||
}
|
@ -56,13 +56,14 @@ class CodegenScope : MutableList<Replacer> 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()
|
||||
|
@ -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<String>) {
|
||||
ValueSettingCodegen::class.nestedClasses
|
||||
.filter { it.isSubclassOf(RegionCodegen::class) }
|
||||
.associateWith { kClass -> kClass.functions.find { it.name == "main" && it.hasAnnotation<JvmStatic>() } }
|
||||
.filter { it.value != null }
|
||||
.forEach { (kClass, entryPoint) ->
|
||||
println("---------------------------------------------")
|
||||
println("Running Codegen: ${kClass.simpleName}")
|
||||
entryPoint!!.call(kClass.objectInstance, arrayOf<String>())
|
||||
println("---------------------------------------------")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user