Browse Source

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
data-source-prototype-1.10.0-alpha02
Nikita Lipsky 4 months ago committed by GitHub
parent
commit
06d5543378
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt
  2. 23
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt
  3. 12
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

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

@ -153,12 +153,6 @@ private fun Project.configureResourceAccessorsGeneration( @@ -153,12 +153,6 @@ private fun Project.configureResourceAccessorsGeneration(
task.packagingDir.set(packagingDir)
}
task.onlyIf { shouldGenerateCode.get() }
task.generateResourceContentHashAnnotation.set(
project.providers
.systemProperty("compose.resources.generate.ResourceContentHash.annotation")
.map { it.toBoolean() }
.orElse(false)
)
}
//register generated source set

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

@ -14,7 +14,22 @@ import java.io.File @@ -14,7 +14,22 @@ import java.io.File
import java.nio.file.Path
import kotlin.io.path.relativeTo
internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
/**
* Configuration for resource accessors generation.
*/
interface ResourceAccessorsConfiguration {
/**
* Property that defines whether to generate `@ResourceContentHash` annotation for resource accessors.
*
* `@ResourceContentHash` annotation is used to mark resource accessors with the resource content hash.
* It can be used by a client to determine if the resource content is changed or not.
* By default, the annotation is not generated but the client may override it by setting this property to `true`.
*/
val generateResourceContentHashAnnotation: Property<Boolean>
}
internal abstract class GenerateResourceAccessorsTask : IdeaImportTask(), ResourceAccessorsConfiguration {
@get:Input
abstract val packageName: Property<String>
@ -32,7 +47,7 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() { @@ -32,7 +47,7 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
abstract val makeAccessorsPublic: Property<Boolean>
@get:Input
abstract val generateResourceContentHashAnnotation: Property<Boolean>
abstract override val generateResourceContentHashAnnotation: Property<Boolean>
@get:InputFiles
@get:SkipWhenEmpty
@ -42,6 +57,10 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() { @@ -42,6 +57,10 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
@get:OutputDirectory
abstract val codeDir: DirectoryProperty
init {
generateResourceContentHashAnnotation.convention(false)
}
override fun safeAction() {
val kotlinDir = codeDir.get().asFile
val rootResDir = resDir.get()

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

@ -586,8 +586,18 @@ class ResourcesTest : GradlePluginTestBase() { @@ -586,8 +586,18 @@ class ResourcesTest : GradlePluginTestBase() {
@Test
fun testGeneratedAccessorsAnnotatedWithResourceContentHash(): Unit = with(testProject("misc/commonResources")) {
file("build.gradle.kts").appendText(
"""
tasks.configureEach {
if (this is org.jetbrains.compose.resources.ResourceAccessorsConfiguration) {
generateResourceContentHashAnnotation.set(true)
}
}
""".trimIndent()
)
//check generated resource's accessors
gradle("prepareKotlinIdeaImport", "-Dcompose.resources.generate.ResourceContentHash.annotation=true").checks {
gradle("prepareKotlinIdeaImport").checks {
val expected = if (System.getProperty("os.name").lowercase().contains("windows")) {
// Windows has different line endings in comparison with Unixes,
// thus the XML resource files differ and produce different content hashes,

Loading…
Cancel
Save