[Gradle, JS] Add module kotlin-dom-compat-api as dependency with opt-out

This commit is contained in:
Ilya Goncharov
2022-12-15 19:33:24 +01:00
committed by Space Team
parent 688894aabc
commit 24cb10bbf3
4 changed files with 146 additions and 2 deletions
@@ -43,6 +43,10 @@ internal fun customizeKotlinDependencies(project: Project) {
)
}
if (propertiesProvider.stdlibDefaultDependency && propertiesProvider.stdlibDomApiIncluded) {
project.configureKotlinDomApiDefaultDependency(topLevelExtension, coreLibrariesVersion)
}
project.configurations.configureDefaultVersionsResolutionStrategy(
coreLibrariesVersion
)
@@ -0,0 +1,135 @@
/*
* Copyright 2010-2022 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.internal
import org.gradle.api.Project
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.GradleKpmModule
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
import org.jetbrains.kotlin.gradle.plugin.sources.sourceSetDependencyConfigurationByScope
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation
import org.jetbrains.kotlin.gradle.targets.js.npm.SemVer
private const val KOTLIN_DOM_API_MODULE_NAME = "kotlin-dom-api-compat"
private val Dependency.isKotlinDomApiDependency: Boolean
get() = group == KOTLIN_MODULE_GROUP && (name == KOTLIN_DOM_API_MODULE_NAME)
private val kotlin1820Version = SemVer(1.toBigInteger(), 8.toBigInteger(), 20.toBigInteger())
private fun isAtLeast1_8_20(version: String) = SemVer.fromGradleRichVersion(version) >= kotlin1820Version
internal fun Project.configureKotlinDomApiDefaultDependency(
topLevelExtension: KotlinTopLevelExtension,
coreLibrariesVersion: Provider<String>
) {
when (topLevelExtension) {
is KotlinPm20ProjectExtension -> addKotlinDomApiToKpmProject(project, coreLibrariesVersion)
is KotlinJsProjectExtension -> topLevelExtension.registerTargetObserver { target ->
target?.addKotlinDomApiDependency(configurations, dependencies, coreLibrariesVersion)
}
is KotlinSingleTargetExtension<*> -> topLevelExtension
.target
.addKotlinDomApiDependency(configurations, dependencies, coreLibrariesVersion)
is KotlinMultiplatformExtension -> topLevelExtension
.targets
.configureEach { target ->
target.addKotlinDomApiDependency(configurations, dependencies, coreLibrariesVersion)
}
}
}
private fun addKotlinDomApiToKpmProject(
project: Project,
coreLibrariesVersion: Provider<String>
) {
project.pm20Extension.modules.named(GradleKpmModule.MAIN_MODULE_NAME) { main ->
main.variants.configureEach { variant ->
when (variant.platformType) {
KotlinPlatformType.common -> error("variants are not expected to be common")
KotlinPlatformType.js -> {
val dependencyHandler = project.dependencies
variant.dependencies {
api(dependencyHandler.kotlinDomApiDependency(coreLibrariesVersion.get()))
}
}
else -> {}
}
}
}
}
private fun KotlinTarget.addKotlinDomApiDependency(
configurations: ConfigurationContainer,
dependencies: DependencyHandler,
coreLibrariesVersion: Provider<String>
) {
compilations.configureEach { compilation ->
compilation.allKotlinSourceSets.forEach { kotlinSourceSet ->
val scopeConfiguration = configurations
.sourceSetDependencyConfigurationByScope(kotlinSourceSet, KotlinDependencyScope.API_SCOPE)
scopeConfiguration.withDependencies { dependencySet ->
if (compilation.platformType != KotlinPlatformType.js) return@withDependencies
if (compilation !is KotlinJsIrCompilation) return@withDependencies
if (isKotlinDomApiAddedByUser(configurations, kotlinSourceSet)) return@withDependencies
val stdlibDependency = KotlinDependencyScope.values()
.map { scope ->
configurations.sourceSetDependencyConfigurationByScope(kotlinSourceSet, scope)
}
.flatMap { it.allNonProjectDependencies() }
.singleOrNull { dependency ->
dependency.group == KOTLIN_MODULE_GROUP && dependency.name in stdlibModules
}
if (stdlibDependency != null) {
val depVersion = stdlibDependency.version ?: coreLibrariesVersion.get()
if (!isAtLeast1_8_20(depVersion)) return@withDependencies
// Check if stdlib is directly added to SourceSet
dependencySet.addLater(
coreLibrariesVersion.map {
dependencies.kotlinDomApiDependency(it)
}
)
}
}
}
}
}
private fun isKotlinDomApiAddedByUser(
configurations: ConfigurationContainer,
vararg sourceSets: KotlinSourceSet
): Boolean {
return sourceSets
.asSequence()
.flatMap { sourceSet ->
KotlinDependencyScope.values().map { scope ->
configurations.sourceSetDependencyConfigurationByScope(sourceSet, scope)
}.asSequence()
}
.flatMap { it.allNonProjectDependencies().asSequence() }
.any { it.isKotlinDomApiDependency }
}
internal fun DependencyHandler.kotlinDomApiDependency(versionOrNull: String?) =
create("$KOTLIN_MODULE_GROUP:$KOTLIN_DOM_API_MODULE_NAME${versionOrNull?.prependIndent(":").orEmpty()}")
@@ -171,7 +171,7 @@ private fun KotlinTarget.addStdlibDependency(
}
}
private fun isStdlibAddedByUser(
internal fun isStdlibAddedByUser(
configurations: ConfigurationContainer,
stdlibModules: Set<String>,
vararg sourceSets: KotlinSourceSet
@@ -220,7 +220,7 @@ private fun KotlinSourceSet.isRelatedToAndroidTestSourceSet(): Boolean {
return androidVariant in androidTestVariants
}
private val stdlibModules = setOf(
internal val stdlibModules = setOf(
"kotlin-stdlib-common",
"kotlin-stdlib",
"kotlin-stdlib-jdk7",
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.cli.common.toBooleanLenient
import org.jetbrains.kotlin.gradle.dsl.NativeCacheKind
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessageOutputStreamHandler.Companion.IGNORE_TCSM_OVERFLOW
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.Companion.jsCompilerProperty
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_JS_STDLIB_DOM_API_INCLUDED
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_ABI_SNAPSHOT
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_JS_KARMA_BROWSERS
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_MPP_ANDROID_GRADLE_PLUGIN_COMPATIBILITY_NO_WARN
@@ -428,6 +429,9 @@ internal class PropertiesProvider private constructor(private val project: Proje
val stdlibJdkVariantsVersionAlignment: Boolean
get() = booleanProperty(KOTLIN_STDLIB_JDK_VARIANTS_VERSION_ALIGNMENT) ?: true
val stdlibDomApiIncluded: Boolean
get() = booleanProperty(KOTLIN_JS_STDLIB_DOM_API_INCLUDED) ?: true
val kotlinTestInferJvmVariant: Boolean
get() = booleanProperty("kotlin.test.infer.jvm.variant") ?: true
@@ -509,6 +513,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
object PropertyNames {
const val KOTLIN_STDLIB_DEFAULT_DEPENDENCY = "kotlin.stdlib.default.dependency"
const val KOTLIN_STDLIB_JDK_VARIANTS_VERSION_ALIGNMENT = "kotlin.stdlib.jdk.variants.version.alignment"
const val KOTLIN_JS_STDLIB_DOM_API_INCLUDED = "kotlin.js.stdlib.dom.api.included"
const val KOTLIN_MPP_ENABLE_GRANULAR_SOURCE_SETS_METADATA = "kotlin.mpp.enableGranularSourceSetsMetadata"
const val KOTLIN_MPP_ENABLE_CINTEROP_COMMONIZATION = "kotlin.mpp.enableCInteropCommonization"
const val KOTLIN_MPP_HIERARCHICAL_STRUCTURE_BY_DEFAULT = "kotlin.internal.mpp.hierarchicalStructureByDefault"