forked from ReVanced/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add function to get the most common compatible version
This adds a function to get the version that is most common for a given package name in a supplied set of patches.
- Loading branch information
Showing
6 changed files
with
165 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 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
28 changes: 28 additions & 0 deletions
28
revanced-lib/src/main/kotlin/app/revanced/lib/PatchUtils.kt
This file contains 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,28 @@ | ||
package app.revanced.lib | ||
|
||
import app.revanced.patcher.PatchSet | ||
|
||
/** | ||
* Utility functions for working with patches. | ||
*/ | ||
@Suppress("MemberVisibilityCanBePrivate", "unused") | ||
object PatchUtils { | ||
/** | ||
* Get the version that is most common for [packageName] in the supplied set of [patches]. | ||
* | ||
* @param patches The set of patches to check. | ||
* @param packageName The name of the compatible package. | ||
* @return The most common version of. | ||
*/ | ||
fun getMostCommonCompatibleVersion(patches: PatchSet, packageName: String) = patches | ||
.mapNotNull { | ||
// Map all patches to their compatible packages with version constraints. | ||
it.compatiblePackages?.firstOrNull { compatiblePackage -> | ||
compatiblePackage.name == packageName && compatiblePackage.versions?.isNotEmpty() == true | ||
} | ||
} | ||
.flatMap { it.versions!! } | ||
.groupingBy { it } | ||
.eachCount() | ||
.maxByOrNull { it.value }?.key | ||
} |
This file contains 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
50 changes: 50 additions & 0 deletions
50
revanced-lib/src/test/kotlin/app/revanced/lib/PatchUtilsTest.kt
This file contains 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,50 @@ | ||
package app.revanced.lib | ||
|
||
import app.revanced.patcher.PatchSet | ||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.patch.BytecodePatch | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal object PatchUtilsTest { | ||
@Test | ||
fun `return 'a' because it is the most common version`() { | ||
val patches = arrayOf("a", "a", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d") | ||
.map { version -> newPatch("some.package", version) } | ||
.toSet() | ||
|
||
assertEqualsVersion("a", patches, "some.package") | ||
} | ||
|
||
@Test | ||
fun `return null because no patches were supplied`() { | ||
assertEqualsVersion(null, emptySet<BytecodePatch>(), "some.package") | ||
} | ||
|
||
@Test | ||
fun `return null because no patch is compatible with the supplied package name`() { | ||
val patches = setOf(newPatch("other.package", "a")) | ||
|
||
assertEqualsVersion(null, patches, "other.package") | ||
} | ||
|
||
@Test | ||
fun `return null because no patch compatible package is constrained to a version`() { | ||
val patches = setOf( | ||
newPatch("other.package"), | ||
newPatch("other.package"), | ||
) | ||
|
||
assertEqualsVersion(null, patches, "other.package") | ||
} | ||
|
||
private fun assertEqualsVersion( | ||
expected: String?, patches: PatchSet, compatiblePackageName: String | ||
) = assertEquals(expected, PatchUtils.getMostCommonCompatibleVersion(patches, compatiblePackageName)) | ||
|
||
private fun newPatch(packageName: String, vararg versions: String) = object : BytecodePatch( | ||
compatiblePackages = setOf(CompatiblePackage(packageName, versions.toSet())) | ||
) { | ||
override fun execute(context: BytecodeContext) {} | ||
} | ||
} |