Skip to content

Commit d8f7123

Browse files
committed
Add ResourceContentHash annotation to generated resource accessors.
Used by Compose Hot Reload to mark a related accessor as dirty if the hash is changed after reloading of new accessors.
1 parent 5f43a2a commit d8f7123

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jetbrains.compose.resources
2+
3+
@Suppress("unused")
4+
@Retention(AnnotationRetention.BINARY)
5+
annotation class ResourceContentHash(val hash: Int)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
110110
}
111111

112112
val type = ResourceType.fromString(typeString) ?: error("Unknown resource type: '$typeString'.")
113-
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path))
113+
return listOf(ResourceItem(type, qualifiers, file.nameWithoutExtension.asUnderscoredIdentifier(), path,
114+
file.readBytes().contentHashCode()))
114115
}
115116

116117
private fun getValueResourceItems(dataFile: File, qualifiers: List<String>, path: Path): List<ResourceItem> {
@@ -141,7 +142,8 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
141142
path: Path
142143
): ResourceItem {
143144
val record = ValueResourceRecord.createFromString(recordString)
144-
return ResourceItem(record.type, qualifiers, record.key.asUnderscoredIdentifier(), path, offset, size)
145+
return ResourceItem(record.type, qualifiers, record.key.asUnderscoredIdentifier(), path,
146+
record.content.hashCode(), offset, size)
145147
}
146148
}
147149

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal data class ResourceItem(
3939
val qualifiers: List<String>,
4040
val name: String,
4141
val path: Path,
42+
val contentHash: Int,
4243
val offset: Long = -1,
4344
val size: Long = -1,
4445
)
@@ -58,6 +59,8 @@ private val resourceItemClass = ClassName("org.jetbrains.compose.resources", "Re
5859
private val internalAnnotationClass = ClassName("org.jetbrains.compose.resources", "InternalResourceApi")
5960
private val internalAnnotation = AnnotationSpec.builder(internalAnnotationClass).build()
6061

62+
private val resourceContentHashAnnotationClass = ClassName("org.jetbrains.compose.resources", "ResourceContentHash")
63+
6164
private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlock.Builder {
6265
val languageQualifier = ClassName("org.jetbrains.compose.resources", "LanguageQualifier")
6366
val regionQualifier = ClassName("org.jetbrains.compose.resources", "RegionQualifier")
@@ -283,7 +286,12 @@ private fun getChunkFileSpec(
283286
.endControlFlow()
284287
.build()
285288

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+
286293
val accessor = PropertySpec.builder(resName, type.getClassName(), resModifier)
294+
.addAnnotation(resourceContentHashAnnotation)
287295
.receiver(ClassName(packageName, resClassName, type.accessorName))
288296
.delegate(initializer)
289297
.build()

0 commit comments

Comments
 (0)