[Gradle] Replace useCompilerVersion by a Provider API property

Now this property acts as the single source of truth of BTA implementation version used in KGP. It allows both read and write.
^KT-62921 Fixed
This commit is contained in:
Alexander.Likhachev
2024-01-23 23:59:15 +01:00
committed by Space Team
parent 10cedd495d
commit a904ded5f2
3 changed files with 30 additions and 15 deletions
@@ -159,7 +159,7 @@ private fun TestProject.enableOtherVersionBuildToolsImpl() {
buildGradle.append(
"""
kotlin {
useCompilerVersion("$OTHER_KOTLIN_VERSION")
compilerVersion.set("$OTHER_KOTLIN_VERSION")
}
""".trimIndent()
)
@@ -10,12 +10,11 @@ import org.gradle.api.Named
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.internal.plugins.DslObject
import org.gradle.api.provider.Property
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.jvm.toolchain.JavaToolchainSpec
import org.jetbrains.kotlin.buildtools.api.ExperimentalBuildToolsApi
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.internal.KOTLIN_BUILD_TOOLS_API_IMPL
import org.jetbrains.kotlin.gradle.internal.KOTLIN_MODULE_GROUP
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle.CoroutineStart.Undispatched
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
@@ -139,18 +138,26 @@ abstract class KotlinTopLevelExtension(internal val project: Project) : KotlinTo
}
}
/**
* Allows to use a different version of the Kotlin Build Tools API implementation and effectively a different version of the compiler.
*
* By default, the Kotlin Build Tools API implementation of the same version as the KGP is used.
*
* Currently only has an effect if the `kotlin.compiler.runViaBuildToolsApi` Gradle property is set to `true`.
*/
@Deprecated("This method is replaced by the `compilerVersion` property", ReplaceWith("compilerVersion"), DeprecationLevel.ERROR)
@ExperimentalKotlinGradlePluginApi
@ExperimentalBuildToolsApi
fun useCompilerVersion(version: String) {
project.dependencies.add(BUILD_TOOLS_API_CLASSPATH_CONFIGURATION_NAME, "$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL:$version")
compilerVersion.set(version)
}
/**
* The version of the Kotlin compiler.
*
* By default, the Kotlin Build Tools API implementation of the same version as the KGP is used.
*
* Be careful with reading the property's value as eager reading will finalize the value and prevent it from being configured.
*
* Note: Currently only has an effect if the `kotlin.compiler.runViaBuildToolsApi` Gradle property is set to `true`.
*/
@ExperimentalKotlinGradlePluginApi
@ExperimentalBuildToolsApi
val compilerVersion: Property<String> =
project.objects.propertyWithConvention(project.getKotlinPluginVersion()).chainedFinalizeValueOnRead()
}
internal fun ExplicitApiMode.toCompilerValue() = when (this) {
@@ -20,6 +20,7 @@ import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectFactory
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.ExternalDependency
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
@@ -102,10 +103,17 @@ abstract class DefaultKotlinBasePlugin : KotlinBasePlugin {
project
.configurations
.maybeCreateResolvable(BUILD_TOOLS_API_CLASSPATH_CONFIGURATION_NAME)
.defaultDependencies {
it.add(
project.dependencies.create("$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL:${project.getKotlinPluginVersion()}")
)
.also {
project.dependencies.add(it.name, "$KOTLIN_MODULE_GROUP:$KOTLIN_BUILD_TOOLS_API_IMPL")
it.withDependencies { dependencies ->
dependencies
.withType<ExternalDependency>()
.configureEach { dependency ->
dependency.version { versionConstraint ->
versionConstraint.strictly(project.kotlinExtension.compilerVersion.get())
}
}
}
}
project
.tasks