|
|
|
@ -21,32 +21,75 @@ abstract class GeneratePluralRuleListsTask : DefaultTask() {
|
|
|
|
|
abstract val pluralsFile: RegularFileProperty |
|
|
|
|
|
|
|
|
|
@get:OutputDirectory |
|
|
|
|
abstract val outputDir: DirectoryProperty |
|
|
|
|
abstract val mainDir: DirectoryProperty |
|
|
|
|
|
|
|
|
|
@get:OutputDirectory |
|
|
|
|
abstract val testDir: DirectoryProperty |
|
|
|
|
|
|
|
|
|
@TaskAction |
|
|
|
|
fun generatePluralRuleLists() { |
|
|
|
|
val outputDir = outputDir.get().asFile |
|
|
|
|
if (outputDir.exists()) { |
|
|
|
|
outputDir.deleteRecursively() |
|
|
|
|
} |
|
|
|
|
outputDir.mkdirs() |
|
|
|
|
generateDirectories() |
|
|
|
|
|
|
|
|
|
val pluralRuleLists = parsePluralRuleLists() |
|
|
|
|
|
|
|
|
|
val mainContent = generateMainContent(pluralRuleLists) |
|
|
|
|
mainDir.get().asFile.resolve("cldr.kt").writeText(mainContent) |
|
|
|
|
|
|
|
|
|
val testContent = generateTestContent(pluralRuleLists) |
|
|
|
|
testDir.get().asFile.resolve("cldr.test.kt").writeText(testContent) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun generateDirectories() { |
|
|
|
|
for (directoryProperty in arrayOf(mainDir, testDir)) { |
|
|
|
|
val directory = directoryProperty.get().asFile |
|
|
|
|
if (directory.exists()) { |
|
|
|
|
directory.deleteRecursively() |
|
|
|
|
} |
|
|
|
|
directory.mkdirs() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun parsePluralRuleLists(): List<PluralRuleList> { |
|
|
|
|
val parser = XmlParser(false, false).apply { |
|
|
|
|
setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false) |
|
|
|
|
setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) |
|
|
|
|
} |
|
|
|
|
val supplementalData = parser.parse(pluralsFile.get().asFile) |
|
|
|
|
val pluralRuleLists = supplementalData.children().filterIsInstance<Node>().first { it.name() == "plurals" } |
|
|
|
|
|
|
|
|
|
return pluralRuleLists.children().filterIsInstance<Node>().map { pluralRules -> |
|
|
|
|
val locales = pluralRules.attribute("locales").toString().split(' ') |
|
|
|
|
PluralRuleList( |
|
|
|
|
locales, |
|
|
|
|
pluralRules.children().filterIsInstance<Node>().map { pluralRule -> |
|
|
|
|
val rule = pluralRule.text().split('@') |
|
|
|
|
PluralRule( |
|
|
|
|
pluralRule.attribute("count").toString(), |
|
|
|
|
// trim samples as not needed |
|
|
|
|
rule[0].trim(), |
|
|
|
|
rule.firstOrNull { it.startsWith("integer") }?.substringAfter("integer")?.trim() ?: "", |
|
|
|
|
rule.firstOrNull { it.startsWith("decimal") }?.substringAfter("decimal")?.trim() ?: "", |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun generateMainContent(pluralRuleLists: List<PluralRuleList>): String { |
|
|
|
|
val pluralRuleListIndexByLocale = pluralRuleLists.flatMapIndexed { idx, pluralRuleList -> |
|
|
|
|
pluralRuleList.locales.map { locale -> |
|
|
|
|
locale to idx |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val fileContent = """ |
|
|
|
|
return """ |
|
|
|
|
package org.jetbrains.compose.resources.intl |
|
|
|
|
|
|
|
|
|
internal val cldrPluralRuleListIndexByLocale = mapOf( |
|
|
|
|
${pluralRuleListIndexByLocale.joinToString { (locale, idx) -> "\"$locale\" to $idx" }} |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
internal val cldrPluralRuleLists = arrayOf( |
|
|
|
|
${ |
|
|
|
|
internal val cldrPluralRuleLists = arrayOf(${ |
|
|
|
|
pluralRuleLists.joinToString { pluralRuleList -> |
|
|
|
|
""" |
|
|
|
|
arrayOf(${ |
|
|
|
@ -56,34 +99,32 @@ abstract class GeneratePluralRuleListsTask : DefaultTask() {
|
|
|
|
|
}) |
|
|
|
|
""".trimIndent() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
""".trimIndent() |
|
|
|
|
|
|
|
|
|
outputDir.resolve("generated.kt").writeText(fileContent) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun parsePluralRuleLists(): List<PluralRuleList> { |
|
|
|
|
val parser = XmlParser(false, false).apply { |
|
|
|
|
setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false) |
|
|
|
|
setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) |
|
|
|
|
private fun generateTestContent(pluralRuleLists: List<PluralRuleList>): String { |
|
|
|
|
val pluralRuleIntegerSamplesByLocale = pluralRuleLists.flatMap { pluralRuleList -> |
|
|
|
|
pluralRuleList.locales.map { locale -> |
|
|
|
|
locale to pluralRuleList.rules.map { it.count to it.integerSample } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
val supplementalData = parser.parse(pluralsFile.get().asFile) |
|
|
|
|
val pluralRuleLists = supplementalData.children().filterIsInstance<Node>().first { it.name() == "plurals" } |
|
|
|
|
|
|
|
|
|
return pluralRuleLists.children().filterIsInstance<Node>().map { pluralRules -> |
|
|
|
|
val locales = pluralRules.attribute("locales").toString().split(' ') |
|
|
|
|
PluralRuleList( |
|
|
|
|
locales, |
|
|
|
|
pluralRules.children().filterIsInstance<Node>().map { pluralRule -> |
|
|
|
|
PluralRule( |
|
|
|
|
pluralRule.attribute("count").toString(), |
|
|
|
|
// trim samples as not needed |
|
|
|
|
pluralRule.text().split('@')[0].trim(), |
|
|
|
|
) |
|
|
|
|
return """ |
|
|
|
|
package org.jetbrains.compose.resources |
|
|
|
|
|
|
|
|
|
import org.jetbrains.compose.resources.intl.PluralCategory |
|
|
|
|
|
|
|
|
|
internal val cldrPluralRuleIntegerSamples = arrayOf(${ |
|
|
|
|
pluralRuleIntegerSamplesByLocale.joinToString { (locale, samples) -> |
|
|
|
|
""""$locale" to arrayOf(${ |
|
|
|
|
samples.joinToString { (count, sample) -> |
|
|
|
|
"PluralCategory.${count.uppercase()} to \"$sample\"" |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} )""".trimIndent() |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
""".trimIndent() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -95,4 +136,6 @@ private data class PluralRuleList(
|
|
|
|
|
private data class PluralRule( |
|
|
|
|
val count: String, |
|
|
|
|
val rule: String, |
|
|
|
|
val integerSample: String, |
|
|
|
|
val decimalSample: String, |
|
|
|
|
) |