Skip to content

Commit b2055ce

Browse files
committed
feat: Include or exclude patches by their index in relation to supplied patch bundles
1 parent 4fc4208 commit b2055ce

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

docs/1_usage.md

+15
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,25 @@ ReVanced CLI is divided into the following fundamental commands:
8787
> adb install app.apk
8888
> ```
8989
90+
> [!NOTE]
91+
> You can use the option `--ii` to include or `--ie` to exclude
92+
> patches by their index in relation to supplied patch bundles,
93+
> similarly to the option `--include` and `--exclude`.
94+
>
95+
> This is useful in case two patches have the same name, and you need to include or exclude one of them.
96+
> The index of a patch is calculated by the position of the patch in the list of patches
97+
> from patch bundles supplied using the option `--patch-bundle`.
98+
>
99+
> You can list all patches with their indices using the command `list-patches`.
100+
>
101+
> Keep in mind, that the indices can change based on the order of the patch bundles supplied,
102+
> as well if the patch bundles are updated, because patches can be added or removed.
103+
90104
```bash
91105
java -jar revanced-cli.jar patch \
92106
--patch-bundle revanced-patches.jar \
93107
--include "Some patch" \
108+
--ii 123 \
94109
--exclude "Some other patch" \
95110
--out patched-app.apk \
96111
--device-serial <device-serial> \

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

+33-21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ internal object ListPatchesCommand : Runnable {
4949
)
5050
private var withUniversalPatches: Boolean = true
5151

52+
@Option(
53+
names = ["-i", "--index"],
54+
description = ["List the index of of each patch in relation to the supplied patch bundles."],
55+
showDefaultValue = ALWAYS
56+
)
57+
private var withIndex: Boolean = true
58+
5259
@Option(
5360
names = ["-f", "--filter-package-name"], description = ["Filter patches by package name."]
5461
)
@@ -77,34 +84,39 @@ internal object ListPatchesCommand : Runnable {
7784
}
7885
}
7986

80-
fun Patch<*>.buildString() = buildString {
81-
append("Name: $name")
82-
83-
if (withDescriptions) append("\nDescription: $description")
84-
85-
if (withOptions && options.isNotEmpty()) {
86-
appendLine("\nOptions:")
87-
append(
88-
options.values.joinToString("\n\n") { option ->
89-
option.buildString()
90-
}.prependIndent("\t")
91-
)
92-
}
93-
94-
if (withPackages && compatiblePackages != null) {
95-
appendLine("\nCompatible packages:")
96-
append(
97-
compatiblePackages!!.joinToString("\n") { it.buildString() }.prependIndent("\t")
98-
)
87+
fun IndexedValue<Patch<*>>.buildString() = let { (index, patch) ->
88+
buildString {
89+
if (withIndex) appendLine("Index: $index")
90+
91+
append("Name: ${patch.name}")
92+
93+
if (withDescriptions) append("\nDescription: ${patch.description}")
94+
95+
if (withOptions && patch.options.isNotEmpty()) {
96+
appendLine("\nOptions:")
97+
append(
98+
patch.options.values.joinToString("\n\n") { option ->
99+
option.buildString()
100+
}.prependIndent("\t")
101+
)
102+
}
103+
104+
if (withPackages && patch.compatiblePackages != null) {
105+
appendLine("\nCompatible packages:")
106+
append(patch.compatiblePackages!!.joinToString("\n") {
107+
it.buildString()
108+
}.prependIndent("\t"))
109+
}
99110
}
100111
}
101112

102113
fun Patch<*>.filterCompatiblePackages(name: String) = compatiblePackages?.any { it.name == name }
103114
?: withUniversalPatches
104115

105-
val patches = PatchBundleLoader.Jar(*patchBundles)
116+
val patches = PatchBundleLoader.Jar(*patchBundles).withIndex().toList()
106117

107-
val filtered = packageName?.let { patches.filter { patch -> patch.filterCompatiblePackages(it) } } ?: patches
118+
val filtered =
119+
packageName?.let { patches.filter { (_, patch) -> patch.filterCompatiblePackages(it) } } ?: patches
108120

109121
if (filtered.isNotEmpty()) logger.info(filtered.joinToString("\n\n") { it.buildString() })
110122
}

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,23 @@ internal object PatchCommand : Runnable {
3939
)
4040
private var includedPatches = arrayOf<String>()
4141

42+
@CommandLine.Option(
43+
names = ["--ii"],
44+
description = ["List of patches to include by their index in relation to the supplied patch bundles."]
45+
)
46+
private var includedPatchesByIndex = arrayOf<Int>()
47+
4248
@CommandLine.Option(
4349
names = ["-e", "--exclude"], description = ["List of patches to exclude."]
4450
)
4551
private var excludedPatches = arrayOf<String>()
4652

53+
@CommandLine.Option(
54+
names = ["--ei"],
55+
description = ["List of patches to exclude by their index in relation to the supplied patch bundles."]
56+
)
57+
private var excludedPatchesByIndex = arrayOf<Int>()
58+
4759
@CommandLine.Option(
4860
names = ["--options"], description = ["Path to patch options JSON file."], showDefaultValue = ALWAYS
4961
)
@@ -283,10 +295,10 @@ internal object PatchCommand : Runnable {
283295
val packageName = context.packageMetadata.packageName
284296
val packageVersion = context.packageMetadata.packageVersion
285297

286-
patches.forEach patch@{ patch ->
298+
patches.withIndex().forEach patch@{ (i, patch) ->
287299
val patchName = patch.name!!
288300

289-
val explicitlyExcluded = excludedPatches.contains(patchName)
301+
val explicitlyExcluded = excludedPatches.contains(patchName) || excludedPatchesByIndex.contains(i)
290302
if (explicitlyExcluded) return@patch logger.info("Excluding $patchName")
291303

292304
// Make sure the patch is compatible with the supplied APK files package name and version.
@@ -314,7 +326,7 @@ internal object PatchCommand : Runnable {
314326
// If the patch is implicitly used, it will be only included if [exclusive] is false.
315327
val implicitlyIncluded = !exclusive && patch.use
316328
// If the patch is explicitly used, it will be included even if [exclusive] is false.
317-
val explicitlyIncluded = includedPatches.contains(patchName)
329+
val explicitlyIncluded = includedPatches.contains(patchName) || includedPatchesByIndex.contains(i)
318330

319331
val included = implicitlyIncluded || explicitlyIncluded
320332
if (!included) return@patch logger.info("$patchName excluded") // Case 1.

0 commit comments

Comments
 (0)