|
| 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 | +} |
0 commit comments