-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from simple-robot/dev/document-api
增加 Document API 和 LinkPreviewOptions 的支持;内部的Resolver处理优化
- Loading branch information
Showing
22 changed files
with
809 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
kotlin.code.style=official | ||
kotlin.js.generate.executable.default=false | ||
kotlin.native.ignoreDisabledTargets=true | ||
org.gradle.jvmargs='-Dfile.encoding=UTF-8' |
13 changes: 13 additions & 0 deletions
13
internal-processors/component-sending-resolvers-processor/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Component Events processor | ||
|
||
用于在核心组件模块中生成所有基础的事件类型定义的接口。 | ||
|
||
会根据 `Update` 中的所有类型,定义所有如下类型的接口: | ||
|
||
```kotlin | ||
|
||
interface TelegramXxxEvent : TelegramEvent { | ||
override val sourceContent: Xxx // 实际上的事件类型 | ||
} | ||
|
||
``` |
48 changes: 48 additions & 0 deletions
48
internal-processors/component-sending-resolvers-processor/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-telegram. | ||
* | ||
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(11) | ||
compilerOptions { | ||
javaParameters = true | ||
jvmTarget.set(JvmTarget.JVM_11) | ||
} | ||
} | ||
|
||
configJavaCompileWithModule() | ||
|
||
dependencies { | ||
// implementation(project(":annotations")) | ||
api(libs.ksp) | ||
api(libs.kotlinPoet.ksp) | ||
testImplementation(kotlin("test-junit5")) | ||
} | ||
|
||
tasks.getByName<Test>("test") { | ||
useJUnitPlatform() | ||
} | ||
|
126 changes: 126 additions & 0 deletions
126
...kotlin/telegram/internal/processors/sendingresolvers/SendingResolversProcessorProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-telegram. | ||
* | ||
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package telegram.internal.processors.sendingresolvers | ||
|
||
import com.google.devtools.ksp.getClassDeclarationByName | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.squareup.kotlinpoet.* | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
/** | ||
* 把所有 `love.forte.simbot.component.telegram.core.message.SendingMessageResolver` | ||
* 的 object 实例整合到 | ||
* `love.forte.simbot.component.telegram.core.message.allSendingResolvers` 列表中。 | ||
* | ||
* | ||
*/ | ||
class SendingResolversProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
ComponentEventProcessor(environment) | ||
} | ||
|
||
|
||
private const val OUTPUT_PACKAGE = "love.forte.simbot.component.telegram.core.message" | ||
private const val RESOLVER_CLASS_NAME = "$OUTPUT_PACKAGE.SendingMessageResolver" | ||
|
||
private const val GENERATED_EVENTS_FILE_NAME = "AllSendingMessageResolvers.generated" | ||
|
||
private const val PROPERTY_NAME = "allSendingResolvers" | ||
|
||
private class ComponentEventProcessor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor { | ||
private val generated = AtomicBoolean(false) | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
if (!generated.compareAndSet(false, true)) { | ||
environment.logger.warn("Processor was used.") | ||
return emptyList() | ||
} | ||
|
||
val resolverClass = resolver.getClassDeclarationByName(RESOLVER_CLASS_NAME) | ||
?: throw NoSuchElementException("Class: $RESOLVER_CLASS_NAME") | ||
|
||
val resolverType = resolverClass.asStarProjectedType() | ||
|
||
val implements = resolver.getAllFiles().flatMap { f -> | ||
f.declarations | ||
}.filterIsInstance<KSClassDeclaration>() | ||
.filter { it.classKind == ClassKind.OBJECT } | ||
.filter { | ||
resolverType.isAssignableFrom(it.asStarProjectedType()) | ||
} | ||
.toList() | ||
|
||
val property = PropertySpec.builder( | ||
PROPERTY_NAME, | ||
LIST.parameterizedBy(resolverType.toTypeName()), | ||
KModifier.INTERNAL | ||
).apply { | ||
this.initializer( | ||
buildCodeBlock { | ||
add("listOf(") | ||
for ((index, impl) in implements.withIndex()) { | ||
add("%T", impl.toClassName()) | ||
if (index != implements.lastIndex) add(", ") | ||
} | ||
add(")") | ||
} | ||
) | ||
}.build() | ||
|
||
val file = FileSpec.builder(packageName = OUTPUT_PACKAGE, fileName = GENERATED_EVENTS_FILE_NAME).apply { | ||
addFileComment( | ||
""" | ||
**************************** | ||
此文件内容是 **自动生成** 的 | ||
**************************** | ||
""".trimIndent() | ||
) | ||
addAnnotation( | ||
AnnotationSpec.builder(Suppress::class) | ||
.addMember("%S, %S", "ALL", "unused") | ||
.build() | ||
) | ||
addProperty(property) | ||
indent(" ") | ||
}.build() | ||
|
||
file.writeTo( | ||
codeGenerator = environment.codeGenerator, | ||
aggregating = true, | ||
originatingKSFiles = buildList { | ||
resolverClass.containingFile?.also { add(it) } | ||
implements.forEach { impl -> | ||
impl.containingFile?.also { add(it) } | ||
} | ||
} | ||
) | ||
|
||
return emptyList() | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
telegram.internal.processors.sendingresolvers.SendingResolversProcessorProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.