2022-08-14 22:43:28 +08:00
|
|
|
import org.gradle.api.Project
|
|
|
|
import org.gradle.api.Task
|
|
|
|
import org.gradle.api.tasks.TaskProvider
|
|
|
|
import org.gradle.kotlin.dsl.TaskContainerScope
|
|
|
|
|
2023-03-25 10:58:29 +08:00
|
|
|
private val emptyResourcesElement = "<resources>\\s*</resources>|<resources/>".toRegex()
|
|
|
|
private val valuesPrefix = "values(-(b\\+)?)?".toRegex()
|
|
|
|
|
2022-08-14 22:43:28 +08:00
|
|
|
fun TaskContainerScope.registerLocalesConfigTask(project: Project): TaskProvider<Task> {
|
|
|
|
return with(project) {
|
|
|
|
register("generateLocalesConfig") {
|
|
|
|
val languages = fileTree("$projectDir/src/main/res/")
|
2023-03-25 10:58:29 +08:00
|
|
|
.matching { include("**/strings.xml") }
|
|
|
|
.filterNot { it.readText().contains(emptyResourcesElement) }
|
2022-08-30 01:54:02 +08:00
|
|
|
.map { it.parentFile.name }
|
|
|
|
.sorted()
|
2022-08-14 22:43:28 +08:00
|
|
|
.joinToString(separator = "\n") {
|
2022-08-30 01:54:02 +08:00
|
|
|
val language = it
|
2022-08-14 22:43:28 +08:00
|
|
|
.replace(valuesPrefix, "")
|
2022-08-30 01:54:02 +08:00
|
|
|
.replace("-r", "-")
|
2023-03-25 10:58:29 +08:00
|
|
|
.replace("+", "-")
|
2022-08-14 22:43:28 +08:00
|
|
|
.takeIf(String::isNotBlank) ?: "en"
|
|
|
|
" <locale android:name=\"$language\"/>"
|
|
|
|
}
|
|
|
|
|
|
|
|
val content = """
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
|
|
|
|
$languages
|
|
|
|
</locale-config>
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
val localeFile = file("$projectDir/src/main/res/xml/locales_config.xml")
|
|
|
|
localeFile.parentFile.mkdirs()
|
|
|
|
localeFile.writeText(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|