|
| 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 | +} |
0 commit comments