Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
feat: Include or exclude patches by their index in relation to suppli…
Browse files Browse the repository at this point in the history
…ed patch bundles
  • Loading branch information
oSumAtrIX committed Nov 3, 2023
1 parent 4fc4208 commit b2055ce
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
15 changes: 15 additions & 0 deletions docs/1_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,25 @@ ReVanced CLI is divided into the following fundamental commands:
> adb install app.apk
> ```
> [!NOTE]
> You can use the option `--ii` to include or `--ie` to exclude
> patches by their index in relation to supplied patch bundles,
> similarly to the option `--include` and `--exclude`.
>
> This is useful in case two patches have the same name, and you need to include or exclude one of them.
> The index of a patch is calculated by the position of the patch in the list of patches
> from patch bundles supplied using the option `--patch-bundle`.
>
> You can list all patches with their indices using the command `list-patches`.
>
> Keep in mind, that the indices can change based on the order of the patch bundles supplied,
> as well if the patch bundles are updated, because patches can be added or removed.
```bash
java -jar revanced-cli.jar patch \
--patch-bundle revanced-patches.jar \
--include "Some patch" \
--ii 123 \
--exclude "Some other patch" \
--out patched-app.apk \
--device-serial <device-serial> \
Expand Down
54 changes: 33 additions & 21 deletions src/main/kotlin/app/revanced/cli/command/ListPatchesCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ internal object ListPatchesCommand : Runnable {
)
private var withUniversalPatches: Boolean = true

@Option(
names = ["-i", "--index"],
description = ["List the index of of each patch in relation to the supplied patch bundles."],
showDefaultValue = ALWAYS
)
private var withIndex: Boolean = true

@Option(
names = ["-f", "--filter-package-name"], description = ["Filter patches by package name."]
)
Expand Down Expand Up @@ -77,34 +84,39 @@ internal object ListPatchesCommand : Runnable {
}
}

fun Patch<*>.buildString() = buildString {
append("Name: $name")

if (withDescriptions) append("\nDescription: $description")

if (withOptions && options.isNotEmpty()) {
appendLine("\nOptions:")
append(
options.values.joinToString("\n\n") { option ->
option.buildString()
}.prependIndent("\t")
)
}

if (withPackages && compatiblePackages != null) {
appendLine("\nCompatible packages:")
append(
compatiblePackages!!.joinToString("\n") { it.buildString() }.prependIndent("\t")
)
fun IndexedValue<Patch<*>>.buildString() = let { (index, patch) ->
buildString {
if (withIndex) appendLine("Index: $index")

append("Name: ${patch.name}")

if (withDescriptions) append("\nDescription: ${patch.description}")

if (withOptions && patch.options.isNotEmpty()) {
appendLine("\nOptions:")
append(
patch.options.values.joinToString("\n\n") { option ->
option.buildString()
}.prependIndent("\t")
)
}

if (withPackages && patch.compatiblePackages != null) {
appendLine("\nCompatible packages:")
append(patch.compatiblePackages!!.joinToString("\n") {
it.buildString()
}.prependIndent("\t"))
}
}
}

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

val patches = PatchBundleLoader.Jar(*patchBundles)
val patches = PatchBundleLoader.Jar(*patchBundles).withIndex().toList()

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

if (filtered.isNotEmpty()) logger.info(filtered.joinToString("\n\n") { it.buildString() })
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/app/revanced/cli/command/PatchCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,23 @@ internal object PatchCommand : Runnable {
)
private var includedPatches = arrayOf<String>()

@CommandLine.Option(
names = ["--ii"],
description = ["List of patches to include by their index in relation to the supplied patch bundles."]
)
private var includedPatchesByIndex = arrayOf<Int>()

@CommandLine.Option(
names = ["-e", "--exclude"], description = ["List of patches to exclude."]
)
private var excludedPatches = arrayOf<String>()

@CommandLine.Option(
names = ["--ei"],
description = ["List of patches to exclude by their index in relation to the supplied patch bundles."]
)
private var excludedPatchesByIndex = arrayOf<Int>()

@CommandLine.Option(
names = ["--options"], description = ["Path to patch options JSON file."], showDefaultValue = ALWAYS
)
Expand Down Expand Up @@ -283,10 +295,10 @@ internal object PatchCommand : Runnable {
val packageName = context.packageMetadata.packageName
val packageVersion = context.packageMetadata.packageVersion

patches.forEach patch@{ patch ->
patches.withIndex().forEach patch@{ (i, patch) ->
val patchName = patch.name!!

val explicitlyExcluded = excludedPatches.contains(patchName)
val explicitlyExcluded = excludedPatches.contains(patchName) || excludedPatchesByIndex.contains(i)
if (explicitlyExcluded) return@patch logger.info("Excluding $patchName")

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

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

0 comments on commit b2055ce

Please sign in to comment.