|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.gradle.plugins.semver |
| 16 | + |
| 17 | +import java.nio.file.Files |
| 18 | +import java.nio.file.Path |
| 19 | +import java.nio.file.Paths |
| 20 | +import java.util.jar.JarEntry |
| 21 | +import java.util.jar.JarInputStream |
| 22 | +import org.gradle.api.DefaultTask |
| 23 | +import org.gradle.api.GradleException |
| 24 | +import org.gradle.api.provider.Property |
| 25 | +import org.gradle.api.tasks.Input |
| 26 | +import org.gradle.api.tasks.TaskAction |
| 27 | +import org.objectweb.asm.ClassReader |
| 28 | +import org.objectweb.asm.tree.ClassNode |
| 29 | + |
| 30 | +abstract class ApiDiffer : DefaultTask() { |
| 31 | + @get:Input abstract val currentJar: Property<String> |
| 32 | + |
| 33 | + @get:Input abstract val previousJar: Property<String> |
| 34 | + |
| 35 | + @get:Input abstract val version: Property<String> |
| 36 | + |
| 37 | + @get:Input abstract val previousVersionString: Property<String> |
| 38 | + |
| 39 | + private val CLASS_EXTENSION = ".class" |
| 40 | + |
| 41 | + @TaskAction |
| 42 | + fun run() { |
| 43 | + val (pMajor, pMinor, _) = previousVersionString.get().split(".") |
| 44 | + val (major, minor, _) = version.get().split(".") |
| 45 | + val curVersionDelta: VersionDelta = |
| 46 | + if (major > pMajor) VersionDelta.MAJOR |
| 47 | + else if (minor > pMinor) VersionDelta.MINOR else VersionDelta.PATCH |
| 48 | + val afterJar = readApi(currentJar.get()) |
| 49 | + val beforeJar = readApi(previousJar.get()) |
| 50 | + val classKeys = afterJar.keys union beforeJar.keys |
| 51 | + val apiDeltas = |
| 52 | + classKeys |
| 53 | + .map { className -> Pair(beforeJar.get(className), afterJar.get(className)) } |
| 54 | + .flatMap { (before, after) -> |
| 55 | + DeltaType.values().flatMap { it.getViolations(before, after) } |
| 56 | + } |
| 57 | + val deltaViolations: List<Delta> = |
| 58 | + if (curVersionDelta == VersionDelta.MINOR) |
| 59 | + apiDeltas.filter { it.versionDelta == VersionDelta.MAJOR } |
| 60 | + else if (curVersionDelta == VersionDelta.PATCH) apiDeltas else mutableListOf() |
| 61 | + if (!apiDeltas.isEmpty()) { |
| 62 | + val printString = |
| 63 | + apiDeltas.joinToString( |
| 64 | + prefix = |
| 65 | + "Here is a list of all the minor/major version bump changes which are made since the last release.\n", |
| 66 | + separator = "\n" |
| 67 | + ) { |
| 68 | + "[${it.versionDelta}] ${it.description}" |
| 69 | + } |
| 70 | + println(printString) |
| 71 | + } |
| 72 | + if (!deltaViolations.isEmpty()) { |
| 73 | + val outputString = |
| 74 | + deltaViolations.joinToString( |
| 75 | + prefix = |
| 76 | + "Here is a list of all the violations which needs to be fixed before we could release.\n", |
| 77 | + separator = "\n" |
| 78 | + ) { |
| 79 | + "[${it.versionDelta}] ${it.description}" |
| 80 | + } |
| 81 | + throw GradleException(outputString) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun readApi(jarPath: String): Map<String, ClassInfo> { |
| 86 | + val classes: Map<String, ClassNode> = readClassNodes(Paths.get(jarPath)) |
| 87 | + return classes.entries.associate { (key, value) -> key to ClassInfo(value, classes) } |
| 88 | + } |
| 89 | + |
| 90 | + /** Returns true if the class is local or anonymous. */ |
| 91 | + private fun isLocalOrAnonymous(classNode: ClassNode): Boolean { |
| 92 | + // JVMS 4.7.7 says a class has an EnclosingMethod attribute iff it is local or anonymous. |
| 93 | + // ASM sets the "enclosing class" only if an EnclosingMethod attribute is present, so this |
| 94 | + // this test will not include named inner classes even though they have enclosing classes. |
| 95 | + return classNode.outerClass != null |
| 96 | + } |
| 97 | + |
| 98 | + private fun getUnqualifiedClassname(classNodeName: String): String { |
| 99 | + // Class names may be "/" or "." separated depending on context. Normalize to "/" |
| 100 | + val normalizedPath = classNodeName.replace(".", "/") |
| 101 | + val withoutPackage = normalizedPath.substring(normalizedPath.lastIndexOf('/') + 1) |
| 102 | + return withoutPackage.substring(withoutPackage.lastIndexOf('$') + 1) |
| 103 | + } |
| 104 | + |
| 105 | + fun readClassNodes(jar: Path): Map<String, ClassNode> { |
| 106 | + val classes: MutableMap<String, ClassNode> = LinkedHashMap() |
| 107 | + val inputStream = Files.newInputStream(jar) |
| 108 | + val jis = JarInputStream(inputStream) |
| 109 | + var je: JarEntry? = null |
| 110 | + while (true) { |
| 111 | + je = jis.nextJarEntry |
| 112 | + if (je == null) { |
| 113 | + break |
| 114 | + } |
| 115 | + if (!je.name.endsWith(CLASS_EXTENSION)) { |
| 116 | + continue |
| 117 | + } |
| 118 | + val expectedName = je.name.substring(0, je.name.length - CLASS_EXTENSION.length) |
| 119 | + val classNode: ClassNode = ClassNode() |
| 120 | + |
| 121 | + ClassReader(jis).accept(classNode, ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) |
| 122 | + |
| 123 | + if (!classNode.name.equals(expectedName)) { |
| 124 | + logger.error( |
| 125 | + "Classnode doesn't match expected name ${classNode.name}. This is not a valid jar." |
| 126 | + ) |
| 127 | + continue |
| 128 | + } |
| 129 | + // Skip classes that appear to be obfuscated. |
| 130 | + if (UtilityClass.isObfuscatedSymbol(getUnqualifiedClassname(classNode.name))) { |
| 131 | + continue |
| 132 | + } |
| 133 | + // Skip local and anonymous classes. |
| 134 | + if (isLocalOrAnonymous(classNode)) { |
| 135 | + continue |
| 136 | + } |
| 137 | + // Skip private nested classes |
| 138 | + if (!classes.containsKey(classNode.name)) { |
| 139 | + classes.put(classNode.name, classNode) |
| 140 | + } else { |
| 141 | + project.logger.info("Duplicate class seen: ${classNode.name}") |
| 142 | + } |
| 143 | + } |
| 144 | + return classes |
| 145 | + } |
| 146 | +} |
0 commit comments