|
| 1 | +import java.util.Locale |
| 2 | +import org.gradle.api.Project |
| 3 | +import org.gradle.api.artifacts.dsl.DependencyConstraintHandler |
| 4 | +import org.gradle.kotlin.dsl.getByType |
| 5 | +import org.gradle.kotlin.dsl.withType |
| 6 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 7 | +import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper |
| 8 | +import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper |
| 9 | +import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper |
| 10 | +import org.jetbrains.kotlin.gradle.plugin.KotlinTarget |
| 11 | +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget |
| 12 | +import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget |
| 13 | + |
| 14 | +/** |
| 15 | + * Collect all the root project's multiplatform targets and add them to the BOM. |
| 16 | + * |
| 17 | + * Only published subprojects are included. |
| 18 | + * |
| 19 | + * This supports Kotlin/Multiplatform and Kotlin/JS subprojects. |
| 20 | + */ |
| 21 | +fun Project.collectBomConstraints() { |
| 22 | + val bomConstraints: DependencyConstraintHandler = dependencies.constraints |
| 23 | + rootProject.subprojects { |
| 24 | + val subproject = this |
| 25 | + |
| 26 | + subproject.plugins.withId("com.vanniktech.maven.publish.base") { |
| 27 | + subproject.plugins.withType<KotlinAndroidPluginWrapper> { |
| 28 | + bomConstraints.api(subproject) |
| 29 | + } |
| 30 | + |
| 31 | + subproject.plugins.withType<KotlinJsPluginWrapper> { |
| 32 | + bomConstraints.api(subproject) |
| 33 | + } |
| 34 | + |
| 35 | + subproject.plugins.withType<KotlinMultiplatformPluginWrapper> { |
| 36 | + subproject.extensions.getByType<KotlinMultiplatformExtension>().targets.all { |
| 37 | + bomConstraints.api(dependencyConstraint(this)) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** Returns a string like "com.squareup.okio:okio-iosarm64:3.4.0" for this target. */ |
| 45 | +private fun Project.dependencyConstraint(target: KotlinTarget): String { |
| 46 | + val artifactId = when (target) { |
| 47 | + is KotlinMetadataTarget -> name |
| 48 | + is KotlinJsTarget -> "$name-js" |
| 49 | + else -> "$name-${target.targetName.toLowerCase(Locale.ROOT)}" |
| 50 | + } |
| 51 | + return "$group:$artifactId:$version" |
| 52 | +} |
| 53 | + |
| 54 | +private fun DependencyConstraintHandler.api(constraintNotation: Any) = |
| 55 | + add("api", constraintNotation) |
0 commit comments