Skip to content

Commit

Permalink
Allow enabling KSP2 per module
Browse files Browse the repository at this point in the history
With the change in google#2034 a side effect was that KSP2 had to be enabled
per Gradle project, not per module.

With this change a DSL property has been added to handle this.
  • Loading branch information
ansman committed Sep 6, 2024
1 parent 6af7077 commit b7be70f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@

package com.google.devtools.ksp.gradle

import com.google.devtools.ksp.KspExperimental
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.process.CommandLineArgumentProvider
import javax.inject.Inject

@OptIn(KspExperimental::class)
abstract class KspExtension @Inject constructor(project: Project) {
/**
* Enables or disables KSP 2, defaults to the `ksp.useKsp2` gradle property or `false` if that's not set.
*
* This API is temporary and will be removed once KSP1 is removed.
*/
@KspExperimental
abstract val useKsp2: Property<Boolean>

internal val apOptions = project.objects.mapProperty(String::class.java, String::class.java)
internal val commandLineArgumentProviders = project.objects.listProperty(CommandLineArgumentProvider::class.java)
.also { it.finalizeValueOnRead() }
Expand All @@ -37,6 +48,16 @@ abstract class KspExtension @Inject constructor(project: Project) {

open val arguments: Map<String, String> get() = apOptions.get()

init {
@Suppress("LeakingThis")
useKsp2.convention(
project.providers
.gradleProperty("ksp.useKsp2")
.map { it.toBoolean() }
.orElse(false)
)
}

open fun arg(k: String, v: String) {
if ('=' in k) {
throw GradleException("'=' is not allowed in custom option's name.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
val isIntermoduleIncremental =
(project.providers.gradleProperty("ksp.incremental.intermodule").orNull?.toBoolean() ?: true) &&
isIncremental
val useKSP2 = project.providers.gradleProperty("ksp.useKSP2").orNull?.toBoolean() ?: false
val useKSP2 = kspExtension.useKsp2
.apply { finalizeValue() }
.get()

// Create and configure KSP tasks.
val kspTaskProvider = if (useKSP2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,16 @@ class GradleCompilationTest {
assertThat(result.output).contains("HAS LIBRARY: ")
assertThat(result.output).doesNotContain("app/build/generated/ksp/main/classes")
}

@Test
fun changingKsp2AtRuntime() {
testRule.setupAppAsJvmApp()
testRule.appModule.buildFileAdditions.add(
"""
ksp { useKsp2.set(true) }
""".trimIndent()
)

testRule.runner().withArguments().build()
}
}

0 comments on commit b7be70f

Please sign in to comment.