-
Notifications
You must be signed in to change notification settings - Fork 624
Semver check for firebase sdks #4826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
79d3950
add semver check
VinayGuthal 5624b14
added api diff
VinayGuthal d51303f
added api diff
VinayGuthal 0a0355c
update according to comments
VinayGuthal 7e00b90
update according to comments
VinayGuthal bce0bcc
fix bugs
VinayGuthal 14819c7
small fix
VinayGuthal a6052ef
fix
VinayGuthal c18a19f
update copyright
VinayGuthal 524401c
address comments
VinayGuthal f0fd310
nit
VinayGuthal 3c32637
Address comment
VinayGuthal 45af5f9
Update buildSrc/src/main/java/com/google/firebase/gradle/plugins/semv…
VinayGuthal 62e22dd
address comments
VinayGuthal 9f0a635
update
VinayGuthal f6735ee
address comments
VinayGuthal 9afe607
move functions within classes
VinayGuthal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
buildSrc/src/main/java/com/google/firebase/gradle/plugins/semver/AccessDescriptor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.gradle.plugins.semver | ||
|
||
import org.objectweb.asm.Opcodes | ||
|
||
/** | ||
* Convenience class that helps avoid confusing (if more performant) bitwise checks against {@link | ||
* Opcodes} | ||
*/ | ||
class AccessDescriptor(private val access: Int) { | ||
fun isProtected(): Boolean = accessIs(Opcodes.ACC_PROTECTED) | ||
|
||
fun isPublic(): Boolean = accessIs(Opcodes.ACC_PUBLIC) | ||
|
||
fun isStatic(): Boolean = accessIs(Opcodes.ACC_STATIC) | ||
|
||
fun isSynthetic(): Boolean = accessIs(Opcodes.ACC_SYNTHETIC) | ||
|
||
fun isBridge(): Boolean = accessIs(Opcodes.ACC_BRIDGE) | ||
|
||
fun isAbstract(): Boolean = accessIs(Opcodes.ACC_ABSTRACT) | ||
|
||
fun isFinal(): Boolean = accessIs(Opcodes.ACC_FINAL) | ||
|
||
fun isPrivate(): Boolean = !this.isProtected() && !this.isPublic() | ||
|
||
fun getVerboseDescription(): String { | ||
val outputStringList = mutableListOf<String>() | ||
if (this.isPublic()) { | ||
outputStringList.add("public") | ||
} | ||
if (this.isPrivate()) { | ||
outputStringList.add("private") | ||
} | ||
if (this.isProtected()) { | ||
outputStringList.add("protected") | ||
} | ||
if (this.isStatic()) { | ||
outputStringList.add("static") | ||
} | ||
if (this.isFinal()) { | ||
outputStringList.add("final") | ||
} | ||
if (this.isAbstract()) { | ||
outputStringList.add("abstract") | ||
} | ||
return outputStringList.joinToString(" ") | ||
} | ||
/** Returns true if the given access modifier matches the given opcode. */ | ||
fun accessIs(opcode: Int): Boolean = (access and opcode) != 0 | ||
} |
146 changes: 146 additions & 0 deletions
146
buildSrc/src/main/java/com/google/firebase/gradle/plugins/semver/ApiDiffer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.gradle.plugins.semver | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.util.jar.JarEntry | ||
import java.util.jar.JarInputStream | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.tree.ClassNode | ||
|
||
abstract class ApiDiffer : DefaultTask() { | ||
@get:Input abstract val currentJar: Property<String> | ||
|
||
@get:Input abstract val previousJar: Property<String> | ||
|
||
@get:Input abstract val version: Property<String> | ||
|
||
@get:Input abstract val previousVersionString: Property<String> | ||
|
||
private val CLASS_EXTENSION = ".class" | ||
|
||
@TaskAction | ||
fun run() { | ||
val (pMajor, pMinor, _) = previousVersionString.get().split(".") | ||
val (major, minor, _) = version.get().split(".") | ||
val curVersionDelta: VersionDelta = | ||
if (major > pMajor) VersionDelta.MAJOR | ||
else if (minor > pMinor) VersionDelta.MINOR else VersionDelta.PATCH | ||
val afterJar = readApi(currentJar.get()) | ||
val beforeJar = readApi(previousJar.get()) | ||
val classKeys = afterJar.keys union beforeJar.keys | ||
val apiDeltas = | ||
classKeys | ||
.map { className -> Pair(beforeJar.get(className), afterJar.get(className)) } | ||
.flatMap { (before, after) -> | ||
DeltaType.values().flatMap { it.getViolations(before, after) } | ||
} | ||
val deltaViolations: List<Delta> = | ||
if (curVersionDelta == VersionDelta.MINOR) | ||
apiDeltas.filter { it.versionDelta == VersionDelta.MAJOR } | ||
else if (curVersionDelta == VersionDelta.PATCH) apiDeltas else mutableListOf() | ||
if (!apiDeltas.isEmpty()) { | ||
val printString = | ||
apiDeltas.joinToString( | ||
prefix = | ||
"Here is a list of all the minor/major version bump changes which are made since the last release.\n", | ||
separator = "\n" | ||
) { | ||
"[${it.versionDelta}] ${it.description}" | ||
} | ||
println(printString) | ||
} | ||
if (!deltaViolations.isEmpty()) { | ||
val outputString = | ||
deltaViolations.joinToString( | ||
prefix = | ||
"Here is a list of all the violations which needs to be fixed before we could release.\n", | ||
separator = "\n" | ||
) { | ||
"[${it.versionDelta}] ${it.description}" | ||
} | ||
throw GradleException(outputString) | ||
} | ||
} | ||
|
||
private fun readApi(jarPath: String): Map<String, ClassInfo> { | ||
val classes: Map<String, ClassNode> = readClassNodes(Paths.get(jarPath)) | ||
return classes.entries.associate { (key, value) -> key to ClassInfo(value, classes) } | ||
} | ||
|
||
/** Returns true if the class is local or anonymous. */ | ||
private fun isLocalOrAnonymous(classNode: ClassNode): Boolean { | ||
// JVMS 4.7.7 says a class has an EnclosingMethod attribute iff it is local or anonymous. | ||
// ASM sets the "enclosing class" only if an EnclosingMethod attribute is present, so this | ||
// this test will not include named inner classes even though they have enclosing classes. | ||
return classNode.outerClass != null | ||
} | ||
|
||
private fun getUnqualifiedClassname(classNodeName: String): String { | ||
// Class names may be "/" or "." separated depending on context. Normalize to "/" | ||
val normalizedPath = classNodeName.replace(".", "/") | ||
val withoutPackage = normalizedPath.substring(normalizedPath.lastIndexOf('/') + 1) | ||
return withoutPackage.substring(withoutPackage.lastIndexOf('$') + 1) | ||
} | ||
|
||
fun readClassNodes(jar: Path): Map<String, ClassNode> { | ||
val classes: MutableMap<String, ClassNode> = LinkedHashMap() | ||
val inputStream = Files.newInputStream(jar) | ||
val jis = JarInputStream(inputStream) | ||
var je: JarEntry? = null | ||
while (true) { | ||
je = jis.nextJarEntry | ||
if (je == null) { | ||
break | ||
} | ||
if (!je.name.endsWith(CLASS_EXTENSION)) { | ||
continue | ||
} | ||
val expectedName = je.name.substring(0, je.name.length - CLASS_EXTENSION.length) | ||
val classNode: ClassNode = ClassNode() | ||
|
||
ClassReader(jis).accept(classNode, ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES) | ||
|
||
if (!classNode.name.equals(expectedName)) { | ||
logger.error( | ||
"Classnode doesn't match expected name ${classNode.name}. This is not a valid jar." | ||
) | ||
continue | ||
} | ||
// Skip classes that appear to be obfuscated. | ||
if (UtilityClass.isObfuscatedSymbol(getUnqualifiedClassname(classNode.name))) { | ||
continue | ||
} | ||
// Skip local and anonymous classes. | ||
if (isLocalOrAnonymous(classNode)) { | ||
continue | ||
} | ||
// Skip private nested classes | ||
if (!classes.containsKey(classNode.name)) { | ||
classes.put(classNode.name, classNode) | ||
} else { | ||
project.logger.info("Duplicate class seen: ${classNode.name}") | ||
} | ||
} | ||
return classes | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.