Skip to content

Commit

Permalink
chore: Lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Nov 26, 2023
1 parent e7c3d64 commit 5fd205f
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 188 deletions.
113 changes: 60 additions & 53 deletions src/main/kotlin/app/revanced/cli/command/ListPatchesCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,119 +8,126 @@ import picocli.CommandLine.Help.Visibility.ALWAYS
import java.io.File
import java.util.logging.Logger


@Command(
name = "list-patches",
description = ["List patches from supplied patch bundles."]
description = ["List patches from supplied patch bundles."],
)
internal object ListPatchesCommand : Runnable {
private val logger = Logger.getLogger(ListPatchesCommand::class.java.name)

@Parameters(
description = ["Paths to patch bundles."],
arity = "1..*"
arity = "1..*",
)
private lateinit var patchBundles: Array<File>

@Option(
names = ["-d", "--with-descriptions"],
description = ["List their descriptions."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var withDescriptions: Boolean = true

@Option(
names = ["-p", "--with-packages"],
description = ["List the packages the patches are compatible with."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var withPackages: Boolean = false

@Option(
names = ["-v", "--with-versions"],
description = ["List the versions of the apps the patches are compatible with."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var withVersions: Boolean = false

@Option(
names = ["-o", "--with-options"],
description = ["List the options of the patches."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var withOptions: Boolean = false

@Option(
names = ["-u", "--with-universal-patches"],
description = ["List patches which are compatible with any app."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var withUniversalPatches: Boolean = true

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

@Option(
names = ["-f", "--filter-package-name"],
description = ["Filter patches by package name."]
description = ["Filter patches by package name."],
)
private var packageName: String? = null

override fun run() {
fun Patch.CompatiblePackage.buildString() = buildString {
if (withVersions && versions != null) {
appendLine("Package name: $name")
appendLine("Compatible versions:")
append(versions!!.joinToString("\n") { version -> version }.prependIndent("\t"))
} else append("Package name: $name")
}

fun PatchOption<*>.buildString() = buildString {
appendLine("Title: $title")
description?.let { appendLine("Description: $it") }
default?.let {
appendLine("Key: $key")
append("Default: $it")
} ?: append("Key: $key")

values?.let { values ->
appendLine("\nValid values:")
append(values.map { "${it.value} (${it.key})" }.joinToString("\n").prependIndent("\t"))
fun Patch.CompatiblePackage.buildString() =
buildString {
if (withVersions && versions != null) {
appendLine("Package name: $name")
appendLine("Compatible versions:")
append(versions!!.joinToString("\n") { version -> version }.prependIndent("\t"))
} else {
append("Package name: $name")
}
}
}

fun IndexedValue<Patch<*>>.buildString() = let { (index, patch) ->
fun PatchOption<*>.buildString() =
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")
)
appendLine("Title: $title")
description?.let { appendLine("Description: $it") }
default?.let {
appendLine("Key: $key")
append("Default: $it")
} ?: append("Key: $key")

values?.let { values ->
appendLine("\nValid values:")
append(values.map { "${it.value} (${it.key})" }.joinToString("\n").prependIndent("\t"))
}
}

if (withPackages && patch.compatiblePackages != null) {
appendLine("\nCompatible packages:")
append(patch.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
fun Patch<*>.filterCompatiblePackages(name: String) =
compatiblePackages?.any { it.name == name }
?: withUniversalPatches

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

Expand All @@ -129,4 +136,4 @@ internal object ListPatchesCommand : Runnable {

if (filtered.isNotEmpty()) logger.info(filtered.joinToString("\n\n") { it.buildString() })
}
}
}
27 changes: 14 additions & 13 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import picocli.CommandLine.Command
import picocli.CommandLine.IVersionProvider
import java.util.*


fun main(args: Array<String>) {
Logger.setDefault()
CommandLine(MainCommand).execute(*args).let(System::exit)
}

private object CLIVersionProvider : IVersionProvider {
override fun getVersion() = arrayOf(
MainCommand::class.java.getResourceAsStream(
"/app/revanced/cli/version.properties"
)?.use { stream ->
Properties().apply {
load(stream)
}.let {
"ReVanced CLI v${it.getProperty("version")}"
}
} ?: "ReVanced CLI")
override fun getVersion() =
arrayOf(
MainCommand::class.java.getResourceAsStream(
"/app/revanced/cli/version.properties",
)?.use { stream ->
Properties().apply {
load(stream)
}.let {
"ReVanced CLI v${it.getProperty("version")}"
}
} ?: "ReVanced CLI",
)
}

@Command(
Expand All @@ -36,6 +37,6 @@ private object CLIVersionProvider : IVersionProvider {
PatchCommand::class,
OptionsCommand::class,
UtilityCommand::class,
]
],
)
private object MainCommand
private object MainCommand
35 changes: 19 additions & 16 deletions src/main/kotlin/app/revanced/cli/command/OptionsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,46 @@ internal object OptionsCommand : Runnable {

@CommandLine.Parameters(
description = ["Paths to patch bundles."],
arity = "1..*"
arity = "1..*",
)
private lateinit var patchBundles: Array<File>

@CommandLine.Option(
names = ["-p", "--path"],
description = ["Path to patch options JSON file."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var filePath: File = File("options.json")

@CommandLine.Option(
names = ["-o", "--overwrite"],
description = ["Overwrite existing options file."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var overwrite: Boolean = false

@CommandLine.Option(
names = ["-u", "--update"],
description = ["Update existing options by adding missing and removing non-existent options."],
showDefaultValue = ALWAYS
showDefaultValue = ALWAYS,
)
private var update: Boolean = false

override fun run() = try {
PatchBundleLoader.Jar(*patchBundles).let { patches ->
val exists = filePath.exists()
if (!exists || overwrite) {
if (exists && update) patches.setOptions(filePath)

Options.serialize(patches, prettyPrint = true).let(filePath::writeText)
} else throw OptionsFileAlreadyExistsException()
override fun run() =
try {
PatchBundleLoader.Jar(*patchBundles).let { patches ->
val exists = filePath.exists()
if (!exists || overwrite) {
if (exists && update) patches.setOptions(filePath)

Options.serialize(patches, prettyPrint = true).let(filePath::writeText)
} else {
throw OptionsFileAlreadyExistsException()
}
}
} catch (ex: OptionsFileAlreadyExistsException) {
logger.severe("Options file already exists, use --overwrite to override it")
}
} catch (ex: OptionsFileAlreadyExistsException) {
logger.severe("Options file already exists, use --overwrite to override it")
}

class OptionsFileAlreadyExistsException : Exception()
}
}
Loading

0 comments on commit 5fd205f

Please sign in to comment.