Skip to content

Commit

Permalink
retrieving commandlinearguments from ksptasks in apoptions
Browse files Browse the repository at this point in the history
(cherry picked from commit 797ce6f)
  • Loading branch information
cdsap authored and KSP Auto Pick committed Nov 3, 2022
1 parent 474d852 commit 3576878
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Expand Up @@ -141,6 +141,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
target: String,
isIncremental: Boolean,
allWarningsAsErrors: Boolean,
commandLineArgumentProviders: ListProperty<CommandLineArgumentProvider>,
): List<SubpluginOption> {
val options = mutableListOf<SubpluginOption>()
options += SubpluginOption("classOutputDir", getKspClassOutputDir(project, sourceSetName, target).path)
Expand Down Expand Up @@ -169,7 +170,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
kspExtension.apOptions.forEach {
options += SubpluginOption("apoption", "${it.key}=${it.value}")
}
kspExtension.commandLineArgumentProviders.forEach {
commandLineArgumentProviders.get().forEach {
val argument = it.asArguments().joinToString("")
if (!argument.matches(Regex("\\S+=\\S+"))) {
throw IllegalArgumentException("KSP apoption does not match \\S+=\\S+: $argument")
Expand Down Expand Up @@ -265,6 +266,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
.extendsFrom(*nonEmptyKspConfigurations.toTypedArray())
kspTask.processorClasspath.from(processorClasspath)
kspTask.dependsOn(processorClasspath.buildDependencies)
kspTask.commandLineArgumentProviders.addAll(kspExtension.commandLineArgumentProviders)

kspTask.options.addAll(
kspTask.project.provider {
Expand All @@ -275,11 +277,11 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
sourceSetName,
target,
isIncremental,
kspExtension.allWarningsAsErrors
kspExtension.allWarningsAsErrors,
kspTask.commandLineArgumentProviders
)
}
)
kspTask.commandLineArgumentProviders.addAll(kspExtension.commandLineArgumentProviders)
kspTask.destination = kspOutputDir
kspTask.blockOtherCompilerPlugins = kspExtension.blockOtherCompilerPlugins
kspTask.apOptions.value(kspExtension.arguments).disallowChanges()
Expand Down
Expand Up @@ -234,4 +234,48 @@ class GradleCompilationTest {
val result = testRule.runner().withArguments(":app:assemble").buildAndFail()
assertThat(result.output).contains("KSP apoption does not match \\S+=\\S+: invalid")
}

@Test
fun commandLineArgumentIsIncludedInApoptionsWhenAddedInKspTask() {
testRule.setupAppAsAndroidApp()
testRule.appModule.dependencies.addAll(
listOf(
artifact(configuration = "ksp", "androidx.room:room-compiler:2.4.2")
)
)
testRule.appModule.buildFileAdditions.add(
"""
class Provider(roomOutputDir: File) : CommandLineArgumentProvider {
@OutputDirectory
val outputDir = roomOutputDir
override fun asArguments(): Iterable<String> {
return listOf(
"room.schemaLocation=${'$'}{outputDir.path}"
)
}
}
afterEvaluate {
tasks.withType<com.google.devtools.ksp.gradle.KspTask>().configureEach {
val destination = project.layout.projectDirectory.dir("schemas-${'$'}{this.name}")
commandLineArgumentProviders.add(Provider(destination.asFile))
options.get().forEach { option ->
println("${'$'}{option.key}=${'$'}{option.value}")
}
commandLineArgumentProviders.get().forEach { commandLine ->
println("commandLine=${'$'}{commandLine.asArguments()}")
}
}
}
""".trimIndent()
)
val result = testRule.runner().withDebug(true).withArguments(":app:assembleDebug").build()
val pattern1 = Regex.escape("apoption=room.schemaLocation=")
val pattern2 = Regex.escape("${testRule.appModule.moduleRoot}/schemas-kspDebugKotlin")
val pattern3 = Regex.escape("commandLine=[")
assertThat(result.output).containsMatch("$pattern1\\S*$pattern2")
assertThat(result.output).containsMatch("$pattern3\\S*$pattern2")
}
}

0 comments on commit 3576878

Please sign in to comment.