|
| 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.plugins.datamodels |
| 18 | + |
| 19 | +import com.google.firebase.gradle.plugins.ModuleVersion |
| 20 | +import java.io.File |
| 21 | +import javax.xml.parsers.DocumentBuilderFactory |
| 22 | +import kotlinx.serialization.Serializable |
| 23 | +import kotlinx.serialization.encodeToString |
| 24 | +import nl.adaptivity.xmlutil.XmlDeclMode |
| 25 | +import nl.adaptivity.xmlutil.newReader |
| 26 | +import nl.adaptivity.xmlutil.serialization.XML |
| 27 | +import nl.adaptivity.xmlutil.serialization.XmlChildrenName |
| 28 | +import nl.adaptivity.xmlutil.serialization.XmlElement |
| 29 | +import nl.adaptivity.xmlutil.serialization.XmlSerialName |
| 30 | +import nl.adaptivity.xmlutil.xmlStreaming |
| 31 | +import org.w3c.dom.Element |
| 32 | + |
| 33 | +/** |
| 34 | + * Representation of a `<license />` element in a a pom file. |
| 35 | + * |
| 36 | + * @see PomElement |
| 37 | + */ |
| 38 | +@Serializable |
| 39 | +@XmlSerialName("license") |
| 40 | +data class LicenseElement( |
| 41 | + @XmlElement val name: String, |
| 42 | + @XmlElement val url: String? = null, |
| 43 | + @XmlElement val distribution: String? = null, |
| 44 | +) |
| 45 | + |
| 46 | +/** |
| 47 | + * Representation of an `<scm />` element in a a pom file. |
| 48 | + * |
| 49 | + * @see PomElement |
| 50 | + */ |
| 51 | +@Serializable |
| 52 | +@XmlSerialName("scm") |
| 53 | +data class SourceControlManagement(@XmlElement val connection: String, @XmlElement val url: String) |
| 54 | + |
| 55 | +/** |
| 56 | + * Representation of a `<dependency />` element in a pom file. |
| 57 | + * |
| 58 | + * @see PomElement |
| 59 | + */ |
| 60 | +@Serializable |
| 61 | +@XmlSerialName("dependency") |
| 62 | +data class ArtifactDependency( |
| 63 | + @XmlElement val groupId: String, |
| 64 | + @XmlElement val artifactId: String, |
| 65 | + // Can be null if the artifact derives its version from a bom |
| 66 | + @XmlElement val version: String? = null, |
| 67 | + @XmlElement val type: String? = null, |
| 68 | + @XmlElement val scope: String? = null, |
| 69 | +) { |
| 70 | + /** |
| 71 | + * Returns the artifact dependency as a a gradle dependency string. |
| 72 | + * |
| 73 | + * ``` |
| 74 | + * implementation("com.google.firebase:firebase-firestore:1.0.0") |
| 75 | + * ``` |
| 76 | + * |
| 77 | + * @see configuration |
| 78 | + * @see simpleDepString |
| 79 | + */ |
| 80 | + override fun toString() = "$configuration(\"$simpleDepString\")" |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * The artifact type of this dependency, or the default inferred by gradle. |
| 85 | + * |
| 86 | + * We use a separate variable instead of inferring the default in the constructor so we can |
| 87 | + * serialize instances of [ArtifactDependency] that should specifically _not_ have a type in the |
| 88 | + * output (like in [DependencyManagementElement] instances). |
| 89 | + */ |
| 90 | +val ArtifactDependency.typeOrDefault: String |
| 91 | + get() = type ?: "jar" |
| 92 | + |
| 93 | +/** |
| 94 | + * The artifact scope of this dependency, or the default inferred by gradle. |
| 95 | + * |
| 96 | + * We use a separate variable instead of inferring the default in the constructor so we can |
| 97 | + * serialize instances of [ArtifactDependency] that should specifically _not_ have a scope in the |
| 98 | + * output (like in [DependencyManagementElement] instances). |
| 99 | + */ |
| 100 | +val ArtifactDependency.scopeOrDefault: String |
| 101 | + get() = scope ?: "compile" |
| 102 | + |
| 103 | +/** |
| 104 | + * The [version][ArtifactDependency.version] represented as a [ModuleVersion]. |
| 105 | + * |
| 106 | + * @throws RuntimeException if the version isn't valid semver, or it's missing. |
| 107 | + */ |
| 108 | +val ArtifactDependency.moduleVersion: ModuleVersion |
| 109 | + get() = |
| 110 | + version?.let { ModuleVersion.fromString(artifactId, it) } |
| 111 | + ?: throw RuntimeException( |
| 112 | + "Missing required version property for artifact dependency: $artifactId" |
| 113 | + ) |
| 114 | + |
| 115 | +/** |
| 116 | + * The fully qualified name of the artifact. |
| 117 | + * |
| 118 | + * Shorthand for: |
| 119 | + * ``` |
| 120 | + * "${artifact.groupId}:${artifact.artifactId}" |
| 121 | + * ``` |
| 122 | + */ |
| 123 | +val ArtifactDependency.fullArtifactName: String |
| 124 | + get() = "$groupId:$artifactId" |
| 125 | + |
| 126 | +/** |
| 127 | + * A string representing the dependency as a maven artifact marker. |
| 128 | + * |
| 129 | + * ``` |
| 130 | + * "com.google.firebase:firebase-common:21.0.0" |
| 131 | + * ``` |
| 132 | + */ |
| 133 | +val ArtifactDependency.simpleDepString: String |
| 134 | + get() = "$fullArtifactName${version?.let { ":$it" } ?: ""}" |
| 135 | + |
| 136 | +/** The gradle configuration that this dependency would apply to (eg; `api` or `implementation`). */ |
| 137 | +val ArtifactDependency.configuration: String |
| 138 | + get() = if (scopeOrDefault == "compile") "api" else "implementation" |
| 139 | + |
| 140 | +@Serializable |
| 141 | +@XmlSerialName("dependencyManagement") |
| 142 | +data class DependencyManagementElement( |
| 143 | + @XmlChildrenName("dependency") val dependencies: List<ArtifactDependency>? = null |
| 144 | +) |
| 145 | + |
| 146 | +/** Representation of a `<project />` element within a `pom.xml` file. */ |
| 147 | +@Serializable |
| 148 | +@XmlSerialName("project") |
| 149 | +data class PomElement( |
| 150 | + @XmlSerialName("xmlns") val namespace: String? = null, |
| 151 | + @XmlSerialName("xmlns:xsi") val schema: String? = null, |
| 152 | + @XmlSerialName("xsi:schemaLocation") val schemaLocation: String? = null, |
| 153 | + @XmlElement val modelVersion: String, |
| 154 | + @XmlElement val groupId: String, |
| 155 | + @XmlElement val artifactId: String, |
| 156 | + @XmlElement val version: String, |
| 157 | + @XmlElement val packaging: String? = null, |
| 158 | + @XmlChildrenName("licenses") val licenses: List<LicenseElement>? = null, |
| 159 | + @XmlElement val scm: SourceControlManagement? = null, |
| 160 | + @XmlElement val dependencyManagement: DependencyManagementElement? = null, |
| 161 | + @XmlChildrenName("dependency") val dependencies: List<ArtifactDependency>? = null, |
| 162 | +) { |
| 163 | + /** |
| 164 | + * Serializes this pom element into a valid XML element and saves it to the specified [file]. |
| 165 | + * |
| 166 | + * @param file Where to save the serialized pom to |
| 167 | + * @return The provided file, for chaining purposes. |
| 168 | + * @see fromFile |
| 169 | + */ |
| 170 | + fun toFile(file: File): File { |
| 171 | + val xmlWriter = XML { |
| 172 | + indent = 2 |
| 173 | + xmlDeclMode = XmlDeclMode.None |
| 174 | + } |
| 175 | + file.writeText(xmlWriter.encodeToString(this)) |
| 176 | + return file |
| 177 | + } |
| 178 | + |
| 179 | + companion object { |
| 180 | + /** |
| 181 | + * Deserializes a [PomElement] from a `pom.xml` file. |
| 182 | + * |
| 183 | + * @param file The file that contains the pom element. |
| 184 | + * @return The deserialized [PomElement] |
| 185 | + * @see toFile |
| 186 | + * @see fromElement |
| 187 | + */ |
| 188 | + fun fromFile(file: File): PomElement = |
| 189 | + fromElement( |
| 190 | + DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file).documentElement |
| 191 | + ) |
| 192 | + |
| 193 | + /** |
| 194 | + * Deserializes a [PomElement] from a document [Element]. |
| 195 | + * |
| 196 | + * @param element The HTML element representing the pom element. |
| 197 | + * @return The deserialized [PomElement] |
| 198 | + * @see fromFile |
| 199 | + */ |
| 200 | + fun fromElement(element: Element): PomElement = |
| 201 | + XML.decodeFromReader(xmlStreaming.newReader(element)) |
| 202 | + } |
| 203 | +} |
0 commit comments