stringGenerator migrated to kotlin dsl

This commit is contained in:
Karenkov Igor 2019-07-16 21:34:48 +03:00
parent 23d74e557c
commit ca21fb6c88
2 changed files with 105 additions and 92 deletions

View File

@ -1,92 +0,0 @@
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
task stringGenerator {
generate(languageMap)
println("Strings generated!")
}
private def generate(Map<String, String> sources) {
if (sources == null || sources.isEmpty()) {
throw new IOException("languageMap can't be null or empty")
}
Map jsonMap = getJsonsMap(sources)
def diffs = calcDiffs(jsonMap)
if (!diffs.isEmpty()) {
printDiffs(diffs)
throw new IllegalStateException("Strings source can't be different")
}
def defaultLang = getDefaultLangKey(sources)
jsonMap.forEach { key, json ->
def sw = new StringWriter()
def xml = new MarkupBuilder(sw)
xml.setDoubleQuotes(true)
xml.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml.resources() {
json.each {
k, v ->
string(name: "${k}", formatted: "false", "${v}".replace('\n', '\\n'))
}
}
def stringsFile = getFile(key, key == defaultLang)
stringsFile.write(sw.toString(), "UTF-8")
}
}
private printDiffs(Map<?, ?> diffs) {
def diffLog = new StringBuilder()
diffs.forEach { k, v ->
if (v.size() > 0) {
diffLog.append("For $k was missed string keys: ${v.size()}\n")
v.forEach {
diffLog.append("\tString key: $it\n")
}
}
}
println(diffLog.toString())
}
private static def calcDiffs(Map<String, Object> jsonsMap) {
if (jsonsMap.size() == 1) {
return [:]
}
def keys = jsonsMap.collectEntries {
[(it.key): (it.value).keySet() as List]
}
def inclusive = keys.get(keys.keySet().first())
def diffs = keys.collectEntries {
[(it.key): inclusive - it.value.intersect(inclusive)]
}.findAll { it.value.size() > 0 }
return diffs
}
private static Map<String, Object> getJsonsMap(Map sources) {
return sources.collectEntries {
[(it.key): new JsonSlurper().parseText(new File(it.value).text)]
}
}
private static File getFile(String key, boolean defaultLang) {
if (defaultLang) {
return new File("app/src/main/res/values/strings.xml")
} else {
def directory = new File("app/src/main/res/values-$key")
if (!directory.exists()) {
directory.mkdir()
}
return new File("app/src/main/res/values-$key/strings.xml")
}
}
private static String getDefaultLangKey(Map<String, String> sources) {
def defaultLanguage = sources.find { it.value.contains("default") }
if (defaultLanguage != null) {
return defaultLanguage.key
} else {
throw new IOException("Can't find default language")
}
}

View File

@ -0,0 +1,105 @@
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.redundent.kotlin.xml.PrintOptions
import org.redundent.kotlin.xml.xml
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.redundent:kotlin-xml-builder:1.5.1")
classpath("com.google.code.gson:gson:2.8.5")
}
}
task("stringGenerator") {
val languageMap: Map<String, String>? by rootProject.extra
generate(languageMap)
println("Strings generated!")
}
fun generate(sources: Map<String, String>?) {
if (sources == null || sources.isEmpty()) {
throw java.io.IOException("languageMap can't be null or empty")
}
val jsonMap = getJsonsMap(sources)
val diffs = calcDiffs(jsonMap)
if (diffs.isNotEmpty()) {
printDiffs(diffs)
throw IllegalStateException("Strings source can't be different")
}
val defaultLang = getDefaultLangKey(sources)
jsonMap.forEach { (key, json) ->
val xmlString = xml("resources") {
includeXmlProlog = true
json.entrySet().forEach {
"string" {
attribute("name", it.key)
attribute("formatted", "false")
-it.value.asString.replace("\n", "\\n")
}
}
}
val stringsFile = getFile(key, key == defaultLang)
stringsFile.writeText(xmlString.toString(PrintOptions(singleLineTextElements = true)))
}
}
fun printDiffs(diffs: Map<String, Set<String>>) {
val diffLog = StringBuilder()
diffs.forEach { (key, value) ->
if (value.isNotEmpty()) {
diffLog.append("For $key was missed string keys: ${value.size}\n")
value.forEach {
diffLog.append("\tString key: $it\n")
}
}
}
println(diffLog.toString())
}
fun calcDiffs(jsonsMap: Map<String, JsonObject>): Map<String, Set<String>> {
if (jsonsMap.size == 1) {
return emptyMap()
}
val keys = jsonsMap.mapValues {
it.value.keySet().toList()
}
val inclusive = keys[keys.keys.first()]!!.toSet()
return keys.mapValues {
inclusive - it.value.intersect(inclusive)
}.filter { it.value.isNotEmpty() }
}
fun getJsonsMap(sources: Map<String, String>): Map<String, JsonObject> {
return sources.mapValues {
JsonParser().parse(File(it.value).readText()).asJsonObject
}
}
fun getFile(key: String, defaultLang: Boolean): File {
if (defaultLang) {
return File("app/src/main/res/values/strings.xml")
} else {
val directory = File("app/src/main/res/values-$key")
if (!directory.exists()) {
directory.mkdir()
}
return File("app/src/main/res/values-$key/strings.xml")
}
}
fun getDefaultLangKey(sources: Map<String, String>): String {
val defaultLanguage = sources
.filter { it.value.contains("default") }
.iterator().run {
if (hasNext()) next() else null
}
if (defaultLanguage != null) {
return defaultLanguage.key
} else {
throw java.io.IOException("Can't find default language")
}
}