Skip to content

Commit 4adc941

Browse files
committed
feat: 新增一个新的组 love.forte.simbot.processor, 以及其中一个新的用于组件开发的ksp处理器 simbot-processor-message-element-polymorphic-include
1 parent 176d3b6 commit 4adc941

File tree

13 files changed

+430
-15
lines changed

13 files changed

+430
-15
lines changed

buildSrc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ val kotlinVersion: String = libs.versions.kotlin.get()
3737
dependencies {
3838
implementation(kotlin("gradle-plugin", kotlinVersion))
3939
implementation(kotlin("serialization", kotlinVersion))
40+
implementation(kotlin("power-assert", kotlinVersion))
4041
implementation(libs.bundles.dokka)
4142

4243
// see https://github.com/gradle-nexus/publish-plugin

buildSrc/src/main/kotlin/P.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ sealed class P(override val group: String) : ProjectDetail() {
6464
const val GROUP_QUANTCAT = "love.forte.simbot.quantcat"
6565
const val GROUP_EXTENSION = "love.forte.simbot.extension"
6666
const val GROUP_BENCHMARK = "love.forte.simbot.benchmark"
67+
const val GROUP_PROCESSOR = "love.forte.simbot.processor"
6768

6869
// const val COMPONENT_GROUP = "love.forte.simbot.component"
6970
const val DESCRIPTION = "Simple Robot,一个通用的bot风格事件调度框架,以灵活的统一标准来编写bot应用。"

settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ include(":internal-processors:interface-uml-processor")
4242
// gradle
4343
include(":simbot-gradles:simbot-gradle-suspendtransforms")
4444

45+
// processors
46+
include("simbot-processors:simbot-processor-message-element-polymorphic-include")
47+
4548
include(":simbot-commons:simbot-common-annotations")
4649
include(":simbot-commons:simbot-common-collection")
4750
include(":simbot-commons:simbot-common-streamable")

simbot-api/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*
2222
*/
2323

24+
import com.google.devtools.ksp.gradle.KspTaskMetadata
2425
import love.forte.gradle.common.kotlin.multiplatform.applyTier1
2526
import love.forte.gradle.common.kotlin.multiplatform.applyTier2
2627
import love.forte.gradle.common.kotlin.multiplatform.applyTier3
@@ -136,6 +137,7 @@ kotlin {
136137
dependencies {
137138
// add("kspJvm", libs.suspend.reversal.processor)
138139
add("kspJvm", project(":internal-processors:interface-uml-processor"))
140+
add("kspCommonMainMetadata", project(":simbot-processors:simbot-processor-message-element-polymorphic-include"))
139141
}
140142

141143
ksp {
@@ -145,6 +147,19 @@ ksp {
145147
// arg("simbot.internal.processor.uml.target", "love.forte.simbot.definition.Actor")
146148
arg("simbot.internal.processor.uml.output", rootDir.resolve("generated-docs/event-uml.md").absolutePath)
147149
// arg("simbot.internal.processor.uml.output", rootDir.resolve("generated-docs/actor-uml.md").absolutePath)
150+
151+
// simbot-processor-message-element-polymorphic-include
152+
arg("simbot.processor.message-element-polymorphic-include.localOnly", "true")
153+
arg("simbot.processor.message-element-polymorphic-include.outputPackage", "love.forte.simbot.message")
154+
arg("simbot.processor.message-element-polymorphic-include.visibility", "internal")
155+
}
156+
157+
kotlin.sourceSets.commonMain {
158+
// solves all implicit dependency trouble and IDEs source code detection
159+
// see https://github.com/google/ksp/issues/963#issuecomment-1894144639
160+
tasks.withType<KspTaskMetadata> {
161+
kotlin.srcDir(destinationDirectory.file("kotlin"))
162+
}
148163
}
149164

150165
// BuildConfig for the current version

simbot-api/src/commonMain/kotlin/love/forte/simbot/message/Messages.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import kotlinx.serialization.json.Json
3737
import kotlinx.serialization.modules.PolymorphicModuleBuilder
3838
import kotlinx.serialization.modules.SerializersModule
3939
import kotlinx.serialization.modules.polymorphic
40-
import kotlinx.serialization.modules.subclass
4140
import love.forte.simbot.message.MessagesBuilder.Companion.create
4241
import kotlin.jvm.JvmMultifileClass
4342
import kotlin.jvm.JvmName
@@ -95,19 +94,7 @@ public sealed interface Messages : Message, Iterable<Message.Element> {
9594
@get:JvmName("standardSerializersModule")
9695
public val standardSerializersModule: SerializersModule = SerializersModule {
9796
polymorphic(Message.Element::class) {
98-
subclass(Text.serializer())
99-
subclass(At.serializer())
100-
subclass(AtAll.serializer())
101-
// images
102-
subclass(OfflineByteArrayImage.serializer())
103-
subclass(SimpleOfflineResourceImage.serializer())
104-
subclass(RemoteIDImage.serializer())
105-
106-
subclass(Emoji.serializer())
107-
subclass(Face.serializer())
108-
subclass(MessageIdReference.serializer())
109-
110-
97+
includeMessageElementPolymorphic()
11198
resolvePlatformStandardSerializers()
11299
}
113100
}

simbot-processors/build.gradle.kts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
subprojects {
25+
group = P.GROUP_PROCESSOR
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Message element polymorphic include
2+
3+
为所有 `Message.Element` (或某个指定子类) 生成一个注册多态序列化信息的聚合函数的 KSP 处理器,
4+
常用于组件开发等场景。
5+
6+
```kotlin
7+
// 生成类似于如下的API
8+
internal fun PolymorphicModuleBuilder<Message.Element>.includeElements() {
9+
subclass(...)
10+
subclass(...)
11+
subclass(...)
12+
}
13+
14+
```
15+
16+
会寻找指定的类型的所有**非抽象****非接口**、且标记了 `kotlinx.serialization.Serializable` 注解的子类。
17+
18+
可选配置项:
19+
20+
```kotlin
21+
// 选项参数以 `simbot.processor.message-element-polymorphic-include` 开头
22+
ksp {
23+
// 是否启用
24+
arg("simbot.processor.message-element-polymorphic-include.enable", "true")
25+
arg("simbot.processor.message-element-polymorphic-include.localOnly", "false")
26+
arg("simbot.processor.message-element-polymorphic-include.baseClass", "love.forte.simbot.message.Message.Element")
27+
arg("simbot.processor.message-element-polymorphic-include.visibility", "internal") // 默认 internal 可选: public, internal
28+
arg("simbot.processor.message-element-polymorphic-include.generateFunName", "includeMessageElementPolymorphic")
29+
arg("simbot.processor.message-element-polymorphic-include.outputPackage", "love.forte.simbot.message") // 默认为 null,如果不提供(不是空字符串哦)则与 baseClass 同包
30+
arg("simbot.processor.message-element-polymorphic-include.outputFileName", "MessageElementPolymorphicInclude.generated")
31+
arg("simbot.processor.message-element-polymorphic-include.outputFileJvmName", "") // 默认 null
32+
arg("simbot.processor.message-element-polymorphic-include.outputFileJvmMultifile", "false") // 默认 false
33+
}
34+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
plugins {
25+
kotlin("jvm")
26+
id("simbot.dokka-module-configuration")
27+
kotlin("plugin.serialization")
28+
}
29+
30+
configJavaCompileWithModule()
31+
apply(plugin = "simbot-jvm-maven-publish")
32+
33+
kotlin {
34+
explicitApi()
35+
configKotlinJvm(JVMConstants.KT_JVM_TARGET_VALUE)
36+
}
37+
38+
dependencies {
39+
implementation(libs.ksp)
40+
implementation(libs.kotlinPoet.ksp)
41+
implementation(libs.kotlinx.serialization.core)
42+
implementation(libs.kotlinx.serialization.properties)
43+
}
44+
45+
tasks.getByName<Test>("test") {
46+
useJUnitPlatform()
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.processor.message.element.polymorphic.include
25+
26+
import com.squareup.kotlinpoet.KModifier
27+
import kotlinx.serialization.Serializable
28+
29+
30+
/**
31+
* configurations for [MessageElementPolymorphicIncludeProcessor].
32+
*
33+
* @author ForteScarlet
34+
*/
35+
@Serializable
36+
public open class MessageElementPolymorphicIncludeConfiguration {
37+
public open var baseClass: String = "love.forte.simbot.message.Message.Element"
38+
public open var enable: Boolean = true
39+
public open var localOnly: Boolean = false
40+
public open var visibility: String = "internal"
41+
public open var generateFunName: String = "includeMessageElementPolymorphic"
42+
public open var outputPackage: String? = null
43+
public open var outputFileName: String = "MessageElementPolymorphicInclude.generated"
44+
public open var outputFileJvmName: String? = null
45+
public open var outputFileJvmMultifile: Boolean = false
46+
47+
public open fun visibilityValue(): KModifier {
48+
return when {
49+
visibility.equals("internal", ignoreCase = true) -> KModifier.INTERNAL
50+
visibility.equals("public", ignoreCase = true) -> KModifier.PUBLIC
51+
else -> throw IllegalArgumentException("Unknown visibility: $visibility, not in ['internal', 'public']")
52+
}
53+
}
54+
55+
public companion object {
56+
public const val CONFIG_PREFIX: String = "simbot.processor.message-element-polymorphic-include"
57+
58+
}
59+
}
60+

0 commit comments

Comments
 (0)