KSP - How to consolidate module results into a file

116 Views Asked by At

I am looking to utilize KSP to generate a comprehensive list of feature flags across various modules in my project, including the 'app', 'module1', 'module2', up to 'moduleN'.

Currently, I have a code processor in place that gathers each @FeatureFlag annotation, and the KSP processor serves as a dependency for each module.

Here is a snapshot of how feature flags are collected in each module:

Module 1:

#build/generated/ksp/main/kotlin/package

internal val featuresFlags : Set<FeatureFlag> = setOf(settingsFeatureFlag, profileFeatureFlag)

Module 2:

#build/generated/ksp/main/kotlin/package

internal val featuresFlags : Set<FeatureFlag> = setOf(fingerprintFeatureFlag)

Module N

#build/generated/ksp/main/kotlin/package

internal val featuresFlags : Set<FeatureFlag> = setOf(0...N)

Now, I'm seeking a way to consolidate these individual results into a single variable or use case. The desired outcome is as follows:

Feature flag module

internal val featuresFlags : Set<FeatureFlag> = setOf(settingsFeatureFlag, profileFeatureFlag, fingerprintFeatureFlag, ...) # contact all modules result

I am also grappling with a potential limitation in KSP -ksp-issue. Also, when I try to write outcome out of each module I get

[ksp] java.lang.IllegalStateException: requested path is outside the bounds of the required directory

1

There are 1 best solutions below

0
Nikola Despotoski On

None of the options I have tried myself, just spitting ideas.

Have you tried to pass the target build directory as KSP argument to the submodules?

Try adding this to the app module gradle:

tasks.withType<com.google.devtools.ksp.gradle.KspTask> {
    options.add(SubpluginOption("targetOutputDir", "build/generated/ksp/debug/kotlin"))
}

Alternatively, after each ksp task is done:

This will append directory as command line argument

tasks.withType<com.google.devtools.ksp.gradle.KspTask> {
    commandLineArgumentProviders.add(CommandLineArgumentProvider { listOf("app/build/generated/ksp/debug/kotlin") })

Or

tasks.withType<com.google.devtools.ksp.gradle.KspTask> {
    doLast {
        val targetFile = File("${projectDir}/app/build/generated/ksp/debug/kotlin", "FeatureFlags.kt")
        targetFile.outputStream().use {output ->
            outputs.files.forEach {
                //copy content
                //append to target file
                output.write(it.readBytes())
            }
        }

    }
}