Create plugin for test to set language and api kotlin versions
This commit is contained in:
committed by
Space Team
parent
1c2f34ab4e
commit
d6a9bca5ec
@@ -0,0 +1,3 @@
|
||||
## Description
|
||||
|
||||
Contains a plugin for Gradle tests to set Kotlin arguments (api and language version) via 'gradle.properties'
|
||||
@@ -0,0 +1,37 @@
|
||||
import plugins.KotlinBuildPublishingPlugin
|
||||
|
||||
plugins {
|
||||
id("gradle-plugin-common-configuration")
|
||||
}
|
||||
|
||||
|
||||
dependencies {
|
||||
commonApi(project(":kotlin-gradle-plugin-api"))
|
||||
commonApi(project(":kotlin-gradle-plugin"))
|
||||
commonCompileOnly(gradleKotlinDsl())
|
||||
}
|
||||
|
||||
|
||||
gradlePlugin {
|
||||
plugins {
|
||||
create("kotlin-compiler-args-properties") {
|
||||
id = "org.jetbrains.kotlin.test.kotlin-compiler-args-properties"
|
||||
displayName = "GradleKotlinCompilerArgumentsPlugin"
|
||||
description = displayName
|
||||
implementationClass = "org.jetbrains.kotlin.gradle.arguments.GradleKotlinCompilerArgumentsPlugin"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Disable releasing for this plugin
|
||||
// It is not intended to be released publicly
|
||||
tasks.withType<PublishToMavenRepository>()
|
||||
.configureEach {
|
||||
if (name.endsWith("PublicationTo${KotlinBuildPublishingPlugin.REPOSITORY_NAME}Repository")) {
|
||||
enabled = false
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named("publishPlugins") {
|
||||
enabled = false
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle.arguments
|
||||
|
||||
import org.gradle.api.Action
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.plugins.ExtensionContainer
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.provider.ProviderFactory
|
||||
import org.gradle.kotlin.dsl.withType
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
|
||||
import javax.inject.Inject
|
||||
|
||||
class GradleKotlinCompilerArgumentsPlugin @Inject constructor(
|
||||
private val providerFactory: ProviderFactory
|
||||
) : KotlinBasePlugin {
|
||||
override val pluginVersion = "test"
|
||||
|
||||
override fun apply(project: Project) {
|
||||
val properties = KotlinTaskProperties(providerFactory)
|
||||
project.configureKotlinVersions(properties)
|
||||
}
|
||||
|
||||
private fun Project.configureKotlinVersions(properties: KotlinTaskProperties) {
|
||||
plugins.withType<KotlinBasePlugin>().configureEach(configureTask(properties))
|
||||
}
|
||||
|
||||
private fun Project.configureTask(properties: KotlinTaskProperties): Action<KotlinBasePlugin> = Action {
|
||||
tasks.withType<KotlinCompilationTask<*>>().configureEach {
|
||||
configureKotlinOption(properties, it.compilerOptions)
|
||||
}
|
||||
}
|
||||
|
||||
private fun ExtensionContainer.projectCompilerOptions(): KotlinCommonCompilerOptions? =
|
||||
findByType(KotlinJvmProjectExtension::class.java)?.compilerOptions
|
||||
?: findByType(KotlinAndroidProjectExtension::class.java)?.compilerOptions
|
||||
|
||||
private fun Project.projectLevelLanguageVersion(): Provider<KotlinVersion> {
|
||||
return extensions.projectCompilerOptions()?.languageVersion ?: providers.provider { null }
|
||||
}
|
||||
|
||||
private fun Project.projectLevelApiVersion(): Provider<KotlinVersion> {
|
||||
return extensions.projectCompilerOptions()?.apiVersion ?: providers.provider { null }
|
||||
}
|
||||
|
||||
private fun Project.configureKotlinOption(properties: KotlinTaskProperties, taskOptions: KotlinCommonCompilerOptions) {
|
||||
taskOptions.languageVersion.convention(properties.kotlinLanguageVersion.orElse(projectLevelLanguageVersion()))
|
||||
taskOptions.apiVersion.convention(properties.kotlinApiVersion.orElse(projectLevelApiVersion()))
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle.arguments
|
||||
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.provider.ProviderFactory
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
|
||||
|
||||
class KotlinTaskProperties(providerFactory: ProviderFactory) {
|
||||
val kotlinLanguageVersion: Provider<KotlinVersion> = readKotlinVersionProperty(KOTLIN_LANGUAGE_VERSION_PROPERTY, providerFactory)
|
||||
val kotlinApiVersion: Provider<KotlinVersion> = readKotlinVersionProperty(KOTLIN_API_VERSION_PROPERTY, providerFactory)
|
||||
|
||||
companion object {
|
||||
private const val KOTLIN_LANGUAGE_VERSION_PROPERTY = "kotlin.test.languageVersion"
|
||||
private const val KOTLIN_API_VERSION_PROPERTY = "kotlin.test.apiVersion"
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun readKotlinVersionProperty(propertyName: String, providerFactory: ProviderFactory): Provider<KotlinVersion> {
|
||||
return if (GradleVersion.current() < GradleVersion.version("7.4")) {
|
||||
providerFactory.gradleProperty(propertyName)
|
||||
.forUseAtConfigurationTime()
|
||||
.map { KotlinVersion.fromVersion(it as String) }
|
||||
} else {
|
||||
providerFactory.gradleProperty(propertyName)
|
||||
.map { KotlinVersion.fromVersion(it as String) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user