-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
Quick-annotations/src/main/java/com/wpf/app/quick/annotations/request/GenerateRequest.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,8 @@ | ||
package com.wpf.app.quick.annotations.request | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class GenerateRequest( | ||
val fileName: String, | ||
val funName: String = "request" | ||
) |
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
67 changes: 67 additions & 0 deletions
67
Quick-compiler-ksp/src/main/java/com/wpf/plugins/processor/GenerateRequestProcessor.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,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() | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.