Skip to content

Commit

Permalink
Merge pull request #5 from simple-robot/dev/document-api
Browse files Browse the repository at this point in the history
增加 Document API 和 LinkPreviewOptions 的支持;内部的Resolver处理优化
  • Loading branch information
ForteScarlet authored Apr 15, 2024
2 parents 3934dbe + ed7f060 commit a750114
Show file tree
Hide file tree
Showing 22 changed files with 809 additions and 62 deletions.
30 changes: 6 additions & 24 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,10 @@ jobs:
java-version: 21

# setup Gradle
- uses: gradle/actions/setup-gradle@v3

- name: Gradle test and publish snapshot
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.5
arguments: |
allTests
publishToSonatype
closeAndReleaseStagingRepository
--info
--warning-mode all
-Porg.gradle.jvmargs="-Xmx8g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8"
-Porg.gradle.daemon=false
run: gradle allTests publishToSonatype closeAndReleaseStagingRepository --info --warning-mode all "-Porg.gradle.daemon=false" "-Porg.gradle.jvmargs='-Xmx8g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8'"

- name: Upload test reports
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -94,19 +86,9 @@ jobs:
java-version: 21

# setup Gradle
- name: Gradle generate documentation
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.5
arguments: |
-Porg.gradle.jvmargs="-Xmx4g -Xms4g -XX:MaxMetaspaceSize=2g -Dfile.encoding=UTF-8"
-Porg.gradle.daemon=false
-DisSnapshot=false
--info
--warning-mode all
-x test
--build-cache
dokkaHtmlMultiModule
- uses: gradle/actions/setup-gradle@v3

- run: gradle dokkaHtmlMultiModule "-Porg.gradle.jvmargs='-Xmx4g -Xms4g -XX:MaxMetaspaceSize=2g -Dfile.encoding=UTF-8'" "-Porg.gradle.daemon=false" "-DisSnapshot=false" --info --warning-mode all -x test --build-cache

- name: Push to doc repository
uses: peaceiris/actions-gh-pages@v3
Expand Down
25 changes: 11 additions & 14 deletions .github/workflows/test-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ jobs:
java-version: 21
cache: 'gradle'

- name: Run All Tests
- name: Set up gradle
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.5
arguments: |
assemble
build
allTests
--info
--warning-mode all
# --build-cache
# -Porg.gradle.daemon=false
# -Porg.gradle.jvmargs="-Xmx4g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8"
add-job-summary-as-pr-comment: on-failure # Valid values are 'never' (default), 'always', and 'on-failure'

- name: Run All Tests
run: gradle assemble allTests "-Porg.gradle.jvmargs='-Xmx4g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8'" --info --warning-mode all --build-cache

- name: Upload test reports
uses: actions/upload-artifact@v4
Expand All @@ -68,11 +62,14 @@ jobs:
java-version: 21
cache: 'gradle'

- name: Run All Tests
- name: Set up gradle
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.5
arguments: detekt
add-job-summary-as-pr-comment: on-failure # Valid values are 'never' (default), 'always', and 'on-failure'

- name: Run detekt
run: gradle detekt

- name: Upload detekt reports
uses: actions/upload-artifact@v4
if: ${{ always() }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
}

fun org.jetbrains.dokka.gradle.AbstractDokkaTask.configOutput(format: String) {
moduleName.set("Simple Robot 组件 | Discord")
moduleName.set("Simple Robot 组件 | Telegram")
outputDirectory.set(rootProject.file("build/dokka/$format"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ plugins {
tasks.withType<DokkaTaskPartial>().configureEach {
dokkaSourceSets.configureEach {
version = P.ComponentTelegram.version.toString()
documentedVisibilities.set(listOf(DokkaConfiguration.Visibility.PUBLIC, DokkaConfiguration.Visibility.PROTECTED))
documentedVisibilities.set(
listOf(DokkaConfiguration.Visibility.PUBLIC, DokkaConfiguration.Visibility.PROTECTED)
)
fun checkModule(projectFileName: String): Boolean {
val moduleMdFile = project.file(projectFileName)
if (moduleMdFile.exists()) {
Expand Down Expand Up @@ -57,7 +59,7 @@ tasks.withType<DokkaTaskPartial>().configureEach {
sourceLink {
localDirectory.set(projectDir.resolve("src"))
val relativeTo = projectDir.relativeTo(rootProject.projectDir)
remoteUrl.set(URI.create("${P.ComponentTelegram.HOMEPAGE}/tree/main/$relativeTo/src/").toURL())
remoteUrl.set(URI.create("${P.ComponentTelegram.HOMEPAGE}/tree/dev/main/$relativeTo/src/").toURL())
remoteLineSuffix.set("#L")
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
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'
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 // 实际上的事件类型
}

```
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()
}

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()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
telegram.internal.processors.sendingresolvers.SendingResolversProcessorProvider
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include(":internal-processors:update-events-processor")
include(":internal-processors:stdlib-processor-extensions-processor")
include(":internal-processors:component-events-processor")
include(":internal-processors:include-component-message-elements-processor")
include(":internal-processors:component-sending-resolvers-processor")

include(":simbot-component-telegram-type")
include(":simbot-component-telegram-api")
Expand Down
1 change: 1 addition & 0 deletions simbot-component-telegram-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ plugins {
kotlin("plugin.serialization")
alias(libs.plugins.ksp)
`simbot-telegram-dokka-partial-configure`
`simbot-telegram-suspend-transform-configure`
}

setup(P.ComponentTelegram)
Expand Down
Loading

0 comments on commit a750114

Please sign in to comment.