Skip to content

Commit

Permalink
feat: add PathOption back
Browse files Browse the repository at this point in the history
Now backed by a String.
  • Loading branch information
Sculas committed Sep 9, 2022
1 parent 456db72 commit 172655b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package app.revanced.patcher.patch

import java.nio.file.Path
import kotlin.io.path.pathString
import kotlin.reflect.KProperty

class NoSuchOptionException(val option: String) : Exception("No such option: $option")
Expand Down Expand Up @@ -227,4 +229,20 @@ sealed class PatchOption<T>(
) : ListOption<Int>(
key, default, options, title, description, required, validator
)

/**
* A [PatchOption] representing a [Path], backed by a [String].
* The validator passes a [String], if you need a [Path] you will have to convert it yourself.
* @see PatchOption
*/
class PathOption(
key: String,
default: Path?,
title: String,
description: String,
required: Boolean = false,
validator: (String?) -> Boolean = { true }
) : PatchOption<String>(
key, default?.pathString, title, description, required, validator
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app.revanced.patcher.patch
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.io.path.Path
import kotlin.io.path.pathString
import kotlin.test.assertNotEquals

internal class PatchOptionsTest {
Expand Down Expand Up @@ -33,6 +35,10 @@ internal class PatchOptionsTest {
println(choice)
}
}

is PatchOption.PathOption -> {
option.value = Path("test.txt").pathString
}
}
}
val option = options.get<String>("key1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.jf.dexlib2.immutable.reference.ImmutableFieldReference
import org.jf.dexlib2.immutable.reference.ImmutableStringReference
import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue
import org.jf.dexlib2.util.Preconditions
import kotlin.io.path.Path

@Patch
@Name("example-bytecode-patch")
Expand Down Expand Up @@ -194,5 +195,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
"key5", null, "title", "description", true
)
)
private var key6 by option(
PatchOption.PathOption(
"key6", Path("test.txt"), "title", "description", true
)
)
}
}

0 comments on commit 172655b

Please sign in to comment.