Skip to content

Commit 06d5543

Browse files
authored
Provide public API for ResourceContentHash annotation generation (#5402)
Provides `generateResourceContentHashAnnotation` property for `generateResourceAccessorsFor*` tasks that defines whether to generate `@ResourceContentHash` annotation for resource accessors. The property can be set the following way: ``` tasks.configureEach { if (this is org.jetbrains.compose.resources.ResourceAccessorsConfiguration) { generateResourceContentHashAnnotation.set(true) } } ``` ## Release Notes ### Features - Multiple Platforms - Provide public API for `@ResourceContentHash` annotation generation
1 parent 783612a commit 06d5543

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ private fun Project.configureResourceAccessorsGeneration(
153153
task.packagingDir.set(packagingDir)
154154
}
155155
task.onlyIf { shouldGenerateCode.get() }
156-
task.generateResourceContentHashAnnotation.set(
157-
project.providers
158-
.systemProperty("compose.resources.generate.ResourceContentHash.annotation")
159-
.map { it.toBoolean() }
160-
.orElse(false)
161-
)
162156
}
163157

164158
//register generated source set

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,22 @@ import java.io.File
1414
import java.nio.file.Path
1515
import kotlin.io.path.relativeTo
1616

17-
internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
17+
/**
18+
* Configuration for resource accessors generation.
19+
*/
20+
interface ResourceAccessorsConfiguration {
21+
22+
/**
23+
* Property that defines whether to generate `@ResourceContentHash` annotation for resource accessors.
24+
*
25+
* `@ResourceContentHash` annotation is used to mark resource accessors with the resource content hash.
26+
* It can be used by a client to determine if the resource content is changed or not.
27+
* By default, the annotation is not generated but the client may override it by setting this property to `true`.
28+
*/
29+
val generateResourceContentHashAnnotation: Property<Boolean>
30+
}
31+
32+
internal abstract class GenerateResourceAccessorsTask : IdeaImportTask(), ResourceAccessorsConfiguration {
1833
@get:Input
1934
abstract val packageName: Property<String>
2035

@@ -32,7 +47,7 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
3247
abstract val makeAccessorsPublic: Property<Boolean>
3348

3449
@get:Input
35-
abstract val generateResourceContentHashAnnotation: Property<Boolean>
50+
abstract override val generateResourceContentHashAnnotation: Property<Boolean>
3651

3752
@get:InputFiles
3853
@get:SkipWhenEmpty
@@ -42,6 +57,10 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
4257
@get:OutputDirectory
4358
abstract val codeDir: DirectoryProperty
4459

60+
init {
61+
generateResourceContentHashAnnotation.convention(false)
62+
}
63+
4564
override fun safeAction() {
4665
val kotlinDir = codeDir.get().asFile
4766
val rootResDir = resDir.get()

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,18 @@ class ResourcesTest : GradlePluginTestBase() {
586586

587587
@Test
588588
fun testGeneratedAccessorsAnnotatedWithResourceContentHash(): Unit = with(testProject("misc/commonResources")) {
589+
file("build.gradle.kts").appendText(
590+
"""
591+
tasks.configureEach {
592+
if (this is org.jetbrains.compose.resources.ResourceAccessorsConfiguration) {
593+
generateResourceContentHashAnnotation.set(true)
594+
}
595+
}
596+
""".trimIndent()
597+
)
598+
589599
//check generated resource's accessors
590-
gradle("prepareKotlinIdeaImport", "-Dcompose.resources.generate.ResourceContentHash.annotation=true").checks {
600+
gradle("prepareKotlinIdeaImport").checks {
591601
val expected = if (System.getProperty("os.name").lowercase().contains("windows")) {
592602
// Windows has different line endings in comparison with Unixes,
593603
// thus the XML resource files differ and produce different content hashes,

0 commit comments

Comments
 (0)