Skip to content

Commit

Permalink
日常修改:2023-12-12.
Browse files Browse the repository at this point in the history
  • Loading branch information
walgr committed Dec 12, 2023
1 parent 38ea56d commit 2c07909
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wpf.app.quick.annotations.request

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class GenerateRequest(
val fileName: String,
val funName: String = "request"
)
3 changes: 3 additions & 0 deletions Quick-compiler-ksp/src/main/java/com/wpf/plugins/QuickKsp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import com.wpf.app.quick.annotations.getclass.GetClass
import com.wpf.app.quick.annotations.getclass.GetFun
import com.wpf.app.quick.annotations.request.GenerateCommonCall
import com.wpf.app.quick.annotations.request.GenerateNormalCall
import com.wpf.app.quick.annotations.request.GenerateRequest
import com.wpf.plugins.processor.BaseProcessor
import com.wpf.plugins.processor.BindViewProcessor
import com.wpf.plugins.processor.GenerateCommonCallProcessor
import com.wpf.plugins.processor.GenerateNormalCallProcessor
import com.wpf.plugins.processor.GenerateRequestProcessor
import com.wpf.plugins.processor.GetClassProcessor
import com.wpf.plugins.processor.TabInitProcessor
import kotlin.reflect.full.primaryConstructor
Expand All @@ -33,6 +35,7 @@ internal class QuickSymbolProcessor(private val environment: SymbolProcessorEnvi
Pair(arrayOf(TabInit::class), TabInitProcessor::class),
Pair(arrayOf(GenerateNormalCall::class), GenerateNormalCallProcessor::class),
Pair(arrayOf(GenerateCommonCall::class), GenerateCommonCallProcessor::class),
Pair(arrayOf(GenerateRequest::class), GenerateRequestProcessor::class),
)

override fun process(resolver: Resolver): List<KSAnnotated> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wpf.plugins.processor

import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeVariableName
import com.wpf.app.quick.annotations.request.GenerateRequest
import com.wpf.app.quick.annotations.tab.TabInit

class GenerateRequestProcessor(environment: SymbolProcessorEnvironment) :
BaseProcessor(environment) {
private var funBuilder: FunSpec.Builder? = null
override fun visitClassDeclaration(
classDeclaration: KSClassDeclaration,
data: Unit,
packageName: String,
className: String
) {
super.visitClassDeclaration(classDeclaration, data, packageName, className)
val generateRequestAnn = classDeclaration.annotations.find {
it.shortName.getShortName().contains(GenerateRequest::class.simpleName!!)
} ?: return
val fileName = generateRequestAnn.arguments.getOrNull(0)?.value as? String
val funName = generateRequestAnn.arguments.getOrNull(1)?.value as String
outFileName = fileName
val dataType = TypeVariableName("Data")
val failType = TypeVariableName("Fail")
val api = ClassName(packageName, className)
val baseRequest = ClassName("com.wpf.app.quicknetwork.base", "BaseRequest")
val retrofitCreateHelper = ClassName("com.wpf.app.quicknetwork.helper", "RetrofitCreateHelper")
val wpfRequest = ClassName("com.wpf.app.quicknetwork.base", "WpfRequest")
val realCall = ClassName("com.wpf.app.quicknetwork.call", "RealCall")
val requestCoroutineScope =
ClassName("com.wpf.app.quicknetwork.base", "RequestCoroutineScope")
funBuilder = FunSpec.builder(funName)
.addModifiers(KModifier.INLINE)
.addTypeVariable(dataType)
.addTypeVariable(failType)
.addParameter(
ParameterSpec.builder("context", requestCoroutineScope.copy(nullable = true))
.defaultValue("null").build()
).addParameter(
ParameterSpec.builder(
"run",
LambdaTypeName.get(api, listOf(), realCall.parameterizedBy(dataType, failType))
).build()
).returns(
baseRequest.parameterizedBy(dataType, failType)
).addCode(CodeBlock.of("return %T.getService<%T>().run(run).enqueue(%T(context))", retrofitCreateHelper, api, wpfRequest))
}

override fun visitEnd() {
if (outFileName.isNullOrEmpty() || funBuilder == null) return
if (outFileSpec == null) {
outFileSpec = FileSpec.builder(packageName, outFileName!!)
}
outFileSpec?.addFunction(funBuilder!!.build())
super.visitEnd()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class TabInitProcessor(environment: SymbolProcessorEnvironment) : IdProcessor(en
propertyName: String?
) {
super.visitPropertyDeclaration(property, data, packageName, className, propertyName)


val tabInitAnn = property.annotations.find {
it.shortName.getShortName().contains(TabInit::class.simpleName!!)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.wpf.app.quicknetwork.base

/**
* Created by 王朋飞 on 2022/7/22.
*
* 注解使用此类,移动需要修改注解代码
*/
abstract class BaseRequest<SResponse, FResponse> {

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/wpf/app/quick/demo/http/TestApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.wpf.app.quick.demo.http

import com.wpf.app.quick.annotations.getclass.GetClass
import com.wpf.app.quick.annotations.getclass.GetFun
import com.wpf.app.quick.annotations.request.GenerateRequest
import com.wpf.app.quick.demo.wanandroid.model.首页文章
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

@GetClass
@GenerateRequest(fileName = "TestService")
interface TestApi {

@GetFun
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/com/wpf/app/quick/demo/http/TestService.kt

This file was deleted.

0 comments on commit 2c07909

Please sign in to comment.