Skip to content

Commit 34a134b

Browse files
committed
chore: ksp processor support
1 parent ced4404 commit 34a134b

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ graph LR
126126
bom
127127
catalog
128128
end
129+
subgraph ksp
130+
processor
131+
end
129132
web --> common
130133
web --> common
131134
desktop --> common
132135
backend --> common
133136
web --> common
134137
benchmarks --> common
135138
136-
```
139+
```

gradle/build-logic/common-plugins/src/main/kotlin/settings/repos.settings.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ pluginManagement {
2121
}
2222

2323
plugins {
24-
// id("org.jetbrains.compose").version(extra["compose.version"] as String)
24+
// val kspVersion: String by settings
25+
// id("com.google.devtools.ksp") version kspVersion apply false
26+
// kotlin("multiplatform") version(extra["kotlin.version"] as String) apply false
2527
}
2628

2729
repositories {

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
java = "22"
33
kotlin = "1.9.0"
4-
kotlin-ksp = "1.9.0-1.0.11"
4+
kotlin-ksp = "1.9.0-1.0.12"
55
kotlin-jvmtarget = "20"
66
kotlin-dsl-jvmtarget = "17"
77
kotlin-api-version = "1.9"
@@ -176,6 +176,7 @@ kotlinx-reflect-lite = { module = "org.jetbrains.kotlinx:kotlinx
176176
kotlinx-collections-immutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable" , version.ref = "kotlinx-collections-immutable"}
177177
kotlinx-html = { module = "org.jetbrains.kotlinx:kotlinx-html" , version.ref = "kotlinx-html"}
178178
kotlinx-bench-runtime = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime" , version.ref = "kotlinx-benchmark"}
179+
kotlin-ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api" , version.ref = "kotlin-ksp"}
179180

180181
ktor-bom = { module = "io.ktor:ktor-bom" , version.ref = "ktor"}
181182
ktor-serialization = { module = "io.ktor:ktor-serialization" , version.ref = "ktor"}

ksp/processor/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins { plugins.kotlin.mpp }
2+
3+
kotlin.sourceSets.jvmMain {
4+
dependencies {
5+
implementation(libs.kotlin.ksp.api)
6+
// implementation("com.squareup:kotlinpoet-ksp")
7+
}
8+
9+
kotlin.srcDir("src/main/kotlin")
10+
resources.srcDir("src/main/resources")
11+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import com.google.auto.service.AutoService
2+
import com.google.devtools.ksp.processing.*
3+
import com.google.devtools.ksp.symbol.KSAnnotated
4+
import com.google.devtools.ksp.symbol.KSClassDeclaration
5+
import com.google.devtools.ksp.symbol.KSNode
6+
import com.google.devtools.ksp.visitor.KSTopDownVisitor
7+
import java.io.OutputStreamWriter
8+
9+
@AutoService(SymbolProcessorProvider::class)
10+
class TestProcessorProvider : SymbolProcessorProvider {
11+
override fun create(environment: SymbolProcessorEnvironment) =
12+
TestProcessor(environment.codeGenerator, environment.logger)
13+
}
14+
15+
class TestProcessor(val codeGenerator: CodeGenerator, val logger: KSPLogger) : SymbolProcessor {
16+
private var invoked = false
17+
18+
override fun process(resolver: Resolver): List<KSAnnotated> {
19+
val allFiles = resolver.getAllFiles().map { it.fileName }
20+
logger.warn(allFiles.toList().toString())
21+
if (invoked) {
22+
return emptyList()
23+
}
24+
invoked = true
25+
26+
codeGenerator
27+
.createNewFile(
28+
dependencies = Dependencies.ALL_FILES,
29+
packageName = "",
30+
fileName = "Foo",
31+
extensionName = "kt")
32+
.use { output ->
33+
OutputStreamWriter(output).use { writer ->
34+
writer.write("package com.example\n\n")
35+
writer.write("class Foo {\n")
36+
37+
val visitor = ClassVisitor()
38+
resolver.getAllFiles().forEach { it.accept(visitor, writer) }
39+
writer.write("}\n")
40+
}
41+
}
42+
return emptyList()
43+
}
44+
}
45+
46+
class ClassVisitor : KSTopDownVisitor<OutputStreamWriter, Unit>() {
47+
override fun defaultHandler(node: KSNode, data: OutputStreamWriter) {}
48+
49+
override fun visitClassDeclaration(
50+
classDeclaration: KSClassDeclaration,
51+
data: OutputStreamWriter
52+
) {
53+
super.visitClassDeclaration(classDeclaration, data)
54+
val symbolName = classDeclaration.simpleName.asString().lowercase()
55+
data.write(" val $symbolName = true\n")
56+
}
57+
}

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ include(":compose:desktop")
2121
include(":dep-mgmt:bom")
2222

2323
include(":dep-mgmt:catalog")
24+
25+
include(":ksp:processor")

0 commit comments

Comments
 (0)