Skip to content

Commit

Permalink
feat: SincePatcher annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas committed Sep 8, 2022
1 parent 6e73631 commit 25f74dc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ tasks {
events("PASSED", "SKIPPED", "FAILED")
}
}
processResources {
expand("projectVersion" to project.version)
}
}

java {
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/app/revanced/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.revanced.patcher.data.impl.findIndexed
import app.revanced.patcher.extensions.PatchExtensions.dependencies
import app.revanced.patcher.extensions.PatchExtensions.deprecated
import app.revanced.patcher.extensions.PatchExtensions.patchName
import app.revanced.patcher.extensions.PatchExtensions.sincePatcherVersion
import app.revanced.patcher.extensions.nullOutputStream
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve
import app.revanced.patcher.patch.Patch
Expand All @@ -15,6 +16,7 @@ import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.impl.BytecodePatch
import app.revanced.patcher.patch.impl.ResourcePatch
import app.revanced.patcher.util.ListBackedSet
import app.revanced.patcher.util.VersionReader
import brut.androlib.Androlib
import brut.androlib.meta.UsesFramework
import brut.androlib.options.BuildOptions
Expand All @@ -36,7 +38,8 @@ import java.io.Closeable
import java.io.File
import java.nio.file.Files

val NAMER = BasicDexFileNamer()
private val NAMER = BasicDexFileNamer()
private val VERSION = VersionReader.read()

/**
* The ReVanced Patcher.
Expand Down Expand Up @@ -244,6 +247,15 @@ class Patcher(private val options: PatcherOptions) {
* @param patches [Patch]es The patches to add.
*/
fun addPatches(patches: Iterable<Class<out Patch<Data>>>) {
for (patch in patches) {
val needsVersion = patch.sincePatcherVersion
if (needsVersion != null && needsVersion > VERSION) {
logger.error("Patch '${patch.patchName}' requires Patcher version $needsVersion or higher")
logger.error("Current Patcher version is $VERSION")
logger.warn("Skipping '${patch.patchName}'!")
continue // TODO: continue or halt/throw?
}
}
data.patches.addAll(patches)
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/app/revanced/patcher/annotation/SincePatcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.revanced.patcher.annotation

import app.revanced.patcher.patch.Patch
import app.revanced.patcher.Patcher

/**
* Declares a [Patch] deprecated for removal.
* @param version The minimum version of the [Patcher] this [Patch] supports.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class SincePatcher(val version: String)
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object PatchExtensions {
if (cl == Patch::class) null else cl
}
}
val Class<out Patch<Data>>.sincePatcherVersion get() = recursiveAnnotation(SincePatcher::class)?.version

@JvmStatic
fun Class<out Patch<Data>>.dependsOn(patch: Class<out Patch<Data>>): Boolean {
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/app/revanced/patcher/util/VersionReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app.revanced.patcher.util

import java.util.*

internal object VersionReader {
@JvmStatic
private val props = Properties().apply {
load(
VersionReader::class.java.getResourceAsStream("/revanced-patcher/version.properties")
?: throw IllegalStateException("Could not load version.properties")
)
}

@JvmStatic
fun read(): String {
return props.getProperty("version") ?: throw IllegalStateException("Version not found")
}
}
1 change: 1 addition & 0 deletions src/main/resources/revanced-patcher/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${projectVersion}
20 changes: 20 additions & 0 deletions src/test/kotlin/app/revanced/patcher/util/VersionReaderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.revanced.patcher.util

import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*

internal class VersionReaderTest {
@Test
fun read() {
val version = VersionReader.read()
assertNotNull(version)
assertTrue(version.isNotEmpty())
val parts = version.split(".")
assertEquals(3, parts.size)
parts.forEach {
assertTrue(it.toInt() >= 0)
}
println(version)
}
}

0 comments on commit 25f74dc

Please sign in to comment.