Browse Source

[gradle] Refactor resource parser and update test data.

pull/4519/head
Konstantin Tskhovrebov 8 months ago committed by Chanjung Kim
parent
commit
b7e3059283
  1. 32
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  2. 26
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Plurals0.kt
  3. 2
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/Res.kt
  4. 26
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Plurals0.kt
  5. 2
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt
  6. 10
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml
  7. 2
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt
  8. 2
      gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt

32
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)) { if (typeString == "values" && file.name.equals("strings.xml", true)) {
val stringIds = getStringIds(file) return getStringResources(file).mapNotNull { (typeName, strId) ->
val pluralStringIds = getPluralStringIds(file) val type = when(typeName) {
return stringIds.map { strId -> "string", "string-array" -> ResourceType.STRING
ResourceItem(ResourceType.STRING, qualifiers, strId.asUnderscoredIdentifier(), path) "plurals" -> ResourceType.PLURAL_STRING
} + pluralStringIds.map { pluralStrId -> else -> return@mapNotNull null
ResourceItem(ResourceType.PLURAL_STRING, qualifiers, pluralStrId.asUnderscoredIdentifier(), path) }
ResourceItem(type, qualifiers, strId.asUnderscoredIdentifier(), path)
} }
} }
@ -114,23 +115,14 @@ internal abstract class GenerateResClassTask : DefaultTask() {
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path)) return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path))
} }
private val stringTypeNames = listOf("string", "string-array") //type -> id
private fun getStringIds(stringsXml: File): Set<String> { private val stringTypeNames = listOf("string", "string-array", "plurals")
private fun getStringResources(stringsXml: File): List<Pair<String, String>> {
val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml) val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml)
val items = doc.getElementsByTagName("resources").item(0).childNodes 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 } .filter { it.nodeName in stringTypeNames }
.map { it.attributes.getNamedItem("name").nodeValue } .map { it.nodeName to it.attributes.getNamedItem("name").nodeValue }
return ids.toSet()
}
private fun getPluralStringIds(stringsXml: File): Set<String> {
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()
} }
private fun File.listNotHiddenFiles(): List<File> = private fun File.listNotHiddenFiles(): List<File> =

26
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"),
)
)

2
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 string
public object plurals
public object font public object font
} }

26
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"),
)
)

2
gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt

@ -27,5 +27,7 @@ internal object Res {
public object string public object string
public object plurals
public object font public object font
} }

10
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml

@ -13,4 +13,14 @@
<string name="PascalCase">PascalCase</string> <string name="PascalCase">PascalCase</string>
<string name="1-kebab-case">1-kebab-case</string> <string name="1-kebab-case">1-kebab-case</string>
<string name="camelCase">camelCase</string> <string name="camelCase">camelCase</string>
<plurals name="numberOfSongsAvailable">
<item quantity="zero">%d zero</item>
<item quantity="one">%d one</item>
<item quantity="two">%d two</item>
<item quantity="few">%d few</item>
<item quantity="many">%d many</item>
<item quantity="other">%d other</item>
</plurals>
</resources> </resources>

2
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt

@ -27,5 +27,7 @@ internal object Res {
public object string public object string
public object plurals
public object font public object font
} }

2
gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt

@ -27,5 +27,7 @@ internal object Res {
public object string public object string
public object plurals
public object font public object font
} }

Loading…
Cancel
Save