xref: /aosp_15_r20/external/okio/build-support/src/main/kotlin/bom.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2023 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import java.util.Locale
17 import org.gradle.api.Project
18 import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
19 import org.gradle.kotlin.dsl.getByType
20 import org.gradle.kotlin.dsl.withType
21 import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
22 import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
23 import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper
24 import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
25 import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
26 import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
27 import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
28 
29 /**
30  * Collect all the root project's multiplatform targets and add them to the BOM.
31  *
32  * Only published subprojects are included.
33  *
34  * This supports Kotlin/Multiplatform and Kotlin/JS subprojects.
35  */
Projectnull36 fun Project.collectBomConstraints() {
37   val bomConstraints: DependencyConstraintHandler = dependencies.constraints
38   rootProject.subprojects {
39     val subproject = this
40 
41     subproject.plugins.withId("com.vanniktech.maven.publish.base") {
42       subproject.plugins.withType<KotlinAndroidPluginWrapper> {
43         bomConstraints.api(subproject)
44       }
45 
46       subproject.plugins.withType<KotlinJsPluginWrapper> {
47         bomConstraints.api(subproject)
48       }
49 
50       subproject.plugins.withType<KotlinMultiplatformPluginWrapper> {
51         subproject.extensions.getByType<KotlinMultiplatformExtension>().targets.all {
52           bomConstraints.api(dependencyConstraint(this))
53         }
54       }
55     }
56   }
57 }
58 
59 /** Returns a string like "com.squareup.okio:okio-iosarm64:3.4.0" for this target. */
Projectnull60 private fun Project.dependencyConstraint(target: KotlinTarget): String {
61   val artifactId = when (target) {
62     is KotlinMetadataTarget -> name
63     is KotlinJsTarget -> "$name-js"
64     else -> "$name-${target.targetName.toLowerCase(Locale.ROOT)}"
65   }
66   return "$group:$artifactId:$version"
67 }
68 
apinull69 private fun DependencyConstraintHandler.api(constraintNotation: Any) =
70   add("api", constraintNotation)
71