Skip to content

Commit

Permalink
Introduce KspAATask.commandLineArgumentProviders
Browse files Browse the repository at this point in the history
so that each KSP task can configured differently.
  • Loading branch information
ting-yuan committed Nov 12, 2024
1 parent c00cad1 commit 755cba4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.logging.LogLevel
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Optional
import org.gradle.process.CommandLineArgumentProvider
import org.gradle.work.ChangeType
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
Expand Down Expand Up @@ -63,6 +65,9 @@ abstract class KspAATask @Inject constructor(
@get:Nested
abstract val kspConfig: KspGradleConfig

@get:Nested
abstract val commandLineArgumentProviders: ListProperty<CommandLineArgumentProvider>

@TaskAction
fun execute(inputChanges: InputChanges) {
// FIXME: Create a class loader with clean classpath instead of shadowing existing ones. It'll require either:
Expand Down Expand Up @@ -220,8 +225,9 @@ abstract class KspAATask @Inject constructor(
)
)
cfg.processorOptions.putAll(kspExtension.apOptions)
cfg.processorOptions.putAll(
kspExtension.commandLineArgumentProviders.map { providers ->

fun ListProperty<CommandLineArgumentProvider>.mapArgProviders() =
map { providers ->
buildMap {
for (provider in providers) {
provider.asArguments().forEach { argument ->
Expand All @@ -234,7 +240,10 @@ abstract class KspAATask @Inject constructor(
}
}
}
)

cfg.processorOptions.putAll(kspExtension.commandLineArgumentProviders.mapArgProviders())
cfg.processorOptions.putAll(kspAATask.commandLineArgumentProviders.mapArgProviders())

val logLevel = LogLevel.entries.first {
project.logger.isEnabled(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ class GradleCompilationTest(val useKSP2: Boolean) {
@Test
fun commandLineArgumentIsIncludedInApoptionsWhenAddedInKspTask() {
Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows", ignoreCase = true))
Assume.assumeFalse(useKSP2)
testRule.setupAppAsAndroidApp()
testRule.appModule.dependencies.addAll(
listOf(
Expand Down Expand Up @@ -361,6 +360,17 @@ class GradleCompilationTest(val useKSP2: Boolean) {
println("commandLine=${'$'}{commandLine.asArguments()}")
}
}
tasks.withType<com.google.devtools.ksp.gradle.KspAATask>().configureEach {
val destination = project.layout.projectDirectory.dir("schemas-${'$'}{this.name}")
commandLineArgumentProviders.add(Provider(destination.asFile))
kspConfig.processorOptions.get().forEach { (key, value) ->
println("apoption=${'$'}key=${'$'}value")
}
commandLineArgumentProviders.get().forEach { commandLine ->
println("commandLine=${'$'}{commandLine.asArguments()}")
}
}
}
""".trimIndent()
)
Expand Down

0 comments on commit 755cba4

Please sign in to comment.