From 9a513c55e4c46709977caa8a955fb583dc418358 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 31 May 2024 14:12:13 +0200 Subject: [PATCH] [gradle] Fix resource accessor name escaping. (#4901) Fixes https://github.com/JetBrains/compose-multiplatform/issues/4548 ## Testing Add compose resources with name such as "package", "is" or "item_$xxx" and check that app compiles and works fine. Accessors should be properly escaped ## Release Notes ### Fixes - Resources - Fix resource accessors escaping. Now it is possible to use resources with names: "package", "is", "item_$xxx" etc --- .../resources/GeneratedResClassSpec.kt | 8 ++--- .../test/tests/integration/ResourcesTest.kt | 4 +-- .../my/lib/res/Drawable0.commonMain.kt | 13 +++++++ .../my/lib/res/String0.commonMain.kt | 19 ++++++++-- .../resources/Drawable0.commonMain.kt | 13 +++++++ .../generated/resources/String0.commonMain.kt | 19 ++++++++-- .../composeResources/drawable/is.xml | 36 +++++++++++++++++++ .../composeResources/values/strings.xml | 1 + 8 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/is.xml diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt index 1ecdd84f35..df8add1101 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt @@ -240,7 +240,7 @@ private fun getChunkFileSpec( typeObject.addModifiers(KModifier.PRIVATE) val properties = idToResources.keys.map { resName -> PropertySpec.builder(resName, type.getClassName()) - .delegate("\nlazy·{ init_$resName() }") + .delegate("\nlazy·{ %N() }", "init_$resName") .build() } typeObject.addProperties(properties) @@ -250,7 +250,7 @@ private fun getChunkFileSpec( idToResources.forEach { (resName, items) -> val accessor = PropertySpec.builder(resName, type.getClassName(), resModifier) .receiver(ClassName(packageName, "Res", type.accessorName)) - .getter(FunSpec.getterBuilder().addStatement("return $chunkClassName.$resName").build()) + .getter(FunSpec.getterBuilder().addStatement("return $chunkClassName.%N", resName).build()) .build() chunkFile.addProperty(accessor) @@ -260,8 +260,8 @@ private fun getChunkFileSpec( .addStatement( CodeBlock.builder() .add("return %T(\n", type.getClassName()).withIndent { - add("\"${type}:${resName}\",") - if (type.requiresKeyName()) add(" \"$resName\",") + add("%S,", "$type:$resName") + if (type.requiresKeyName()) add(" %S,", resName) withIndent { add("\nsetOf(\n").withIndent { items.forEach { item -> diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt index 3a3da032cf..9b5c8b22ae 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt @@ -227,9 +227,7 @@ class ResourcesTest : GradlePluginTestBase() { @Test fun testMultiModuleResources() { - val environment = defaultTestEnvironment.copy( - kotlinVersion = "2.0.0-RC2" - ) + val environment = defaultTestEnvironment.copy(kotlinVersion = "2.0.0") with( testProject("misc/kmpResourcePublication", environment) ) { diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/Drawable0.commonMain.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/Drawable0.commonMain.kt index 408e508f37..d85c4af1ad 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/Drawable0.commonMain.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/Drawable0.commonMain.kt @@ -12,6 +12,9 @@ private object CommonMainDrawable0 { public val camelCaseName: DrawableResource by lazy { init_camelCaseName() } + public val `is`: DrawableResource by + lazy { init_is() } + public val vector: DrawableResource by lazy { init_vector() } @@ -41,6 +44,16 @@ private fun init_camelCaseName(): DrawableResource = ) ) +public val Res.drawable.`is`: DrawableResource + get() = CommonMainDrawable0.`is` + +private fun init_is(): DrawableResource = org.jetbrains.compose.resources.DrawableResource( + "drawable:is", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/is.xml", -1, -1), + ) +) + public val Res.drawable.vector: DrawableResource get() = CommonMainDrawable0.vector diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/String0.commonMain.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/String0.commonMain.kt index d3a70bb1f2..e7ddb90a51 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/String0.commonMain.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected-open-res/commonMainResourceAccessors/my/lib/res/String0.commonMain.kt @@ -21,6 +21,9 @@ private object CommonMainString0 { public val hello: StringResource by lazy { init_hello() } + public val `info_using_release_$x`: StringResource by + lazy { `init_info_using_release_$x`() } + public val multi_line: StringResource by lazy { init_multi_line() } @@ -83,13 +86,25 @@ private fun init_hello(): StringResource = org.jetbrains.compose.resources.Strin ) ) +public val Res.string.`info_using_release_$x`: StringResource + get() = CommonMainString0.`info_using_release_$x` + +private fun `init_info_using_release_$x`(): StringResource = + org.jetbrains.compose.resources.StringResource( + "string:info_using_release_${'$'}x", "info_using_release_${'$'}x", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 320, + 57), + ) + ) + public val Res.string.multi_line: StringResource get() = CommonMainString0.multi_line private fun init_multi_line(): StringResource = org.jetbrains.compose.resources.StringResource( "string:multi_line", "multi_line", setOf( - org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 320, + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 378, 178), ) ) @@ -100,7 +115,7 @@ public val Res.string.str_template: StringResource private fun init_str_template(): StringResource = org.jetbrains.compose.resources.StringResource( "string:str_template", "str_template", setOf( - org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 499, + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 557, 76), ) ) \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/Drawable0.commonMain.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/Drawable0.commonMain.kt index 5e7a25ac0e..0853a96b34 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/Drawable0.commonMain.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/Drawable0.commonMain.kt @@ -12,6 +12,9 @@ private object CommonMainDrawable0 { public val camelCaseName: DrawableResource by lazy { init_camelCaseName() } + public val `is`: DrawableResource by + lazy { init_is() } + public val vector: DrawableResource by lazy { init_vector() } @@ -41,6 +44,16 @@ private fun init_camelCaseName(): DrawableResource = ) ) +internal val Res.drawable.`is`: DrawableResource + get() = CommonMainDrawable0.`is` + +private fun init_is(): DrawableResource = org.jetbrains.compose.resources.DrawableResource( + "drawable:is", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/is.xml", -1, -1), + ) +) + internal val Res.drawable.vector: DrawableResource get() = CommonMainDrawable0.vector diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/String0.commonMain.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/String0.commonMain.kt index c57944b003..570cccd780 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/String0.commonMain.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/commonMainResourceAccessors/app/group/resources_test/generated/resources/String0.commonMain.kt @@ -21,6 +21,9 @@ private object CommonMainString0 { public val hello: StringResource by lazy { init_hello() } + public val `info_using_release_$x`: StringResource by + lazy { `init_info_using_release_$x`() } + public val multi_line: StringResource by lazy { init_multi_line() } @@ -83,13 +86,25 @@ private fun init_hello(): StringResource = org.jetbrains.compose.resources.Strin ) ) +internal val Res.string.`info_using_release_$x`: StringResource + get() = CommonMainString0.`info_using_release_$x` + +private fun `init_info_using_release_$x`(): StringResource = + org.jetbrains.compose.resources.StringResource( + "string:info_using_release_${'$'}x", "info_using_release_${'$'}x", + setOf( + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 320, + 57), + ) + ) + internal val Res.string.multi_line: StringResource get() = CommonMainString0.multi_line private fun init_multi_line(): StringResource = org.jetbrains.compose.resources.StringResource( "string:multi_line", "multi_line", setOf( - org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 320, + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 378, 178), ) ) @@ -100,7 +115,7 @@ internal val Res.string.str_template: StringResource private fun init_str_template(): StringResource = org.jetbrains.compose.resources.StringResource( "string:str_template", "str_template", setOf( - org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 499, + org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.commonMain.cvr", 557, 76), ) ) \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/is.xml b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/is.xml new file mode 100644 index 0000000000..d7bf7955f4 --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/is.xml @@ -0,0 +1,36 @@ + + + + + + + + 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 2afea688cf..06e9e90654 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 @@ -8,6 +8,7 @@ PascalCase 1-kebab-case camelCase + info_using_release_$x %d zero