forked from Konyaco/compose-fluent-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ksp plugin for generating source code string of Gallery sample
- Loading branch information
Showing
10 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
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,22 @@ | ||
import com.konyaco.fluent.plugin.build.BuildConfig | ||
|
||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) | ||
} | ||
|
||
group = BuildConfig.group | ||
version = "gallery-processor" | ||
|
||
kotlin { | ||
jvm() | ||
sourceSets { | ||
val jvmMain by getting { | ||
dependencies { | ||
implementation(libs.squareup.kotlinpoet) | ||
implementation("com.google.devtools.ksp:symbol-processing-api:${libs.versions.ksp.get()}") | ||
implementation("com.google.devtools.ksp:symbol-processing:${libs.versions.ksp.get()}") | ||
implementation(kotlin("compiler")) | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...-processor/src/jvmMain/kotlin/com/konyaco/fluent/gallery/processor/SampleCodeProcessor.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,80 @@ | ||
package com.konyaco.fluent.gallery.processor | ||
|
||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
import com.google.devtools.ksp.symbol.impl.kotlin.KSFunctionDeclarationImpl | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import java.io.OutputStreamWriter | ||
import java.nio.charset.StandardCharsets | ||
|
||
class SampleCodeProcessor(private val logger: KSPLogger, private val codeGenerator: CodeGenerator) : SymbolProcessor { | ||
|
||
private val annotationPackage = "com.konyaco.fluent.gallery.annotation" | ||
private val sampleAnnotation = "Sample" | ||
|
||
private val sampleCodeFunctions = mutableMapOf<String, MutableList<KSFunctionDeclaration>>() | ||
|
||
private val visitor = FindFunctionVisitor { | ||
if (it.annotations.any { annotation -> | ||
annotation.shortName.asString() == sampleAnnotation && | ||
annotation.annotationType.resolve().declaration.packageName.asString() == annotationPackage | ||
} | ||
) { | ||
val list = sampleCodeFunctions[it.packageName.asString()] ?: mutableListOf<KSFunctionDeclaration>().apply { | ||
sampleCodeFunctions[it.packageName.asString()] = this | ||
} | ||
list.add(it) | ||
} | ||
} | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
resolver.getAllFiles().forEach { it.accept(visitor, Unit) } | ||
return emptyList() | ||
} | ||
|
||
override fun finish() { | ||
super.finish() | ||
val fileName = "_SampleCodeString" | ||
sampleCodeFunctions.forEach { (packageName, functions) -> | ||
if (functions.isNotEmpty()) { | ||
val sourceFile = FileSpec.builder(packageName, fileName) | ||
val sourceFileList = mutableListOf<KSFile>() | ||
functions.forEach { func -> | ||
func.containingFile?.let { sourceFileList.add(it) } | ||
if (func is KSFunctionDeclarationImpl) { | ||
val funcName = func.simpleName.asString() | ||
sourceFile.addProperty( | ||
PropertySpec.builder( | ||
"sourceCodeOf${funcName.first().uppercase()}${funcName.substring(1)}", | ||
String::class | ||
) | ||
.addModifiers(KModifier.INTERNAL) | ||
.getter( | ||
FunSpec.getterBuilder() | ||
.addStatement("return %S", func.ktFunction.text) | ||
.build() | ||
) | ||
.build() | ||
) | ||
} | ||
} | ||
val file = codeGenerator.createNewFile( | ||
Dependencies(true, *(sourceFileList).toTypedArray()), | ||
packageName, | ||
fileName | ||
) | ||
OutputStreamWriter(file, StandardCharsets.UTF_8).use(sourceFile.build()::writeTo) | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
class SampleCodeProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { | ||
return SampleCodeProcessor(environment.logger, environment.codeGenerator) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
gallery-processor/src/jvmMain/kotlin/com/konyaco/fluent/gallery/processor/Visitor.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,22 @@ | ||
package com.konyaco.fluent.gallery.processor | ||
|
||
import com.google.devtools.ksp.getDeclaredFunctions | ||
import com.google.devtools.ksp.symbol.* | ||
|
||
internal class FindFunctionVisitor( | ||
private val onNode:(node: KSFunctionDeclaration) -> Unit | ||
) : KSVisitorVoid() { | ||
|
||
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | ||
super.visitClassDeclaration(classDeclaration, data) | ||
classDeclaration.getDeclaredFunctions().forEach { it.accept(this, Unit) } | ||
} | ||
|
||
override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) { | ||
onNode(function) | ||
} | ||
|
||
override fun visitFile(file: KSFile, data: Unit) { | ||
file.declarations.forEach { it.accept(this, Unit) } | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
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 @@ | ||
com.konyaco.fluent.gallery.processor.SampleCodeProcessorProvider |
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
4 changes: 4 additions & 0 deletions
4
gallery/src/commonMain/kotlin/com/konyaco/fluent/gallery/annotation/Sample.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,4 @@ | ||
package com.konyaco.fluent.gallery.annotation | ||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
annotation class Sample() |
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