Skip to content

Commit

Permalink
避免包名变更方案
Browse files Browse the repository at this point in the history
  • Loading branch information
zerofancy committed Aug 13, 2022
1 parent 74996c0 commit d957445
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("kapt") version "1.7.10"
`maven-publish`
}

group = "top.ntutn.starsea"
version = "1.1.3"
version = "1.2.0"

repositories {
mavenCentral()
}

dependencies {
val autoService = "1.0-rc7"
compileOnly("com.google.auto.service", "auto-service-annotations", autoService)
kapt("com.google.auto.service", "auto-service", autoService)
testImplementation(kotlin("test"))
}

Expand Down
47 changes: 47 additions & 0 deletions src/main/kotlin/top/ntutn/starseasdk/proxy/AliasProcessor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package top.ntutn.starseasdk.proxy

import com.google.auto.service.AutoService
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment
import javax.annotation.processing.SupportedAnnotationTypes
import javax.lang.model.element.TypeElement
import javax.tools.FileObject
import javax.tools.StandardLocation

@AutoService(Processor::class)
@SupportedAnnotationTypes("top.ntutn.starseasdk.proxy.NewestApi")
class AliasProcessor : AbstractProcessor() {

override fun process(set: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {
if (set.isNullOrEmpty() || roundEnvironment == null) {
return false
}
val outputPackageName = processingEnv.options["aliasprocessor.package"]!!
val suffix = processingEnv.options["aliasprocessor.suffix"]?:""
var outputContent = """
package $outputPackageName
/**
* generated by AliasProcessor
* 这个类是对tg机器人框架starsea提供的最新版本API的alias
* 用于固定包名,避免升级版本号后有一大片包名要改
* have a nice day.
*/
""".trimIndent()
roundEnvironment.getElementsAnnotatedWith(NewestApi::class.java).forEach {
it as TypeElement
outputContent += "\ntypealias ${it.simpleName}${suffix} = ${it.qualifiedName}\n"
}
val filerSourceFile: FileObject = processingEnv.filer.createResource(
StandardLocation.SOURCE_OUTPUT,
outputPackageName, "CompatAlias.kt"
)
with(filerSourceFile.openOutputStream()) {
write(outputContent.toByteArray())
flush()
close()
}
return true
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/top/ntutn/starseasdk/proxy/NewestApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package top.ntutn.starseasdk.proxy

/**
* 标记当前类是最新API版本的类,用于自动生成typealias
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class NewestApi
3 changes: 3 additions & 0 deletions src/main/kotlin/top/ntutn/starseasdk/v2/BotContentProvider.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package top.ntutn.starseasdk.v2

import top.ntutn.starseasdk.proxy.NewestApi

/**
* 机器人功能接口,需要插件来实现
*/
@NewestApi
interface BotContentProvider {
val pluginName: String

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/top/ntutn/starseasdk/v2/BotContext.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package top.ntutn.starseasdk.v2

import top.ntutn.starseasdk.proxy.NewestApi
import java.util.ServiceLoader

/**
* 用于访问机器人的全局上下文。
*/
@NewestApi
object BotContext: IBotContext by ServiceLoader.load(IBotContext::class.java).first()
7 changes: 6 additions & 1 deletion src/main/kotlin/top/ntutn/starseasdk/v2/ChatContext.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package top.ntutn.starseasdk.v2

import top.ntutn.starseasdk.proxy.NewestApi
import java.io.File

/**
* 机器人全局上下文。在插件加载后随时可用。用于主动行为,如主动向用户发送消息。
*/
@NewestApi
interface IBotContext {
/**
* 向其他用户发送文本内容
Expand All @@ -30,7 +32,7 @@ interface IBotContext {
fun sendLocalPhoto(chatId: String, photoFile: File)
}


@NewestApi
interface IChatContext {
/**
* 发送方chat_id
Expand Down Expand Up @@ -65,6 +67,7 @@ interface IChatContext {
/**
* 文本消息上下文
*/
@NewestApi
interface ITextChatContext: IChatContext {
/**
* 收到的文本内容
Expand All @@ -86,6 +89,7 @@ interface ITextChatContext: IChatContext {
/**
* 一个图片文件尺寸资源
*/
@NewestApi
interface IPhotoSize {
val fileId: String
val width: Int
Expand All @@ -95,6 +99,7 @@ interface IPhotoSize {
/**
* 图片上下文
*/
@NewestApi
interface IPhotoChatContext: IChatContext {
/**
* 文本内容,如果有
Expand Down

0 comments on commit d957445

Please sign in to comment.