From 8081a202858d54f114fea1de6361f8a3ab9de532 Mon Sep 17 00:00:00 2001 From: bogadana <30848157+bogadana@users.noreply.github.com> Date: Sun, 10 Jul 2022 15:28:18 +0200 Subject: [PATCH] docs: templated readme (#138) --- README-template.md | 8 +++++ build.gradle.kts | 2 +- .../Generator.kt} | 36 ++++++------------- .../app/revanced/meta/readme/Template.kt | 14 ++++++++ 4 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 README-template.md rename src/main/kotlin/app/revanced/meta/{ReadmeGenerator.kt => readme/Generator.kt} (55%) create mode 100644 src/main/kotlin/app/revanced/meta/readme/Template.kt diff --git a/README-template.md b/README-template.md new file mode 100644 index 000000000..5270fca77 --- /dev/null +++ b/README-template.md @@ -0,0 +1,8 @@ +# ReVanced Patches +🧩 Official patches by ReVanced + +# Patch list + +| 💊 Patch | 📜 Description | 🎯 Target Package | 🏹 Target Version | +|:--------:|:--------------:|:-----------------:|:-----------------:| +{{ table }} diff --git a/build.gradle.kts b/build.gradle.kts index b151da605..815515c7b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -54,7 +54,7 @@ tasks { dependsOn(build) classpath = sourceSets["main"].runtimeClasspath - mainClass.set("app.revanced.patches.meta.ReadmeGenerator") + mainClass.set("app.revanced.patches.meta.readme.Generator") } // Dummy task to fix the Gradle semantic-release plugin. // Remove this if you forked it to support building only. diff --git a/src/main/kotlin/app/revanced/meta/ReadmeGenerator.kt b/src/main/kotlin/app/revanced/meta/readme/Generator.kt similarity index 55% rename from src/main/kotlin/app/revanced/meta/ReadmeGenerator.kt rename to src/main/kotlin/app/revanced/meta/readme/Generator.kt index d5f3313c8..9a0ecc60e 100644 --- a/src/main/kotlin/app/revanced/meta/ReadmeGenerator.kt +++ b/src/main/kotlin/app/revanced/meta/readme/Generator.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.meta +package app.revanced.patches.meta.readme import java.io.File import kotlin.io.writeText @@ -8,36 +8,16 @@ import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages import app.revanced.patcher.extensions.PatchExtensions.patchName import app.revanced.patcher.extensions.PatchExtensions.description -class ReadmeGenerator { +class Generator { companion object { @JvmStatic fun main(args: Array) { - //should be moved to a file? - val generalReadme = - """ - # ReVanced Patches - 🧩 Official patches by ReVanced - - # Patch list - """.trimIndent() - - val tableHeader = - """ - | 💊 Patch | 📜 Description | 🎯 Target Package | 🏹 Target Version | - |:-----:|:-----------:|:--------------:|:----------------------:| - """.trimIndent() - - val readmeFile = File("README.md") - val buildDir = File("build/libs/") val buildJar = buildDir.listFiles().first { it.name.startsWith("revanced-patches-") && it.name.endsWith(".jar") } val bundle = JarPatchBundle(buildJar.absolutePath).loadPatches() - val builder = StringBuilder() - - builder.appendLine(generalReadme) - builder.appendLine(tableHeader) + val table = StringBuilder() for (patch in bundle) { val humanName = @@ -46,10 +26,16 @@ class ReadmeGenerator { val compatiblePackage = patch.compatiblePackages?.first() val latestVersion = compatiblePackage?.versions?.maxByOrNull { it.replace(".", "").toInt() } ?: "all" - builder.appendLine("|$humanName|${patch.description}|`${compatiblePackage?.name}`|$latestVersion|") + table.appendLine("|$humanName|${patch.description}|`${compatiblePackage?.name}`|$latestVersion|") } - readmeFile.writeText(builder.toString()) + val readMeTemplateFile = File("README-template.md") + val readMeTemplate = Template(readMeTemplateFile.readText()) + + readMeTemplate.replaceVariable("table", table.toString()) + + val readMeFile = File("README.md") + readMeFile.writeText(readMeTemplate.toString()) } } } diff --git a/src/main/kotlin/app/revanced/meta/readme/Template.kt b/src/main/kotlin/app/revanced/meta/readme/Template.kt new file mode 100644 index 000000000..98b2d5d81 --- /dev/null +++ b/src/main/kotlin/app/revanced/meta/readme/Template.kt @@ -0,0 +1,14 @@ +package app.revanced.patches.meta.readme + +class Template(val template: String) { + val result: StringBuilder = StringBuilder(template) + + fun replaceVariable(name: String, value: String) { + val regex = Regex("\\{\\{\\s?$name\\s?\\}\\}") + val range = regex.find(result)!!.range + + result.replace(range.start, range.endInclusive + 1, value) + } + + override fun toString(): String = result.toString() +}