mirror of
https://github.com/revanced/revanced-cli.git
synced 2024-06-02 11:26:06 +02:00
chore: json: use classes
This commit is contained in:
parent
3e222e0559
commit
4b5425d96e
|
@ -1,5 +1,6 @@
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "1.9.10"
|
kotlin("jvm") version "1.9.10"
|
||||||
|
kotlin("plugin.serialization") version "1.9.10"
|
||||||
alias(libs.plugins.shadow)
|
alias(libs.plugins.shadow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@ package app.revanced.cli.command
|
||||||
import app.revanced.patcher.PatchBundleLoader
|
import app.revanced.patcher.PatchBundleLoader
|
||||||
import app.revanced.patcher.patch.Patch
|
import app.revanced.patcher.patch.Patch
|
||||||
import app.revanced.patcher.patch.options.PatchOption
|
import app.revanced.patcher.patch.options.PatchOption
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import picocli.CommandLine.*
|
import picocli.CommandLine.*
|
||||||
import picocli.CommandLine.Help.Visibility.ALWAYS
|
import picocli.CommandLine.Help.Visibility.ALWAYS
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -72,7 +73,7 @@ internal object ListPatchesCommand : Runnable {
|
||||||
|
|
||||||
@Option(
|
@Option(
|
||||||
names = ["-j", "--json"],
|
names = ["-j", "--json"],
|
||||||
description = ["Output in machine-readable format."],
|
description = ["Output in machine-readable format. Implies -diopv"],
|
||||||
showDefaultValue = ALWAYS
|
showDefaultValue = ALWAYS
|
||||||
)
|
)
|
||||||
private var asJson: Boolean = false
|
private var asJson: Boolean = false
|
||||||
|
@ -136,41 +137,9 @@ internal object ListPatchesCommand : Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun formatJson(patches: List<IndexedValue<Patch<*>>>): String {
|
private fun formatJson(patches: List<IndexedValue<Patch<*>>>): String {
|
||||||
fun Patch.CompatiblePackage.asJson(): JsonObject = buildJsonObject {
|
val data = patches.map { app.revanced.cli.serialization.Patch(it) }
|
||||||
put("name", name)
|
val format = Json { encodeDefaults = false }
|
||||||
if (withVersions) versions?.let {
|
return format.encodeToString(data)
|
||||||
put("compatible_versions", JsonArray(it.map(::JsonPrimitive)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun PatchOption<*>.asJson(): JsonObject = buildJsonObject {
|
|
||||||
put("title", title)
|
|
||||||
description?.let { put("description", it) }
|
|
||||||
put("key", key)
|
|
||||||
default?.let { put("default", it.toString()) }
|
|
||||||
values?.let { values ->
|
|
||||||
put("valid", JsonObject(values.mapValues { entry -> JsonPrimitive(entry.value.toString()) }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun IndexedValue<Patch<*>>.asJson(): JsonObject = let { (index, patch) ->
|
|
||||||
buildJsonObject {
|
|
||||||
if (withIndex) put("index", index)
|
|
||||||
put("name", patch.name)
|
|
||||||
if (withDescriptions) patch.description?.let { put("description", it) }
|
|
||||||
if (withOptions && patch.options.isNotEmpty()) {
|
|
||||||
put("options", JsonArray(patch.options.values.map(PatchOption<*>::asJson)))
|
|
||||||
}
|
|
||||||
if (withPackages) {
|
|
||||||
patch.compatiblePackages?.let {
|
|
||||||
put("compatible_packages", JsonArray(it.map(Patch.CompatiblePackage::asJson)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val json = JsonArray(patches.map(IndexedValue<Patch<*>>::asJson))
|
|
||||||
return json.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
|
|
50
src/main/kotlin/app/revanced/cli/serialization/schema.kt
Normal file
50
src/main/kotlin/app/revanced/cli/serialization/schema.kt
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package app.revanced.cli.serialization
|
||||||
|
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class CompatiblePackage(
|
||||||
|
val name: String,
|
||||||
|
@SerialName("compatible_versions")
|
||||||
|
val compatibleVersions: Set<String> = setOf()
|
||||||
|
) {
|
||||||
|
constructor(from: app.revanced.patcher.patch.Patch.CompatiblePackage) : this(
|
||||||
|
from.name,
|
||||||
|
from.versions.orEmpty()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class PatchOption(
|
||||||
|
val title: String?,
|
||||||
|
val description: String? = null,
|
||||||
|
val key: String,
|
||||||
|
val default: String? = null,
|
||||||
|
val valid: Map<String, String?> = mapOf(),
|
||||||
|
) {
|
||||||
|
constructor(from: app.revanced.patcher.patch.options.PatchOption<*>) : this(
|
||||||
|
from.title,
|
||||||
|
from.description,
|
||||||
|
from.key,
|
||||||
|
from.default?.toString(),
|
||||||
|
from.values?.mapValues { it.value.toString() }.orEmpty()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Patch(
|
||||||
|
val index: Int,
|
||||||
|
val description: String? = null,
|
||||||
|
val options: List<PatchOption> = listOf(),
|
||||||
|
@SerialName("compatible_packages")
|
||||||
|
val compatiblePackages: List<CompatiblePackage> = listOf(),
|
||||||
|
) {
|
||||||
|
constructor(from: IndexedValue<app.revanced.patcher.patch.Patch<*>>) : this(
|
||||||
|
from.index,
|
||||||
|
from.value.description,
|
||||||
|
from.value.options.values.map(::PatchOption),
|
||||||
|
from.value.compatiblePackages?.map(::CompatiblePackage).orEmpty(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user