Skip to content

Commit a974b8e

Browse files
committed
feat: Add list-versions command
1 parent 89c35ee commit a974b8e

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kotlin-test = "1.9.20"
44
kotlinx-coroutines-core = "1.7.3"
55
picocli = "4.7.3"
66
revanced-patcher = "19.0.0"
7-
revanced-library = "1.3.0"
7+
revanced-library = "1.4.0"
88

99
[libraries]
1010
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin-test" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package app.revanced.cli.command
2+
3+
import app.revanced.library.PackageName
4+
import app.revanced.library.PatchUtils
5+
import app.revanced.library.VersionMap
6+
import app.revanced.patcher.PatchBundleLoader
7+
import picocli.CommandLine
8+
import java.io.File
9+
import java.util.logging.Logger
10+
11+
@CommandLine.Command(
12+
name = "list-versions",
13+
description = [
14+
"List the most common compatible versions of apps that are compatible " +
15+
"with the patches in the supplied patch bundles.",
16+
],
17+
)
18+
internal class ListCompatibleVersions : Runnable {
19+
private val logger = Logger.getLogger(ListCompatibleVersions::class.java.name)
20+
21+
@CommandLine.Parameters(
22+
description = ["Paths to patch bundles."],
23+
arity = "1..*",
24+
)
25+
private lateinit var patchBundles: Array<File>
26+
27+
@CommandLine.Option(
28+
names = ["-f", "--filter-package-names"],
29+
description = ["Filter patches by package name."],
30+
)
31+
private var packageNames: Set<String>? = null
32+
33+
@CommandLine.Option(
34+
names = ["-u", "--count-unused-patches"],
35+
description = ["Count patches that are not used by default."],
36+
showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
37+
)
38+
private var countUnusedPatches: Boolean = false
39+
40+
override fun run() {
41+
val patches = PatchBundleLoader.Jar(*patchBundles)
42+
43+
fun VersionMap.buildVersionsString(): String {
44+
if (isEmpty()) return "Any"
45+
46+
fun buildPatchesCountString(count: Int) = if (count == 1) "1 patch" else "$count patches"
47+
48+
return entries.joinToString("\n") { (version, count) ->
49+
"$version (${buildPatchesCountString(count)})"
50+
}
51+
}
52+
53+
fun buildString(entry: Map.Entry<PackageName, VersionMap>) =
54+
buildString {
55+
val (name, versions) = entry
56+
appendLine("Package name: $name")
57+
appendLine("Most common compatible versions:")
58+
appendLine(versions.buildVersionsString().prependIndent("\t"))
59+
}
60+
61+
PatchUtils.getMostCommonCompatibleVersions(
62+
patches,
63+
packageNames,
64+
countUnusedPatches,
65+
).entries.joinToString("\n", transform = ::buildString).let(logger::info)
66+
}
67+
}

src/main/kotlin/app/revanced/cli/command/MainCommand.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ private object CLIVersionProvider : IVersionProvider {
3333
mixinStandardHelpOptions = true,
3434
versionProvider = CLIVersionProvider::class,
3535
subcommands = [
36-
ListPatchesCommand::class,
3736
PatchCommand::class,
3837
OptionsCommand::class,
38+
ListPatchesCommand::class,
39+
ListCompatibleVersions::class,
3940
UtilityCommand::class,
4041
],
4142
)

0 commit comments

Comments
 (0)