Skip to content

Commit 0aba3c2

Browse files
committed
增加 Document API 和 LinkPreviewOptions 的支持;内部的Resolver处理优化
1 parent f7ad14a commit 0aba3c2

File tree

15 files changed

+783
-21
lines changed

15 files changed

+783
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Component Events processor
2+
3+
用于在核心组件模块中生成所有基础的事件类型定义的接口。
4+
5+
会根据 `Update` 中的所有类型,定义所有如下类型的接口:
6+
7+
```kotlin
8+
9+
interface TelegramXxxEvent : TelegramEvent {
10+
override val sourceContent: Xxx // 实际上的事件类型
11+
}
12+
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-telegram.
5+
*
6+
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
19+
20+
plugins {
21+
kotlin("jvm")
22+
}
23+
24+
repositories {
25+
mavenCentral()
26+
}
27+
28+
kotlin {
29+
jvmToolchain(11)
30+
compilerOptions {
31+
javaParameters = true
32+
jvmTarget.set(JvmTarget.JVM_11)
33+
}
34+
}
35+
36+
configJavaCompileWithModule()
37+
38+
dependencies {
39+
// implementation(project(":annotations"))
40+
api(libs.ksp)
41+
api(libs.kotlinPoet.ksp)
42+
testImplementation(kotlin("test-junit5"))
43+
}
44+
45+
tasks.getByName<Test>("test") {
46+
useJUnitPlatform()
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-telegram.
5+
*
6+
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package telegram.internal.processors.sendingresolvers
19+
20+
import com.google.devtools.ksp.getClassDeclarationByName
21+
import com.google.devtools.ksp.processing.Resolver
22+
import com.google.devtools.ksp.processing.SymbolProcessor
23+
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
24+
import com.google.devtools.ksp.processing.SymbolProcessorProvider
25+
import com.google.devtools.ksp.symbol.ClassKind
26+
import com.google.devtools.ksp.symbol.KSAnnotated
27+
import com.google.devtools.ksp.symbol.KSClassDeclaration
28+
import com.squareup.kotlinpoet.*
29+
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
30+
import com.squareup.kotlinpoet.ksp.toClassName
31+
import com.squareup.kotlinpoet.ksp.toTypeName
32+
import com.squareup.kotlinpoet.ksp.writeTo
33+
import java.util.concurrent.atomic.AtomicBoolean
34+
35+
/**
36+
* 把所有 `love.forte.simbot.component.telegram.core.message.SendingMessageResolver`
37+
* 的 object 实例整合到
38+
* `love.forte.simbot.component.telegram.core.message.allSendingResolvers` 列表中。
39+
*
40+
*
41+
*/
42+
class SendingResolversProcessorProvider : SymbolProcessorProvider {
43+
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor =
44+
ComponentEventProcessor(environment)
45+
}
46+
47+
48+
private const val OUTPUT_PACKAGE = "love.forte.simbot.component.telegram.core.message"
49+
private const val RESOLVER_CLASS_NAME = "$OUTPUT_PACKAGE.SendingMessageResolver"
50+
51+
private const val GENERATED_EVENTS_FILE_NAME = "AllSendingMessageResolvers.generated"
52+
53+
private const val PROPERTY_NAME = "allSendingResolvers"
54+
55+
private class ComponentEventProcessor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor {
56+
private val generated = AtomicBoolean(false)
57+
58+
override fun process(resolver: Resolver): List<KSAnnotated> {
59+
if (!generated.compareAndSet(false, true)) {
60+
environment.logger.warn("Processor was used.")
61+
return emptyList()
62+
}
63+
64+
val resolverClass = resolver.getClassDeclarationByName(RESOLVER_CLASS_NAME)
65+
?: throw NoSuchElementException("Class: $RESOLVER_CLASS_NAME")
66+
67+
val resolverType = resolverClass.asStarProjectedType()
68+
69+
val implements = resolver.getAllFiles().flatMap { f ->
70+
f.declarations
71+
}.filterIsInstance<KSClassDeclaration>()
72+
.filter { it.classKind == ClassKind.OBJECT }
73+
.filter {
74+
resolverType.isAssignableFrom(it.asStarProjectedType())
75+
}
76+
.toList()
77+
78+
val property = PropertySpec.builder(
79+
PROPERTY_NAME,
80+
LIST.parameterizedBy(resolverType.toTypeName()),
81+
KModifier.INTERNAL
82+
).apply {
83+
this.initializer(
84+
buildCodeBlock {
85+
add("listOf(")
86+
for ((index, impl) in implements.withIndex()) {
87+
add("%T", impl.toClassName())
88+
if (index != implements.lastIndex) add(", ")
89+
}
90+
add(")")
91+
}
92+
)
93+
}.build()
94+
95+
val file = FileSpec.builder(packageName = OUTPUT_PACKAGE, fileName = GENERATED_EVENTS_FILE_NAME).apply {
96+
addFileComment(
97+
"""
98+
****************************
99+
此文件内容是 **自动生成** 的
100+
****************************
101+
""".trimIndent()
102+
)
103+
addAnnotation(
104+
AnnotationSpec.builder(Suppress::class)
105+
.addMember("%S, %S", "ALL", "unused")
106+
.build()
107+
)
108+
addProperty(property)
109+
indent(" ")
110+
}.build()
111+
112+
file.writeTo(
113+
codeGenerator = environment.codeGenerator,
114+
aggregating = true,
115+
originatingKSFiles = buildList {
116+
resolverClass.containingFile?.also { add(it) }
117+
implements.forEach { impl ->
118+
impl.containingFile?.also { add(it) }
119+
}
120+
}
121+
)
122+
123+
return emptyList()
124+
}
125+
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
telegram.internal.processors.sendingresolvers.SendingResolversProcessorProvider

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ include(":internal-processors:update-events-processor")
2121
include(":internal-processors:stdlib-processor-extensions-processor")
2222
include(":internal-processors:component-events-processor")
2323
include(":internal-processors:include-component-message-elements-processor")
24+
include(":internal-processors:component-sending-resolvers-processor")
2425

2526
include(":simbot-component-telegram-type")
2627
include(":simbot-component-telegram-api")

0 commit comments

Comments
 (0)