https://kotlinlang.org logo
Title
c

charbgr

04/20/2022, 10:39 AM
Hello folks! Is there a way to produce classes from main sources to only test sources?
j

Jiaxiang

04/20/2022, 4:58 PM
you mean only generate code in test directory?
c

charbgr

04/20/2022, 4:59 PM
yes
j

Jiaxiang

04/20/2022, 5:00 PM
maybe explicitly running ksp task for test task only? Is that an option for you?
c

charbgr

04/20/2022, 5:00 PM
kspTest("our-processor")
produces classes from test sources to test sources! I want to read from main sources and produce for test sources!
j

Jiaxiang

04/20/2022, 5:04 PM
I see what you mean, can you do this: run ksp on main source for generating the test code you want, and include that generated source in test task’s input?
c

charbgr

04/20/2022, 5:10 PM
hmm how do I do something like this? is there a sample project/example I can look at?
j

Jiaxiang

04/20/2022, 5:14 PM
something like this? I am not sure if this will work for you but that’s my idea.
kotlin {
    sourceSets {
        test {
        kotlin.srcDir("$buildDir/generated")
        }
    }
}
c

charbgr

04/20/2022, 5:16 PM
will try it thanks!
👍 1
another question if you know 😛
do you think there is a way to make the ksp task run only when test task will run?
j

Jiaxiang

04/20/2022, 5:19 PM
it should be doable, just need writes in build script, I can try to play around with gradle when I got time.
c

charbgr

04/20/2022, 5:19 PM
atm I do something like this but it doesn not work perfect
gradle.taskGraph.whenReady {
    val willTestsRun = gradle.taskGraph.allTasks.any {
        it.name.startsWith("test")
    }
    ksp.arg("ourprocessor:willTestsRun", "$willTestsRun")
}