t
I will!
I have another question I have a compilation,
jvmTest
Copy code
compilation.allKotlinSourceSets
I would expect that to include
commonTest
, but it only includes a single source set,
jvmTest
. But I know (right?) that
commonTest
is a dependency of
jvmTest
. So why is
allKotlinSourceSets
not showing that? Maybe more generally, is there a way to get, via an API, all the source sets involved in any single compilation?
I can see in the docs that those source sets are connected. I also verified by adding a class with the same fully-qualified name to both
commonTest
and
jvmTest
=> there was a compilation failure due to the
Redeclaration
. So, I can just assume that there's a relationship due to the special nature of these default source sets and compilations, but it would be preferable to query an API, if it exists
I'd also like to be able to access generated source via supported APIs. I have source gen working such that compiling code will run my source gen task, but then I am not quite sure how I'd access all sources, included generated sources. This is fairly straightforward to do with the standard Gradle SourceSet APIs
Copy code
abstract class SourceGen : DefaultTask() {

  @get:OutputDirectory abstract val output: DirectoryProperty

  @TaskAction fun action() {
    val outputDir = output.get()
    val file = outputDir.file("foo/Foo.kt").asFile
    file.parentFile.mkdirs()

    file.writeText(
      """
        package foo
        
        class Foo
      """.trimIndent()
    )
  }
}

val sourceGen = tasks.register<SourceGen>("sourceGen") {
  output.set(layout.buildDirectory.dir("gen"))
}

kotlin {
  sourceSets {
    commonMain {
      kotlin {
        srcDir(sourceGen.flatMap { it.output })
      }
    }
}
and with that code above, compiling my jvmMain source will result in the
sourceGen
task running and adding that generated source to the commonMain source set
1
ahh I was able to access the generated sources by passing in the
sourceSet.kotlin.sourceDirectories
directly as the task input, rather than
sourceSet.kotlin.sourceDirectories.asFileTree.files
, so that's all good now