refactor: improve ResourceData.kt

Old methods have been marked as deprecated, and will be removed in the future.

- ResourceData.kt was made an Iterable<File>, and the forEach method was removed in favor of Kotlin's forEach function. (no modifications required)
- The resolve method was deprecated in favor of a new operator getter function, which can be either called using get(path) or data[path]. This keeps backwards compatibility with the old get method.
- The getXmlEditor method was deprecated in favor of the new xmlEditor variable, which is a XmlFileHolder which has an operator getter which acts like an array. This is syntactically better.
This commit is contained in:
Lucaskyy 2022-06-26 17:14:38 +02:00
parent 35749454ab
commit f565c4f6a7
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
2 changed files with 14 additions and 7 deletions

View File

@ -9,13 +9,20 @@ import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult import javax.xml.transform.stream.StreamResult
class ResourceData(private val resourceCacheDirectory: File) : Data { class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<File> {
private fun resolve(path: String) = resourceCacheDirectory.resolve(path) operator fun get(path: String) = resourceCacheDirectory.resolve(path)
val xmlEditor = XmlFileHolder()
override fun iterator() = resourceCacheDirectory.walkTopDown().iterator()
fun forEach(action: (File) -> Unit) = resourceCacheDirectory.walkTopDown().forEach(action) inner class XmlFileHolder {
fun get(path: String) = resolve(path) operator fun get(path: String) = DomFileEditor(this@ResourceData[path])
}
fun getXmlEditor(path: String) = DomFileEditor(resolve(path)) @Deprecated("Use operator getter instead of resolve function", ReplaceWith("get(path)"))
fun resolve(path: String) = get(path)
@Deprecated("Use operator getter on xmlEditor instead of getXmlEditor function", ReplaceWith("xmlEditor[path]"))
fun getXmlEditor(path: String) = xmlEditor[path]
} }
class DomFileEditor internal constructor(private val domFile: File) : Closeable { class DomFileEditor internal constructor(private val domFile: File) : Closeable {

View File

@ -18,8 +18,8 @@ import org.w3c.dom.Element
@Version("0.0.1") @Version("0.0.1")
class ExampleResourcePatch : ResourcePatch() { class ExampleResourcePatch : ResourcePatch() {
override fun execute(data: ResourceData): PatchResult { override fun execute(data: ResourceData): PatchResult {
data.getXmlEditor("AndroidManifest.xml").use { domFileEditor -> data.xmlEditor["AndroidManifest.xml"].use { editor ->
val element = domFileEditor // regular DomFileEditor val element = editor // regular DomFileEditor
.file .file
.getElementsByTagName("application") .getElementsByTagName("application")
.item(0) as Element .item(0) as Element