mirror of
https://github.com/inotia00/revanced-patches.git
synced 2025-06-12 05:07:41 +02:00
feat: Add JSON meta
This commit is contained in:
27
src/main/kotlin/app/revanced/meta/Meta.kt
Normal file
27
src/main/kotlin/app/revanced/meta/Meta.kt
Normal file
@ -0,0 +1,27 @@
|
||||
package app.revanced.meta
|
||||
|
||||
import app.revanced.meta.json.generateJson
|
||||
import app.revanced.meta.readme.generateText
|
||||
import app.revanced.patcher.data.Data
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import app.revanced.patcher.util.patch.impl.JarPatchBundle
|
||||
import java.io.File
|
||||
|
||||
typealias Bundle = List<Class<out Patch<Data>>>
|
||||
|
||||
object Meta {
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val patches = accumulatePatches()
|
||||
if (patches.isEmpty()) throw IllegalStateException("No patches found")
|
||||
|
||||
generateText(patches)
|
||||
generateJson(patches)
|
||||
}
|
||||
}
|
||||
|
||||
fun accumulatePatches() = JarPatchBundle(
|
||||
File("build/libs/").listFiles()!!.first {
|
||||
it.name.startsWith("revanced-patches-") && it.name.endsWith(".jar")
|
||||
}.absolutePath
|
||||
).loadPatches()
|
33
src/main/kotlin/app/revanced/meta/json/Generator.kt
Normal file
33
src/main/kotlin/app/revanced/meta/json/Generator.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package app.revanced.meta.json
|
||||
|
||||
import app.revanced.meta.Bundle
|
||||
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
|
||||
import app.revanced.patcher.extensions.PatchExtensions.dependencies
|
||||
import app.revanced.patcher.extensions.PatchExtensions.description
|
||||
import app.revanced.patcher.extensions.PatchExtensions.include
|
||||
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
||||
import app.revanced.patcher.extensions.PatchExtensions.version
|
||||
import com.google.gson.Gson
|
||||
import java.io.File
|
||||
|
||||
private val gson = Gson()
|
||||
|
||||
fun generateJson(bundle: Bundle) {
|
||||
val patches = bundle.map {
|
||||
JsonPatch(
|
||||
it.patchName,
|
||||
it.description ?: "This patch has no description.",
|
||||
it.version ?: "0.0.0",
|
||||
!it.include,
|
||||
it.dependencies?.map { dep ->
|
||||
dep.java.patchName
|
||||
}?.toTypedArray() ?: emptyArray(),
|
||||
it.compatiblePackages?.map { pkg ->
|
||||
CompatiblePackage(pkg.name, pkg.versions)
|
||||
}?.toTypedArray() ?: emptyArray()
|
||||
)
|
||||
}
|
||||
|
||||
val json = File("patches.json")
|
||||
json.writeText(gson.toJson(patches))
|
||||
}
|
17
src/main/kotlin/app/revanced/meta/json/JsonPatch.kt
Normal file
17
src/main/kotlin/app/revanced/meta/json/JsonPatch.kt
Normal file
@ -0,0 +1,17 @@
|
||||
@file:Suppress("ArrayInDataClass") // We don't need it here.
|
||||
|
||||
package app.revanced.meta.json
|
||||
|
||||
data class JsonPatch(
|
||||
val name: String,
|
||||
val description: String,
|
||||
val version: String,
|
||||
val excluded: Boolean,
|
||||
val dependencies: Array<String>,
|
||||
val compatiblePackages: Array<CompatiblePackage>,
|
||||
)
|
||||
|
||||
data class CompatiblePackage(
|
||||
val name: String,
|
||||
val versions: Array<String>,
|
||||
)
|
@ -4,8 +4,7 @@ import app.revanced.patcher.data.Data
|
||||
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
|
||||
import app.revanced.patcher.patch.Patch
|
||||
|
||||
internal fun Class<out Patch<Data>>.getLatestVersion(): SemanticVersion? =
|
||||
this.compatiblePackages?.first()?.versions?.map { SemanticVersion.fromString(it) }
|
||||
?.maxWithOrNull(
|
||||
SemanticVersionComparator
|
||||
)
|
||||
internal fun Class<out Patch<Data>>.getLatestVersion() =
|
||||
this.compatiblePackages?.first()?.versions?.map {
|
||||
SemanticVersion.fromString(it)
|
||||
}?.maxWithOrNull(SemanticVersionComparator)
|
||||
|
@ -1,55 +1,44 @@
|
||||
package app.revanced.meta.readme
|
||||
|
||||
import app.revanced.meta.Bundle
|
||||
import app.revanced.patcher.data.Data
|
||||
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
|
||||
import app.revanced.patcher.extensions.PatchExtensions.description
|
||||
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import app.revanced.patcher.util.patch.impl.JarPatchBundle
|
||||
import java.io.File
|
||||
|
||||
object Generator {
|
||||
private const val TABLE_HEADER =
|
||||
"| \uD83D\uDC8A Patch | \uD83D\uDCDC Description | \uD83C\uDFF9 Target Version |\n" +
|
||||
"|:--------:|:--------------:|:-----------------:|"
|
||||
private const val TABLE_HEADER =
|
||||
"| \uD83D\uDC8A Patch | \uD83D\uDCDC Description | \uD83C\uDFF9 Target Version |\n" + "|:--------:|:--------------:|:-----------------:|"
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val buildDir = File("build/libs/")
|
||||
val buildJar =
|
||||
buildDir.listFiles()?.first { it.name.startsWith("revanced-patches-") && it.name.endsWith(".jar") }!!
|
||||
private val TABLE_REGEX = Regex("\\{\\{\\s?table\\s?}}")
|
||||
|
||||
val bundle = JarPatchBundle(buildJar.absolutePath).loadPatches()
|
||||
fun generateText(bundle: Bundle) {
|
||||
val output = StringBuilder()
|
||||
val packages = mutableMapOf<String, MutableList<Class<out Patch<Data>>>>()
|
||||
|
||||
val output = StringBuilder()
|
||||
|
||||
val packages = mutableMapOf<String, MutableList<Class<out Patch<Data>>>>()
|
||||
|
||||
bundle.map {
|
||||
val packageName = it.compatiblePackages?.first()?.name!!
|
||||
if (!packages.contains(packageName)) {
|
||||
packages[packageName] = mutableListOf()
|
||||
}
|
||||
|
||||
packages[packageName]?.add(it)
|
||||
bundle.map {
|
||||
val packageName = it.compatiblePackages?.first()?.name!!
|
||||
if (!packages.contains(packageName)) {
|
||||
packages[packageName] = mutableListOf()
|
||||
}
|
||||
|
||||
for (pkg in packages) {
|
||||
output.appendLine("### \uD83D\uDCE6 `${pkg.key}`")
|
||||
output.appendLine("<details>\n")
|
||||
|
||||
output.appendLine(TABLE_HEADER)
|
||||
pkg.value.forEach { output.appendLine("| `${it.patchName}` | ${it.description} | ${it.getLatestVersion() ?: "all"} |") }
|
||||
|
||||
output.appendLine("</details>\n")
|
||||
}
|
||||
|
||||
val readMeTemplateFile = File("README-template.md")
|
||||
val readmeTemplate = Template(readMeTemplateFile.readText())
|
||||
|
||||
readmeTemplate.replaceVariable("table", output.toString())
|
||||
|
||||
val readme = File("README.md")
|
||||
readme.writeText(readmeTemplate.toString())
|
||||
packages[packageName]?.add(it)
|
||||
}
|
||||
|
||||
for (pkg in packages) {
|
||||
output.appendLine("### \uD83D\uDCE6 `${pkg.key}`")
|
||||
output.appendLine("<details>\n")
|
||||
|
||||
output.appendLine(TABLE_HEADER)
|
||||
pkg.value.forEach { output.appendLine("| `${it.patchName}` | ${it.description} | ${it.getLatestVersion() ?: "all"} |") }
|
||||
|
||||
output.appendLine("</details>\n")
|
||||
}
|
||||
|
||||
val readmeTemplate = Template(File("README-template.md").readText())
|
||||
readmeTemplate.replaceVariable(TABLE_REGEX, output.toString())
|
||||
|
||||
val readme = File("README.md")
|
||||
readme.writeText(readmeTemplate.toString())
|
||||
}
|
||||
|
@ -3,10 +3,8 @@ package app.revanced.meta.readme
|
||||
data class SemanticVersion(val major: Int, val minor: Int, val patch: Int) {
|
||||
companion object {
|
||||
fun fromString(version: String): SemanticVersion {
|
||||
var parts = version.split(".")
|
||||
|
||||
val parts = version.split(".")
|
||||
if (parts.count() != 3) throw IllegalArgumentException("Invalid semantic version")
|
||||
|
||||
val versionNumbers = parts.map { it.toInt() }
|
||||
return SemanticVersion(versionNumbers[0], versionNumbers[1], versionNumbers[2])
|
||||
}
|
||||
|
@ -1,12 +1,10 @@
|
||||
package app.revanced.meta.readme
|
||||
|
||||
class Template(template: String) {
|
||||
val result: StringBuilder = StringBuilder(template)
|
||||
val result = StringBuilder(template)
|
||||
|
||||
fun replaceVariable(name: String, value: String) {
|
||||
val regex = Regex("\\{\\{\\s?$name\\s?}}")
|
||||
fun replaceVariable(regex: Regex, value: String) {
|
||||
val range = regex.find(result)!!.range
|
||||
|
||||
result.replace(range.first, range.last + 1, value)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user