Skip to content

Commit

Permalink
Trying to fix KSP cache declaration.
Browse files Browse the repository at this point in the history
Note that there's no official way to record a generated file in commonTest. Also some local tests shown that those files are updated only when required so it looks promising... (as a workaround until KSP 2.1)
  • Loading branch information
glureau committed Nov 19, 2024
1 parent 466f8ca commit 50a8bc2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ javassist = { module = "org.javassist:javassist", version.ref = "javassist" }
kotlinPoet-ksp = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlinPoet" }
ksp-gradlePlugin = { module = "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
ksp-symbolProcessingApi = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
ksp-symbolProcessing = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
objenesis = { module = "org.objenesis:objenesis", version.ref = "objenesis" }
reflect = { module = "org.jetbrains.kotlin:kotlin-reflect" }
1 change: 1 addition & 0 deletions mockmp-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
implementation(libs.ksp.symbolProcessingApi)
implementation(libs.ksp.symbolProcessing)
implementation(libs.kotlinPoet.ksp)
implementation(projects.mockmpRuntime)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kodein.mock.ksp

import com.google.devtools.ksp.*
import com.google.devtools.ksp.common.impl.CodeGeneratorImpl
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.*
Expand Down Expand Up @@ -291,7 +292,7 @@ public class MocKMPProcessor(
}
gFile.addType(gCls.build())
gFile.build().writeTo(codeGenerator, Dependencies(true, *process.files.toTypedArray()))
moveInCommonTest()
moveInCommonTest(gFile.build(), process.files)
}

toFake.forEach { (vType, process) ->
Expand Down Expand Up @@ -360,7 +361,7 @@ public class MocKMPProcessor(
}
gFile.addFunction(gFun.build())
gFile.build().writeTo(codeGenerator, Dependencies(true, *filesDeps.toTypedArray()))
moveInCommonTest()
moveInCommonTest(gFile.build(), filesDeps)
}

toInject.forEach { (vCls, vProps) ->
Expand Down Expand Up @@ -417,34 +418,47 @@ public class MocKMPProcessor(
}
gFile.addFunction(gFun.build())
gFile.build().writeTo(codeGenerator, Dependencies(true, *filesDeps.toTypedArray()))
moveInCommonTest()
moveInCommonTest(gFile.build(), filesDeps)
}

return emptyList()
}

private fun moveInCommonTest() {
codeGenerator.generatedFile.forEach {
if (it.exists() && it.length() > 0) {
val file = if (it.absolutePath.contains("jvm")) {
// generated/ksp/jvm/jvmTest -> generated/ksp/common/commonTest
File(it.absolutePath.replace("jvm", "common"))
} else {
// generated/ksp/android/androidUnitTest[Debug|Release] -> generated/ksp/common/commonTest
File(
it.absolutePath
.replace("androidUnitTestDebug", "commonTest")
.replace("androidUnitTestRelease", "commonTest")
.replaceFirst("android", "common")
)
private fun moveInCommonTest(fileSpec: FileSpec, sources: java.util.HashSet<KSFile>) {
codeGenerator.generatedFile
.firstOrNull {
it.exists() && it.length() > 0 &&
it.path.contains(fileSpec.packageName.replace(".", "/")) &&
it.path.endsWith(fileSpec.name + ".kt")
}?.let {
if (it.exists() && it.length() > 0) {
val commonTestFile = if (it.absolutePath.contains("jvm")) {
// generated/ksp/jvm/jvmTest -> generated/ksp/common/commonTest
File(it.absolutePath.replace("jvm", "common"))
} else {
// generated/ksp/android/androidUnitTest[Debug|Release] -> generated/ksp/common/commonTest
File(
it.absolutePath
.replace("androidUnitTestDebug", "commonTest")
.replace("androidUnitTestRelease", "commonTest")
.replaceFirst("android", "common")
)
}
commonTestFile.parentFile.mkdirs()
commonTestFile.writeText(it.readText())

// Playing with KSP CodeGeneratorImpl to declare the right file path as output:
(codeGenerator as? CodeGeneratorImpl)?.let { cg ->
cg.sourceToOutputs.put(
commonTestFile,
sources.map { kSFile -> File(kSFile.filePath) }.toMutableSet()
)
}
// Deleting the file means that incremental compilation algorithm in KSP will fail.
// Putting empty seems to do the trick. (Not elegant, but if it works...)
it.writeText("")
//it.delete()
}
file.parentFile.mkdirs()
file.writeText(it.readText())
// Deleting the file means that incremental compilation algorithm in KSP will fail.
// Putting empty seems to do the trick. (Not elegant, but if it works...)
it.writeText("")
//it.delete()
}
}
} ?: error("Compiler issue!")
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
maven(url = "https://raw.githubusercontent.com/kosi-libs/kodein-internal-gradle-plugin/mvn-repo")
}
Expand Down

0 comments on commit 50a8bc2

Please sign in to comment.