Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce KspAATask.commandLineArgumentProviders #2201

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading