Skip to content

Commit d53cc7c

Browse files
committed
```
refactor: Improve component group processing and add source dependencies This commit refactors the component group processing logic in `ComponentProcessor` and `SampleCodeProcessor`, and adds source dependencies to the generated files. - **Component Group Processing:** - Simplified the logic for organizing component functions into groups. - Use `getOrPut` to manage component functions. - If component package not mapped, then the default group is "/". - **Dependencies:** - Added `componentPackageProperty` and `componentScreenFile` to store component files. - Added source dependencies to the generated files using `Dependencies`. - **Sample Code Processing:** - Use `getOrPut` to manage sample code functions. ```
1 parent 5f602d5 commit d53cc7c

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

gallery-processor/src/jvmMain/kotlin/io/github/composefluent/gallery/processor/ComponentProcessor.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.google.devtools.ksp.processing.Dependencies
55
import com.google.devtools.ksp.processing.KSPLogger
66
import com.google.devtools.ksp.impl.symbol.kotlin.KSPropertyDeclarationImpl
77
import com.google.devtools.ksp.symbol.KSAnnotation
8+
import com.google.devtools.ksp.symbol.KSFile
89
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
910
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
1011
import com.google.devtools.ksp.symbol.KSValueArgument
@@ -33,6 +34,8 @@ class ComponentProcessor(private val logger: KSPLogger, private val codeGenerato
3334
private val componentFunctions = mutableMapOf<String, MutableList<Pair<KSAnnotation, KSFunctionDeclaration>>>()
3435
private val componentGroups = mutableMapOf<String, Pair<KSAnnotation, KSPropertyDeclarationImpl>>()
3536
private val componentPackageMap = mutableMapOf<String, String>()
37+
private val componentPackageProperty = mutableSetOf<KSFile>()
38+
private val componentScreenFile = mutableSetOf<KSFile>()
3639

3740
private val componentPackage = "io.github.jpy.wangposefluent.gallery.component"
3841
private val componentItemClass = ClassName(componentPackage, "ComponentItem")
@@ -52,19 +55,12 @@ class ComponentProcessor(private val logger: KSPLogger, private val codeGenerato
5255
private fun arrangeComponentGroup() {
5356
val mapPackage = componentFunctions.remove("/_Auto") ?: emptyList()
5457
mapPackage.forEach { pair ->
55-
val group = componentPackageMap[pair.second.packageName.asString()]
56-
if (!group.isNullOrEmpty()) {
57-
val list =
58-
componentFunctions[group] ?: mutableListOf<Pair<KSAnnotation, KSFunctionDeclaration>>().apply {
59-
componentFunctions[group] = this
60-
}
61-
list.add(pair)
62-
} else {
63-
val list = componentFunctions["/"] ?: mutableListOf<Pair<KSAnnotation, KSFunctionDeclaration>>().apply {
64-
componentFunctions["/"] = this
65-
}
66-
list.add(pair)
58+
val group = componentPackageMap[pair.second.packageName.asString()].let {
59+
if (it.isNullOrEmpty()) "/" else it
6760
}
61+
componentFunctions
62+
.getOrPut(group) { mutableListOf() }
63+
.add(pair)
6864
}
6965
}
7066

@@ -79,11 +75,10 @@ class ComponentProcessor(private val logger: KSPLogger, private val codeGenerato
7975
}
8076
}
8177
val group = (groupArg?.value as? String)?.prefixIfNot("/") ?: return@forEach
82-
val list =
83-
componentFunctions[group] ?: mutableListOf<Pair<KSAnnotation, KSFunctionDeclaration>>().apply {
84-
componentFunctions[group] = this
85-
}
86-
list.add(annotation to function)
78+
componentFunctions
79+
.getOrPut(group) { mutableListOf() }
80+
.add(annotation to function)
81+
componentScreenFile.add(function.containingFile ?: return)
8782
return
8883
}
8984
}
@@ -99,6 +94,7 @@ class ComponentProcessor(private val logger: KSPLogger, private val codeGenerato
9994
if (psi is KtProperty) {
10095
val groupName = psi.initializer?.text?.removePrefix("\"")?.removeSuffix("\"")?.prefixIfNot("/")
10196
?: return
97+
property.containingFile?.let { componentPackageProperty.add(it) }
10298
componentGroups[groupName] = annotation to property
10399
val packageNameValue =
104100
annotation.arguments.firstOrNull { it.name?.asString() == "packageMap" }?.value as? String
@@ -200,15 +196,15 @@ class ComponentProcessor(private val logger: KSPLogger, private val codeGenerato
200196
.build()
201197
)
202198
val file = codeGenerator.createNewFile(
203-
Dependencies(true),
199+
Dependencies(aggregating = true, sources = componentPackageProperty.toTypedArray()),
204200
fileSpecBuilder.packageName,
205201
fileSpecBuilder.name
206202
)
207203
val pathFileSpec = FileSpec.builder(
208204
componentPackage, "ComponentPagePath"
209205
).addType(componentPagePathType.build()).build()
210206
val pathFile = codeGenerator.createNewFile(
211-
Dependencies(true),
207+
Dependencies(true, sources = componentScreenFile.toTypedArray()),
212208
componentPackage,
213209
pathFileSpec.name
214210
)

gallery-processor/src/jvmMain/kotlin/io/github/composefluent/gallery/processor/SampleCodeProcessor.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ class SampleCodeProcessor(private val logger: KSPLogger, private val codeGenerat
2424
super.onFunctionVisit(function)
2525
function.annotations.forEach {
2626
if (it.isTargetAnnotation(sampleAnnotation)) {
27-
val list = sampleCodeFunctions[function.packageName.asString()]
28-
?: mutableListOf<KSFunctionDeclaration>().apply {
29-
sampleCodeFunctions[function.packageName.asString()] = this
30-
}
31-
list.add(function)
27+
sampleCodeFunctions
28+
.getOrPut(function.packageName.asString()) { mutableListOf() }
29+
.add(function)
3230
}
3331
return
3432
}

0 commit comments

Comments
 (0)