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 {
|
dependencies {
|
||||||
api(kotlin("stdlib-jdk8"))
|
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 ->
|
content.replace(Regex("""//// region $regionName CODEGEN ////([\s\S]*?)( *)//// endregion $regionName CODEGEN ////""")) { result ->
|
||||||
val indent = result.groups[2]!!.value
|
val indent = result.groups[2]!!.value
|
||||||
val indentedCode = CodegenScope()
|
val indentedCode = CodegenScope()
|
||||||
.apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) }
|
.apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) } // add codegen task
|
||||||
.applyTo("")
|
.applyTo("") // perform codegen
|
||||||
.mapLine { "${indent}$it" }
|
.lines().dropLastWhile(String::isBlank).joinToString("\n") // remove blank following lines
|
||||||
|
.mapLine { "${indent}$it" } // indent
|
||||||
"""
|
"""
|
||||||
|//// region $regionName CODEGEN ////
|
|//// region $regionName CODEGEN ////
|
||||||
|
|
|
|
||||||
|$indentedCode
|
|${indentedCode}
|
||||||
|
|
|
|
||||||
|${indent}//// endregion $regionName CODEGEN ////
|
|${indent}//// endregion $regionName CODEGEN ////
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
|
@ -11,6 +11,10 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.console.codegen
|
package net.mamoe.mirai.console.codegen
|
||||||
|
|
||||||
|
import kotlin.reflect.full.functions
|
||||||
|
import kotlin.reflect.full.hasAnnotation
|
||||||
|
import kotlin.reflect.full.isSubclassOf
|
||||||
|
|
||||||
object ValueSettingCodegen {
|
object ValueSettingCodegen {
|
||||||
/**
|
/**
|
||||||
* The interface
|
* The interface
|
||||||
@ -109,6 +113,11 @@ internal fun Setting.valueImpl(default: ${ktType.standardName}): SerializerAware
|
|||||||
return object : ${ktType.standardName}ValueImpl(default) {
|
return object : ${ktType.standardName}ValueImpl(default) {
|
||||||
override fun onChanged() = this@valueImpl.onValueChanged(this)
|
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) {
|
override fun StringBuilder.apply(ktType: KtType) {
|
||||||
appendKCode(
|
appendKCode(
|
||||||
"""
|
"""
|
||||||
${ktType.standardName}::class -> valueImpl(default as ${ktType.standardName})
|
${ktType.standardName}::class -> ${ktType.lowerCaseName}ValueImpl()
|
||||||
""".trimIndent()
|
""".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