From b572868affbbaf7e5f621f983a7f9b76bd1200b2 Mon Sep 17 00:00:00 2001 From: Benoit Quenaudon Date: Fri, 7 Jul 2023 13:02:46 +0200 Subject: [PATCH] Sweet BOM collections --- build-logic/build.gradle.kts | 24 ++++++++ build-logic/src/main/kotlin/BuildLogic.kt | 23 ++++++++ build-logic/src/main/kotlin/bom.kt | 70 +++++++++++++++++++++++ wire-bom/build.gradle.kts | 24 +------- 4 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 build-logic/src/main/kotlin/BuildLogic.kt create mode 100644 build-logic/src/main/kotlin/bom.kt diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 3179e90af2..7cdb4bea87 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -22,6 +22,30 @@ buildscript { } } +plugins { + `kotlin-dsl` + `java-gradle-plugin` +} + +repositories { + mavenCentral() +} + +dependencies { + add("compileOnly", kotlin("gradle-plugin")) + add("compileOnly", kotlin("gradle-plugin-api")) + implementation(libs.pluginz.kotlin) +} + +gradlePlugin { + plugins { + create("com.squareup.wire.build.logic") { + id = "build-logic" + implementationClass = "BuildLogic" + } + } +} + rootProject.plugins.withType(NodeJsRootPlugin::class) { // 16+ required for Apple Silicon support // https://youtrack.jetbrains.com/issue/KT-49109#focus=Comments-27-5259190.0-0 diff --git a/build-logic/src/main/kotlin/BuildLogic.kt b/build-logic/src/main/kotlin/BuildLogic.kt new file mode 100644 index 0000000000..e460a9eee0 --- /dev/null +++ b/build-logic/src/main/kotlin/BuildLogic.kt @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import org.gradle.api.Plugin +import org.gradle.api.Project + +class BuildLogic : Plugin { + override fun apply(project: Project) { + // Do nothing. + } +} diff --git a/build-logic/src/main/kotlin/bom.kt b/build-logic/src/main/kotlin/bom.kt new file mode 100644 index 0000000000..b647714ce3 --- /dev/null +++ b/build-logic/src/main/kotlin/bom.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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() { + val bomConstraints: DependencyConstraintHandler = dependencies.constraints + rootProject.subprojects { + val subproject = this + + subproject.plugins.withId("com.vanniktech.maven.publish.base") { + subproject.plugins.withType { + bomConstraints.api(subproject) + } + + subproject.plugins.withType { + bomConstraints.api(subproject) + } + + subproject.plugins.withType { + subproject.extensions.getByType().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) diff --git a/wire-bom/build.gradle.kts b/wire-bom/build.gradle.kts index 1594c2ac8d..809db39a64 100644 --- a/wire-bom/build.gradle.kts +++ b/wire-bom/build.gradle.kts @@ -1,30 +1,10 @@ plugins { id("com.vanniktech.maven.publish.base") id("java-platform") + id("com.squareup.wire.build.logic") } -dependencies { - constraints { - api(projects.wireCompiler) - api(projects.wireGradlePlugin) - api(projects.wireGrpcClient) - api(projects.wireGrpcClient.group + ":wire-grpc-client-jvm:" + projects.wireGrpcClient.version) - api(projects.wireGrpcServer) - api(projects.wireGrpcServerGenerator) - api(projects.wireGrpcMockwebserver) - api(projects.wireGsonSupport) - api(projects.wireJavaGenerator) - api(projects.wireKotlinGenerator) - api(projects.wireMoshiAdapter) - api(projects.wireReflector) - api(projects.wireRuntime) - api(projects.wireRuntime.group + ":wire-runtime-jvm:" + projects.wireRuntime.version) - api(projects.wireSchema) - api(projects.wireSchema.group + ":wire-schema-jvm:" + projects.wireSchema.version) - api(projects.wireSchemaTests) - api(projects.wireSwiftGenerator) - } -} +collectBomConstraints() extensions.configure { publications.create("maven", MavenPublication::class) {