Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect BOM constraints automatically #1269

Merged
merged 2 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions build-support/src/main/kotlin/bom.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Locale
import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget

/**
* Collect all the root project's multiplatform targets and add them to the BOM.
*
* Only published subprojects are included.
*
* This supports Kotlin/Multiplatform and Kotlin/JS subprojects.
*/
fun Project.collectBomConstraints() {
swankjesse marked this conversation as resolved.
Show resolved Hide resolved
val bomConstraints: DependencyConstraintHandler = dependencies.constraints
rootProject.subprojects {
val subproject = this

subproject.plugins.withId("com.vanniktech.maven.publish.base") {
subproject.plugins.withType<KotlinAndroidPluginWrapper> {
bomConstraints.api(subproject)
}

subproject.plugins.withType<KotlinJsPluginWrapper> {
bomConstraints.api(subproject)
}

subproject.plugins.withType<KotlinMultiplatformPluginWrapper> {
subproject.extensions.getByType<KotlinMultiplatformExtension>().targets.all {
bomConstraints.api(dependencyConstraint(this))
}
}
}
}
}

/** Returns a string like "com.squareup.okio:okio-iosarm64:3.4.0" for this target. */
private fun Project.dependencyConstraint(target: KotlinTarget): String {
val artifactId = when (target) {
is KotlinMetadataTarget -> name
is KotlinJsTarget -> "$name-js"
else -> "$name-${target.targetName.toLowerCase(Locale.ROOT)}"
}
return "$group:$artifactId:$version"
}

private fun DependencyConstraintHandler.api(constraintNotation: Any) =
add("api", constraintNotation)
5 changes: 2 additions & 3 deletions okio-assetfilesystem/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.vanniktech.maven.publish.JavadocJar.Dokka
import com.vanniktech.maven.publish.KotlinMultiplatform
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import com.vanniktech.maven.publish.MavenPublishBaseExtension

plugins {
Expand Down Expand Up @@ -31,6 +30,6 @@ dependencies {

configure<MavenPublishBaseExtension> {
configure(
KotlinMultiplatform(javadocJar = Dokka("dokkaGfm"))
AndroidSingleVariantLibrary()
)
}
11 changes: 1 addition & 10 deletions okio-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ plugins {
id("java-platform")
}

dependencies {
constraints {
api(projects.okio)
api(projects.okioFakefilesystem)
if (kmpJsEnabled) {
// No typesafe project accessor as the accessor won't exist if kmpJs is not enabled.
api(project(":okio-nodefilesystem"))
}
}
}
collectBomConstraints()

extensions.configure<PublishingExtension> {
publications.create("maven", MavenPublication::class) {
Expand Down