From b7e3059283eb0652e79272eeff1d4116b9d4036f Mon Sep 17 00:00:00 2001 From: Konstantin Tskhovrebov Date: Fri, 22 Mar 2024 17:36:10 +0100 Subject: [PATCH] [gradle] Refactor resource parser and update test data. --- .../compose/resources/GenerateResClassTask.kt | 32 +++++++------------ .../expected-open-res/Plurals0.kt | 26 +++++++++++++++ .../commonResources/expected-open-res/Res.kt | 2 ++ .../misc/commonResources/expected/Plurals0.kt | 26 +++++++++++++++ .../misc/commonResources/expected/Res.kt | 2 ++ .../composeResources/values/strings.xml | 10 ++++++ .../misc/emptyResources/expected/Res.kt | 2 ++ .../misc/jvmOnlyResources/expected/Res.kt | 2 ++ 8 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Plurals0.kt create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Plurals0.kt diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt index 40a981bc25..5e7a234cd3 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt @@ -101,12 +101,13 @@ internal abstract class GenerateResClassTask : DefaultTask() { } if (typeString == "values" && file.name.equals("strings.xml", true)) { - val stringIds = getStringIds(file) - val pluralStringIds = getPluralStringIds(file) - return stringIds.map { strId -> - ResourceItem(ResourceType.STRING, qualifiers, strId.asUnderscoredIdentifier(), path) - } + pluralStringIds.map { pluralStrId -> - ResourceItem(ResourceType.PLURAL_STRING, qualifiers, pluralStrId.asUnderscoredIdentifier(), path) + return getStringResources(file).mapNotNull { (typeName, strId) -> + val type = when(typeName) { + "string", "string-array" -> ResourceType.STRING + "plurals" -> ResourceType.PLURAL_STRING + else -> return@mapNotNull null + } + ResourceItem(type, qualifiers, strId.asUnderscoredIdentifier(), path) } } @@ -114,23 +115,14 @@ internal abstract class GenerateResClassTask : DefaultTask() { return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path)) } - private val stringTypeNames = listOf("string", "string-array") - private fun getStringIds(stringsXml: File): Set { + //type -> id + private val stringTypeNames = listOf("string", "string-array", "plurals") + private fun getStringResources(stringsXml: File): List> { val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml) val items = doc.getElementsByTagName("resources").item(0).childNodes - val ids = List(items.length) { items.item(it) } + return List(items.length) { items.item(it) } .filter { it.nodeName in stringTypeNames } - .map { it.attributes.getNamedItem("name").nodeValue } - return ids.toSet() - } - - private fun getPluralStringIds(stringsXml: File): Set { - val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml) - val items = doc.getElementsByTagName("resources").item(0).childNodes - val ids = List(items.length) { items.item(it) } - .filter { it.nodeName == "plurals" } - .map { it.attributes.getNamedItem("name").nodeValue } - return ids.toSet() + .map { it.nodeName to it.attributes.getNamedItem("name").nodeValue } } private fun File.listNotHiddenFiles(): List = diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Plurals0.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Plurals0.kt new file mode 100644 index 0000000000..31cac7ea1f --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Plurals0.kt @@ -0,0 +1,26 @@ +@file:OptIn(org.jetbrains.compose.resources.InternalResourceApi::class) + +package my.lib.res + +import kotlin.OptIn +import org.jetbrains.compose.resources.ExperimentalResourceApi +import org.jetbrains.compose.resources.PluralStringResource + +@ExperimentalResourceApi +private object Plurals0 { + public val numberOfSongsAvailable: PluralStringResource by + lazy { init_numberOfSongsAvailable() } +} + +@ExperimentalResourceApi +public val Res.plurals.numberOfSongsAvailable: PluralStringResource + get() = Plurals0.numberOfSongsAvailable + +@ExperimentalResourceApi +private fun init_numberOfSongsAvailable(): PluralStringResource = + org.jetbrains.compose.resources.PluralStringResource( + "plurals:numberOfSongsAvailable", "numberOfSongsAvailable", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"), + ) + ) \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Res.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Res.kt index 76964658a9..4644d746fe 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Res.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Res.kt @@ -27,5 +27,7 @@ public object Res { public object string + public object plurals + public object font } diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Plurals0.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Plurals0.kt new file mode 100644 index 0000000000..81148dce1d --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Plurals0.kt @@ -0,0 +1,26 @@ +@file:OptIn(org.jetbrains.compose.resources.InternalResourceApi::class) + +package app.group.resources_test.generated.resources + +import kotlin.OptIn +import org.jetbrains.compose.resources.ExperimentalResourceApi +import org.jetbrains.compose.resources.PluralStringResource + +@ExperimentalResourceApi +private object Plurals0 { + public val numberOfSongsAvailable: PluralStringResource by + lazy { init_numberOfSongsAvailable() } +} + +@ExperimentalResourceApi +internal val Res.plurals.numberOfSongsAvailable: PluralStringResource + get() = Plurals0.numberOfSongsAvailable + +@ExperimentalResourceApi +private fun init_numberOfSongsAvailable(): PluralStringResource = + org.jetbrains.compose.resources.PluralStringResource( + "plurals:numberOfSongsAvailable", "numberOfSongsAvailable", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"), + ) + ) \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt index 3bfdad8113..416db64499 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt @@ -27,5 +27,7 @@ internal object Res { public object string + public object plurals + public object font } diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml index 6fa9a0966b..9e537278ba 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml @@ -13,4 +13,14 @@ PascalCase 1-kebab-case camelCase + + + %d zero + %d one + %d two + %d few + %d many + %d other + + diff --git a/gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt b/gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt index 81038d418b..160bec3ba9 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt @@ -27,5 +27,7 @@ internal object Res { public object string + public object plurals + public object font } \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt b/gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt index be1a2fe52a..21484d23c2 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt @@ -27,5 +27,7 @@ internal object Res { public object string + public object plurals + public object font }