Skip to content

Commit 7202f9d

Browse files
authored
Merge branch 'master' into dconeybe/SpecTestPortJsSdkPr7021
2 parents c76b67c + dfad414 commit 7202f9d

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

buildSrc/src/main/java/com/google/firebase/gradle/plugins/BaseFirebaseLibraryPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
7878

7979
protected fun getIsPomValidTask(project: Project, firebaseLibrary: FirebaseLibraryExtension) {
8080
project.tasks.register<PomValidator>("isPomDependencyValid") {
81-
pomFilePath.value(project.file("build/publications/mavenAar/pom-default.xml"))
82-
groupId.value(firebaseLibrary.groupId.get())
83-
artifactId.value(firebaseLibrary.artifactId.get())
81+
pomFile.set(project.layout.buildDirectory.file("publications/mavenAar/pom-default.xml"))
82+
groupId.set(firebaseLibrary.groupId.get())
83+
artifactId.set(firebaseLibrary.artifactId.get())
8484
dependsOn("generatePomFileForMavenAarPublication")
8585
}
8686
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/PomValidator.kt

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,85 @@
1414

1515
package com.google.firebase.gradle.plugins
1616

17-
import java.io.File
1817
import java.net.URL
19-
import javax.xml.parsers.DocumentBuilder
2018
import javax.xml.parsers.DocumentBuilderFactory
2119
import org.gradle.api.DefaultTask
2220
import org.gradle.api.GradleException
21+
import org.gradle.api.file.RegularFileProperty
2322
import org.gradle.api.provider.Property
2423
import org.gradle.api.tasks.Input
2524
import org.gradle.api.tasks.InputFile
2625
import org.gradle.api.tasks.TaskAction
27-
import org.w3c.dom.Document
2826
import org.w3c.dom.Element
29-
import org.w3c.dom.Node
30-
import org.w3c.dom.NodeList
3127

28+
/**
29+
* Ensures that pom dependencies are not accidently downgraded.
30+
*
31+
* Compares the latest pom at gmaven for the given artifact with the one generate for the current
32+
* release.
33+
*
34+
* @property pomFile The pom file for the current release
35+
* @property artifactId The artifactId for the pom parent
36+
* @property groupId The groupId for the pom parent
37+
* @throws GradleException if a dependency is found with a degraded version
38+
*/
3239
abstract class PomValidator : DefaultTask() {
33-
@get:InputFile abstract val pomFilePath: Property<File>
40+
@get:InputFile abstract val pomFile: RegularFileProperty
3441
@get:Input abstract val artifactId: Property<String>
3542
@get:Input abstract val groupId: Property<String>
3643

3744
@TaskAction
3845
fun run() {
39-
val gMavenHelper = GmavenHelper(groupId.get(), artifactId.get())
40-
val latestReleasedVersion = gMavenHelper.getLatestReleasedVersion()
41-
val releasedVersionPomUrl = gMavenHelper.getPomFileForVersion(latestReleasedVersion)
42-
var output: String = diffWithPomFileUrl(releasedVersionPomUrl).trim()
43-
if (output.isNotEmpty()) {
44-
throw GradleException("${output}\nPlease fix the above errors")
46+
var diff = diffWithPomFromURL(getLatestReleasePomUrl())
47+
48+
if (diff.isNotEmpty()) {
49+
throw GradleException("Dependency version errors found:\n${diff}")
4550
}
4651
}
4752

48-
fun getMapFromXml(pomNodeList: NodeList): Map<String, String> {
49-
val pomMap = mutableMapOf<String, String>()
50-
for (i in 0..pomNodeList.length - 1) {
51-
val node: Node = pomNodeList.item(i)
52-
if (node.getNodeType() == Node.ELEMENT_NODE) {
53-
val element = node as Element
54-
val artifact = element.getElementsByTagName("artifactId").item(0).getTextContent()
55-
val version = element.getElementsByTagName("version").item(0).getTextContent()
56-
pomMap[artifact] = version
57-
}
53+
private fun getLatestReleasePomUrl() =
54+
with(GmavenHelper(groupId.get(), artifactId.get())) {
55+
getPomFileForVersion(getLatestReleasedVersion())
5856
}
59-
return pomMap
60-
}
6157

62-
fun diffWithPomFileUrl(pomUrl: String): String {
63-
val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
64-
val oldPomBuilder: DocumentBuilder = factory.newDocumentBuilder()
65-
val oldPomDoc: Document = oldPomBuilder.parse(URL(pomUrl).openStream())
66-
val currentPomBuilder: DocumentBuilder = factory.newDocumentBuilder()
67-
val currentPomDoc: Document = currentPomBuilder.parse(pomFilePath.get())
68-
val oldPomMap = getMapFromXml(oldPomDoc.getElementsByTagName("dependency"))
69-
val currentPomMap = getMapFromXml(currentPomDoc.getElementsByTagName("dependency"))
58+
private fun getMapOfDependencies(doc: Element) =
59+
doc
60+
.findElementsByTag("dependency")
61+
.associate { it.textByTag("artifactId") to it.textByTag("version") }
62+
.filterKeys { it !in IGNORED_DEPENDENCIES }
63+
.mapValues {
64+
ModuleVersion.fromStringOrNull(it.value)
65+
?: throw RuntimeException("Invalid module version found for '${it.key}': ${it.value}")
66+
}
67+
68+
data class DependencyDiff(
69+
val artifactId: String,
70+
val oldVersion: ModuleVersion,
71+
val currentVersion: ModuleVersion
72+
)
7073

71-
return currentPomMap
72-
.filter {
73-
(oldPomMap.get(it.key) != null) && (oldPomMap.get(it.key)!!.trim()) > it.value.trim()
74+
fun diffWithPomFromURL(url: String): String {
75+
val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
76+
77+
val oldPom = documentBuilder.parse(URL(url).openStream())
78+
val currentPom = documentBuilder.parse(pomFile.get().asFile)
79+
80+
val oldDependencies = getMapOfDependencies(oldPom.documentElement)
81+
val currentDependencies = getMapOfDependencies(currentPom.documentElement)
82+
83+
return currentDependencies
84+
.map { DependencyDiff(it.key, oldDependencies.getOrDefault(it.key, it.value), it.value) }
85+
.filter { it.oldVersion > it.currentVersion }
86+
.map {
87+
"Dependency on ${it.artifactId} has been degraded from ${it.oldVersion} to ${it.currentVersion}"
7488
}
75-
.map { "Artifacts ${it.key} has been degraded to ${it.value} from ${oldPomMap.get(it.key)}" }
7689
.joinToString("\n")
7790
}
91+
92+
companion object {
93+
val IGNORED_DEPENDENCIES =
94+
listOf(
95+
"javax.inject" // javax.inject doesn't respect SemVer and doesn't update
96+
)
97+
}
7898
}

0 commit comments

Comments
 (0)