Skip to content

Commit

Permalink
[gradle] Apply back gradle plugin changes to support plurals with new…
Browse files Browse the repository at this point in the history
… published library version

This reverts commit 7166247.
  • Loading branch information
terrakok committed Mar 25, 2024
1 parent 7166247 commit 3975a50
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,28 @@ internal abstract class GenerateResClassTask : DefaultTask() {
}

if (typeString == "values" && file.name.equals("strings.xml", true)) {
val stringIds = getStringIds(file)
return stringIds.map { strId ->
ResourceItem(ResourceType.STRING, qualifiers, strId.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)
}
}

val type = ResourceType.fromString(typeString)
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path))
}

private val stringTypeNames = listOf("string", "string-array")
private fun getStringIds(stringsXml: File): Set<String> {
//type -> id
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 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()
.map { it.nodeName to it.attributes.getNamedItem("name").nodeValue }
}

private fun File.listNotHiddenFiles(): List<File> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.io.path.invariantSeparatorsPathString
internal enum class ResourceType(val typeName: String) {
DRAWABLE("drawable"),
STRING("string"),
PLURAL_STRING("plurals"),
FONT("font");

override fun toString(): String = typeName
Expand All @@ -31,6 +32,7 @@ internal data class ResourceItem(
private fun ResourceType.getClassName(): ClassName = when (this) {
ResourceType.DRAWABLE -> ClassName("org.jetbrains.compose.resources", "DrawableResource")
ResourceType.STRING -> ClassName("org.jetbrains.compose.resources", "StringResource")
ResourceType.PLURAL_STRING -> ClassName("org.jetbrains.compose.resources", "PluralStringResource")
ResourceType.FONT -> ClassName("org.jetbrains.compose.resources", "FontResource")
}

Expand Down Expand Up @@ -225,7 +227,7 @@ private fun getChunkFileSpec(
CodeBlock.builder()
.add("return %T(\n", type.getClassName()).withIndent {
add("\"${type}:${resName}\",")
if (type == ResourceType.STRING) add(" \"$resName\",")
if (type == ResourceType.STRING || type == ResourceType.PLURAL_STRING) add(" \"$resName\",")
withIndent {
add("\nsetOf(\n").withIndent {
items.forEach { item ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public object Res {

public object string

public object plurals

public object font
}
Original file line number Diff line number Diff line change
@@ -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"),
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ internal object Res {

public object string

public object plurals

public object font
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@
<string name="PascalCase">PascalCase</string>
<string name="1-kebab-case">1-kebab-case</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>
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ internal object Res {

public object string

public object plurals

public object font
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ internal object Res {

public object string

public object plurals

public object font
}
2 changes: 1 addition & 1 deletion gradle-plugins/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kotlin.code.style=official
dev.junit.parallel=false

# Default version of Compose Libraries used by Gradle plugin
compose.version=1.6.10-dev1523
compose.version=1.6.10-dev1546
# The latest version of Compose Compiler used by Gradle plugin. Used only in tests/CI.
compose.tests.compiler.version=1.5.10.1
# The latest version of Kotlin compatible with compose.tests.compiler.version. Used only in tests/CI.
Expand Down

0 comments on commit 3975a50

Please sign in to comment.