|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + |
| 17 | +package com.google.firebase.gradle.bomgenerator |
| 18 | + |
| 19 | +import com.google.firebase.gradle.plugins.ModuleVersion |
| 20 | +import com.google.firebase.gradle.plugins.VersionType |
| 21 | +import com.google.firebase.gradle.plugins.createIfAbsent |
| 22 | +import com.google.firebase.gradle.plugins.datamodels.ArtifactDependency |
| 23 | +import com.google.firebase.gradle.plugins.datamodels.DependencyManagementElement |
| 24 | +import com.google.firebase.gradle.plugins.datamodels.LicenseElement |
| 25 | +import com.google.firebase.gradle.plugins.datamodels.PomElement |
| 26 | +import com.google.firebase.gradle.plugins.datamodels.fullArtifactName |
| 27 | +import com.google.firebase.gradle.plugins.datamodels.moduleVersion |
| 28 | +import com.google.firebase.gradle.plugins.diff |
| 29 | +import com.google.firebase.gradle.plugins.orEmpty |
| 30 | +import com.google.firebase.gradle.plugins.partitionNotNull |
| 31 | +import com.google.firebase.gradle.plugins.services.GMavenService |
| 32 | +import org.gradle.api.DefaultTask |
| 33 | +import org.gradle.api.file.DirectoryProperty |
| 34 | +import org.gradle.api.provider.ListProperty |
| 35 | +import org.gradle.api.provider.MapProperty |
| 36 | +import org.gradle.api.provider.Property |
| 37 | +import org.gradle.api.services.ServiceReference |
| 38 | +import org.gradle.api.tasks.Input |
| 39 | +import org.gradle.api.tasks.OutputDirectory |
| 40 | +import org.gradle.api.tasks.TaskAction |
| 41 | + |
| 42 | +/** |
| 43 | + * Generates the firebase bom, using gmaven as a source of truth for artifacts and versions. |
| 44 | + * |
| 45 | + * @see validateArtifacts |
| 46 | + * @see GenerateBomReleaseNotesTask |
| 47 | + * @see GenerateTutorialBundleTask |
| 48 | + */ |
| 49 | +abstract class GenerateBomTask : DefaultTask() { |
| 50 | + /** |
| 51 | + * Artifacts to include in the bom. |
| 52 | + * |
| 53 | + * ``` |
| 54 | + * bomArtifacts.set(listOf( |
| 55 | + * "com.google.firebase:firebase-firestore", |
| 56 | + * "com.google.firebase:firebase-storage" |
| 57 | + * )) |
| 58 | + * ``` |
| 59 | + */ |
| 60 | + @get:Input abstract val bomArtifacts: ListProperty<String> |
| 61 | + |
| 62 | + /** |
| 63 | + * Artifacts to exclude from the bom. |
| 64 | + * |
| 65 | + * These are artifacts that are under the `com.google.firebase` namespace, but are intentionally |
| 66 | + * not included in the bom. |
| 67 | + * |
| 68 | + * ``` |
| 69 | + * bomArtifacts.set(listOf( |
| 70 | + * "com.google.firebase:crashlytics", |
| 71 | + * "com.google.firebase:crash-plugin" |
| 72 | + * )) |
| 73 | + * ``` |
| 74 | + */ |
| 75 | + @get:Input abstract val ignoredArtifacts: ListProperty<String> |
| 76 | + |
| 77 | + /** |
| 78 | + * Optional map of versions to use instead of the versions on gmaven. |
| 79 | + * |
| 80 | + * ``` |
| 81 | + * versionOverrides.set(mapOf( |
| 82 | + * "com.google.firebase:firebase-firestore" to "10.0.0" |
| 83 | + * )) |
| 84 | + * ``` |
| 85 | + */ |
| 86 | + @get:Input abstract val versionOverrides: MapProperty<String, String> |
| 87 | + |
| 88 | + /** Directory to save the bom under. */ |
| 89 | + @get:OutputDirectory abstract val outputDirectory: DirectoryProperty |
| 90 | + |
| 91 | + @get:ServiceReference("gmaven") abstract val gmaven: Property<GMavenService> |
| 92 | + |
| 93 | + @TaskAction |
| 94 | + fun generate() { |
| 95 | + val versionOverrides = versionOverrides.getOrElse(emptyMap()) |
| 96 | + |
| 97 | + val validatedArtifactsToPublish = validateArtifacts() |
| 98 | + val artifactsToPublish = |
| 99 | + validatedArtifactsToPublish.map { |
| 100 | + val version = versionOverrides[it.fullArtifactName] ?: it.version |
| 101 | + logger.debug("Using ${it.fullArtifactName} with version $version") |
| 102 | + |
| 103 | + it.copy(version = version) |
| 104 | + } |
| 105 | + |
| 106 | + val newVersion = determineNewBomVersion(artifactsToPublish) |
| 107 | + |
| 108 | + val pom = |
| 109 | + PomElement( |
| 110 | + namespace = "http://maven.apache.org/POM/4.0.0", |
| 111 | + schema = "http://www.w3.org/2001/XMLSchema-instance", |
| 112 | + schemaLocation = |
| 113 | + "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd", |
| 114 | + modelVersion = "4.0.0", |
| 115 | + groupId = "com.google.firebase", |
| 116 | + artifactId = "firebase-bom", |
| 117 | + version = newVersion.toString(), |
| 118 | + packaging = "pom", |
| 119 | + licenses = |
| 120 | + listOf( |
| 121 | + LicenseElement( |
| 122 | + name = "The Apache Software License, Version 2.0", |
| 123 | + url = "http://www.apache.org/licenses/LICENSE-2.0.txt", |
| 124 | + distribution = "repo", |
| 125 | + ) |
| 126 | + ), |
| 127 | + dependencyManagement = DependencyManagementElement(artifactsToPublish), |
| 128 | + ) |
| 129 | + |
| 130 | + val bomFile = |
| 131 | + outputDirectory.file( |
| 132 | + "com/google/firebase/firebase-bom/$newVersion/firebase-bom-$newVersion.pom" |
| 133 | + ) |
| 134 | + |
| 135 | + pom.toFile(bomFile.get().asFile.createIfAbsent()) |
| 136 | + } |
| 137 | + |
| 138 | + private fun determineNewBomVersion( |
| 139 | + releasingDependencies: List<ArtifactDependency> |
| 140 | + ): ModuleVersion { |
| 141 | + logger.info("Determining the new bom version") |
| 142 | + |
| 143 | + val oldBom = gmaven.get().latestPom("com.google.firebase", "firebase-bom") |
| 144 | + val oldBomVersion = ModuleVersion.fromString(oldBom.artifactId, oldBom.version) |
| 145 | + |
| 146 | + val oldBomDependencies = oldBom.dependencyManagement?.dependencies.orEmpty() |
| 147 | + val changedDependencies = oldBomDependencies.diff(releasingDependencies) |
| 148 | + |
| 149 | + val versionBumps = |
| 150 | + changedDependencies.mapNotNull { (old, new) -> |
| 151 | + if (old == null) { |
| 152 | + logger.warn("Dependency was added: ${new?.fullArtifactName}") |
| 153 | + |
| 154 | + VersionType.MINOR |
| 155 | + } else if (new === null) { |
| 156 | + logger.warn("Dependency was removed: ${old.fullArtifactName}") |
| 157 | + |
| 158 | + VersionType.MAJOR |
| 159 | + } else { |
| 160 | + old.moduleVersion.bumpFrom(new.moduleVersion) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + val finalBump = versionBumps.minOrNull() |
| 165 | + return oldBomVersion.bump(finalBump) |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Validates that the provided bom artifacts satisfy the following constraints: |
| 170 | + * - All are released and live on gmaven. |
| 171 | + * - They include _all_ of the firebase artifacts on gmaven, unless they're specified in |
| 172 | + * [ignoredArtifacts].+ |
| 173 | + * |
| 174 | + * @return The validated artifacts to release. |
| 175 | + * @throws RuntimeException If any of the validations fail. |
| 176 | + */ |
| 177 | + private fun validateArtifacts(): List<ArtifactDependency> { |
| 178 | + logger.info("Validating bom artifacts") |
| 179 | + |
| 180 | + val firebaseArtifacts = bomArtifacts.get().toSet() |
| 181 | + val ignoredArtifacts = ignoredArtifacts.orEmpty().toSet() |
| 182 | + |
| 183 | + val allFirebaseArtifacts = |
| 184 | + gmaven |
| 185 | + .get() |
| 186 | + .groupIndex("com.google.firebase") |
| 187 | + .map { "${it.groupId}:${it.artifactId}" } |
| 188 | + .toSet() |
| 189 | + |
| 190 | + val (released, unreleased) = |
| 191 | + firebaseArtifacts |
| 192 | + .associateWith { gmaven.get().groupIndexArtifactOrNull(it) } |
| 193 | + .partitionNotNull() |
| 194 | + |
| 195 | + if (unreleased.isNotEmpty()) { |
| 196 | + throw RuntimeException( |
| 197 | + """ |
| 198 | + |Some artifacts required for bom generation are not live on gmaven yet: |
| 199 | + |${unreleased.joinToString("\n")} |
| 200 | + """ |
| 201 | + .trimMargin() |
| 202 | + ) |
| 203 | + } |
| 204 | + |
| 205 | + val requiredArtifacts = allFirebaseArtifacts - ignoredArtifacts |
| 206 | + val missingArtifacts = requiredArtifacts - firebaseArtifacts |
| 207 | + if (missingArtifacts.isNotEmpty()) { |
| 208 | + throw RuntimeException( |
| 209 | + """ |
| 210 | + |There are Firebase artifacts missing from the provided bom artifacts. |
| 211 | + |Add the artifacts to the ignoredArtifacts property to ignore them or to the bomArtifacts property to include them in the bom. |
| 212 | + |Dependencies missing: |
| 213 | + |${missingArtifacts.joinToString("\n")} |
| 214 | + """ |
| 215 | + .trimMargin() |
| 216 | + ) |
| 217 | + } |
| 218 | + |
| 219 | + return released.values.map { it.toArtifactDependency() } |
| 220 | + } |
| 221 | +} |
0 commit comments