Created
June 25, 2019 08:29
-
-
Save navneet-kumar/a003071c435dd04d6464670c4ee78899 to your computer and use it in GitHub Desktop.
Gradle task to generate kotlin data class on the fly from xml file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| task generateStrings{ | |
| String dir = "build/sources" | |
| delete dir | |
| mkdir(dir) | |
| def f = file(dir + "/StringsEn.kt") | |
| File file = file("src/main/resources/strings_en.xml") | |
| f.append("package myCompany.framework.data\n") | |
| f.append("data class StringsEn constructor (\n") | |
| def strings = [] | |
| def xml = new XmlParser().parse(file) | |
| xml.children().each {string -> | |
| String val = string.text() | |
| val = val.replaceAll("\"","\\\\\"").replace('$','\\$') | |
| strings.add( "public val " + string.@name + " : String = \"" + val + "\"") | |
| } | |
| f.append(strings.join(",\n") + ")") | |
| sourceSets.main.java.srcDir(dir) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment