Skip to content

Commit 8bbf398

Browse files
committed
Generate ResourceContentHash annotation only when the respective system property is set for the Gradle task.
1 parent 999f11b commit 8bbf398

16 files changed

Lines changed: 306 additions & 8 deletions

File tree

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,17 @@ private fun getChunkFileSpec(
286286
.endControlFlow()
287287
.build()
288288

289-
val resourceContentHashAnnotation = AnnotationSpec.builder(resourceContentHashAnnotationClass)
290-
.useSiteTarget(AnnotationSpec.UseSiteTarget.DELEGATE)
291-
.addMember("%L", items.fold(0){acc, item -> ((acc * 31) + item.contentHash)}).build()
292-
293-
val accessor = PropertySpec.builder(resName, type.getClassName(), resModifier)
294-
.addAnnotation(resourceContentHashAnnotation)
289+
val accessorBuilder = PropertySpec.builder(resName, type.getClassName(), resModifier)
295290
.receiver(ClassName(packageName, resClassName, type.accessorName))
296291
.delegate(initializer)
297-
.build()
298-
chunkFile.addProperty(accessor)
292+
if (System.getProperty("compose.resources.generate.ResourceContentHash.annotation") == "true") {
293+
accessorBuilder.addAnnotation(
294+
AnnotationSpec.builder(resourceContentHashAnnotationClass)
295+
.useSiteTarget(AnnotationSpec.UseSiteTarget.DELEGATE)
296+
.addMember("%L", items.fold(0){acc, item -> ((acc * 31) + item.contentHash)}).build()
297+
)
298+
}
299+
chunkFile.addProperty(accessorBuilder.build())
299300
}
300301

301302
//__collect${chunkClassName}Resources function

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ class ResourcesTest : GradlePluginTestBase() {
578578
}
579579
}
580580

581+
@Test
582+
fun testGeneratedAccessorsAnnotatedWithResourceContentHash(): Unit = with(testProject("misc/commonResourcesAnnotatedWithResourceContentHash")) {
583+
//check generated resource's accessors
584+
gradle("prepareKotlinIdeaImport", "-Dcompose.resources.generate.ResourceContentHash.annotation=true").checks {
585+
assertDirectoriesContentEquals(
586+
file("build/generated/compose/resourceGenerator/kotlin"),
587+
file("expected")
588+
)
589+
}
590+
}
591+
581592
@Test
582593
fun testJvmOnlyProject(): Unit = with(testProject("misc/jvmOnlyResources")) {
583594
gradle("jar").checks {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
kotlin("multiplatform")
3+
kotlin("plugin.compose")
4+
id("org.jetbrains.compose")
5+
}
6+
7+
group = "app.group"
8+
9+
kotlin {
10+
jvm("desktop")
11+
12+
sourceSets {
13+
commonMain {
14+
dependencies {
15+
implementation(compose.runtime)
16+
implementation(compose.material)
17+
implementation(compose.components.resources)
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@file:OptIn(InternalResourceApi::class)
2+
3+
package app.group.resources_test.generated.resources
4+
5+
import kotlin.OptIn
6+
import kotlin.String
7+
import kotlin.collections.MutableMap
8+
import org.jetbrains.compose.resources.DrawableResource
9+
import org.jetbrains.compose.resources.InternalResourceApi
10+
import org.jetbrains.compose.resources.ResourceContentHash
11+
import org.jetbrains.compose.resources.ResourceItem
12+
13+
private const val MD: String = "composeResources/app.group.resources_test.generated.resources/"
14+
15+
@delegate:ResourceContentHash(-1_557_874_605)
16+
internal val Res.drawable.raster: DrawableResource by lazy {
17+
DrawableResource("drawable:raster", setOf(
18+
ResourceItem(setOf(), "${MD}drawable/raster.png", -1, -1),
19+
))
20+
}
21+
22+
@InternalResourceApi
23+
internal fun _collectCommonMainDrawable0Resources(map: MutableMap<String, DrawableResource>) {
24+
map.put("raster", Res.drawable.raster)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@file:OptIn(InternalResourceApi::class)
2+
3+
package app.group.resources_test.generated.resources
4+
5+
import kotlin.OptIn
6+
import kotlin.String
7+
import kotlin.collections.MutableMap
8+
import org.jetbrains.compose.resources.FontResource
9+
import org.jetbrains.compose.resources.InternalResourceApi
10+
import org.jetbrains.compose.resources.ResourceContentHash
11+
import org.jetbrains.compose.resources.ResourceItem
12+
13+
private const val MD: String = "composeResources/app.group.resources_test.generated.resources/"
14+
15+
@delegate:ResourceContentHash(1_132_920_421)
16+
internal val Res.font.emptyFont: FontResource by lazy {
17+
FontResource("font:emptyFont", setOf(
18+
ResourceItem(setOf(), "${MD}font/emptyFont.otf", -1, -1),
19+
))
20+
}
21+
22+
@InternalResourceApi
23+
internal fun _collectCommonMainFont0Resources(map: MutableMap<String, FontResource>) {
24+
map.put("emptyFont", Res.font.emptyFont)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@file:OptIn(InternalResourceApi::class)
2+
3+
package app.group.resources_test.generated.resources
4+
5+
import kotlin.OptIn
6+
import kotlin.String
7+
import kotlin.collections.MutableMap
8+
import org.jetbrains.compose.resources.InternalResourceApi
9+
import org.jetbrains.compose.resources.PluralStringResource
10+
import org.jetbrains.compose.resources.ResourceContentHash
11+
import org.jetbrains.compose.resources.ResourceItem
12+
13+
private const val MD: String = "composeResources/app.group.resources_test.generated.resources/"
14+
15+
@delegate:ResourceContentHash(-759_003_763)
16+
internal val Res.plurals.plurals: PluralStringResource by lazy {
17+
PluralStringResource("plurals:plurals", "plurals", setOf(
18+
ResourceItem(setOf(), "${MD}values/strings.commonMain.cvr", 10, 34),
19+
))
20+
}
21+
22+
@InternalResourceApi
23+
internal fun _collectCommonMainPlurals0Resources(map: MutableMap<String, PluralStringResource>) {
24+
map.put("plurals", Res.plurals.plurals)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@file:OptIn(InternalResourceApi::class)
2+
3+
package app.group.resources_test.generated.resources
4+
5+
import kotlin.OptIn
6+
import kotlin.String
7+
import kotlin.collections.MutableMap
8+
import org.jetbrains.compose.resources.InternalResourceApi
9+
import org.jetbrains.compose.resources.ResourceContentHash
10+
import org.jetbrains.compose.resources.ResourceItem
11+
import org.jetbrains.compose.resources.StringResource
12+
13+
private const val MD: String = "composeResources/app.group.resources_test.generated.resources/"
14+
15+
@delegate:ResourceContentHash(64_828_505)
16+
internal val Res.string.app_name: StringResource by lazy {
17+
StringResource("string:app_name", "app_name", setOf(
18+
ResourceItem(setOf(), "${MD}values/strings.commonMain.cvr", 45, 44),
19+
))
20+
}
21+
22+
@InternalResourceApi
23+
internal fun _collectCommonMainString0Resources(map: MutableMap<String, StringResource>) {
24+
map.put("app_name", Res.string.app_name)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package app.group.resources_test.generated.resources
2+
3+
import kotlin.String
4+
import kotlin.collections.Map
5+
import org.jetbrains.compose.resources.DrawableResource
6+
import org.jetbrains.compose.resources.FontResource
7+
import org.jetbrains.compose.resources.PluralStringResource
8+
import org.jetbrains.compose.resources.StringArrayResource
9+
import org.jetbrains.compose.resources.StringResource
10+
11+
internal expect val Res.allDrawableResources: Map<String, DrawableResource>
12+
13+
internal expect val Res.allStringResources: Map<String, StringResource>
14+
15+
internal expect val Res.allStringArrayResources: Map<String, StringArrayResource>
16+
17+
internal expect val Res.allPluralStringResources: Map<String, PluralStringResource>
18+
19+
internal expect val Res.allFontResources: Map<String, FontResource>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@file:OptIn(InternalResourceApi::class)
2+
@file:Suppress(
3+
"RedundantVisibilityModifier",
4+
"REDUNDANT_VISIBILITY_MODIFIER",
5+
)
6+
7+
package app.group.resources_test.generated.resources
8+
9+
import kotlin.ByteArray
10+
import kotlin.OptIn
11+
import kotlin.String
12+
import kotlin.Suppress
13+
import org.jetbrains.compose.resources.InternalResourceApi
14+
import org.jetbrains.compose.resources.getResourceUri
15+
import org.jetbrains.compose.resources.readResourceBytes
16+
17+
internal object Res {
18+
/**
19+
* Reads the content of the resource file at the specified path and returns it as a byte array.
20+
*
21+
* Example: `val bytes = Res.readBytes("files/key.bin")`
22+
*
23+
* @param path The path of the file to read in the compose resource's directory.
24+
* @return The content of the file as a byte array.
25+
*/
26+
public suspend fun readBytes(path: String): ByteArray = readResourceBytes("composeResources/app.group.resources_test.generated.resources/" + path)
27+
28+
/**
29+
* Returns the URI string of the resource file at the specified path.
30+
*
31+
* Example: `val uri = Res.getUri("files/key.bin")`
32+
*
33+
* @param path The path of the file in the compose resource's directory.
34+
* @return The URI string of the file.
35+
*/
36+
public fun getUri(path: String): String = getResourceUri("composeResources/app.group.resources_test.generated.resources/" + path)
37+
38+
public object drawable
39+
40+
public object string
41+
42+
public object array
43+
44+
public object plurals
45+
46+
public object font
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@file:OptIn(org.jetbrains.compose.resources.InternalResourceApi::class)
2+
3+
package app.group.resources_test.generated.resources
4+
5+
import kotlin.OptIn
6+
import kotlin.String
7+
import kotlin.collections.Map
8+
import org.jetbrains.compose.resources.DrawableResource
9+
import org.jetbrains.compose.resources.FontResource
10+
import org.jetbrains.compose.resources.PluralStringResource
11+
import org.jetbrains.compose.resources.StringArrayResource
12+
import org.jetbrains.compose.resources.StringResource
13+
14+
internal actual val Res.allDrawableResources: Map<String, DrawableResource> by lazy {
15+
val map = mutableMapOf<String, DrawableResource>()
16+
_collectCommonMainDrawable0Resources(map)
17+
return@lazy map
18+
}
19+
20+
internal actual val Res.allStringResources: Map<String, StringResource> by lazy {
21+
val map = mutableMapOf<String, StringResource>()
22+
_collectCommonMainString0Resources(map)
23+
return@lazy map
24+
}
25+
26+
internal actual val Res.allStringArrayResources: Map<String, StringArrayResource> by lazy {
27+
val map = mutableMapOf<String, StringArrayResource>()
28+
return@lazy map
29+
}
30+
31+
internal actual val Res.allPluralStringResources: Map<String, PluralStringResource> by lazy {
32+
val map = mutableMapOf<String, PluralStringResource>()
33+
_collectCommonMainPlurals0Resources(map)
34+
return@lazy map
35+
}
36+
37+
internal actual val Res.allFontResources: Map<String, FontResource> by lazy {
38+
val map = mutableMapOf<String, FontResource>()
39+
_collectCommonMainFont0Resources(map)
40+
return@lazy map
41+
}

0 commit comments

Comments
 (0)