Browse Source

[gradle] Add a customization for resources directories. (#5016)

With the new API it is possible to customize compose resources
directories. For example:

```kotlin
abstract class GenerateAndroidRes : DefaultTask() {
    @get:Inject
    abstract val layout: ProjectLayout

    @get:OutputDirectory
    val outputDir = layout.buildDirectory.dir("generatedAndroidResources")

    @TaskAction
    fun run() {...}
}
compose.resources {
    customDirectory(
        sourceSetName = "androidMain",
        directoryProvider = tasks.register<GenerateAndroidRes>("generateAndroidRes").map { it.outputDir.get() }
    )
    customDirectory(
        sourceSetName = "desktopMain",
        directoryProvider = provider { layout.projectDirectory.dir("desktopResources") }
    )
}
```

<!-- Optional -->
Fixes https://github.com/JetBrains/compose-multiplatform/issues/4718
Fixes https://github.com/JetBrains/compose-multiplatform/issues/4564

## Release Notes
### Features - Resources
- Add a customization for resources directories. Now it is possible to
use e.g downloaded resources.
pull/3640/merge v1.7.0-dev1703
Konstantin 6 months ago committed by GitHub
parent
commit
c86df61183
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt
  2. 27
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/PrepareComposeResources.kt
  3. 13
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesDSL.kt
  4. 28
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/build.gradle.kts
  5. 3
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/androidMain/composeResources/values/strings.xml

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

@ -54,7 +54,7 @@ internal fun Project.configureComposeResourcesGeneration(
}
//common resources must be converted (XML -> CVR)
val preparedResourcesTask = registerPrepareComposeResourcesTask(sourceSet)
val preparedResourcesTask = registerPrepareComposeResourcesTask(sourceSet, config)
val preparedResources = preparedResourcesTask.flatMap { it.outputDir.asFile }
configureResourceAccessorsGeneration(
sourceSet,

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

@ -1,6 +1,7 @@
package org.jetbrains.compose.resources
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.FileTree
@ -25,11 +26,19 @@ import javax.inject.Inject
import javax.xml.parsers.DocumentBuilderFactory
internal fun Project.registerPrepareComposeResourcesTask(
sourceSet: KotlinSourceSet
sourceSet: KotlinSourceSet,
config: Provider<ResourcesExtension>
): TaskProvider<PrepareComposeResourcesTask> {
val resDir = "${sourceSet.name}/$COMPOSE_RESOURCES_DIR"
val userComposeResourcesDir = project.projectDir.resolve("src/$resDir")
val preparedComposeResourcesDir = layout.buildDirectory.dir("$RES_GEN_DIR/preparedResources/$resDir")
val userComposeResourcesDir: Provider<Directory> = config.flatMap { ext ->
ext.customResourceDirectories[sourceSet.name] ?: provider {
//default path
layout.projectDirectory.dir("src/${sourceSet.name}/$COMPOSE_RESOURCES_DIR")
}
}
val preparedComposeResourcesDir = layout.buildDirectory.dir(
"$RES_GEN_DIR/preparedResources/${sourceSet.name}/$COMPOSE_RESOURCES_DIR"
)
val convertXmlValueResources = tasks.register(
"convertXmlValueResourcesFor${sourceSet.name.uppercaseFirstChar()}",
@ -61,11 +70,11 @@ internal fun Project.registerPrepareComposeResourcesTask(
}
internal fun Project.getPreparedComposeResourcesDir(sourceSet: KotlinSourceSet): Provider<File> = tasks
.named(
getPrepareComposeResourcesTaskName(sourceSet),
PrepareComposeResourcesTask::class.java
)
.flatMap { it.outputDir.asFile }
.named(
getPrepareComposeResourcesTaskName(sourceSet),
PrepareComposeResourcesTask::class.java
)
.flatMap { it.outputDir.asFile }
private fun getPrepareComposeResourcesTaskName(sourceSet: KotlinSourceSet) =
"prepareComposeResourcesTaskFor${sourceSet.name.uppercaseFirstChar()}"

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

@ -1,6 +1,7 @@
package org.jetbrains.compose.resources
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.provider.Provider
import java.io.File
@ -36,6 +37,18 @@ abstract class ResourcesExtension {
* - `never`: Never generate the Res class.
*/
var generateResClass: ResourceClassGeneration = auto
internal val customResourceDirectories: MutableMap<String, Provider<Directory>> = mutableMapOf()
/**
* Associates a custom resource directory with a specific source set.
*
* @param sourceSetName the name of the source set to associate the custom resource directory with
* @param directoryProvider the provider that provides the custom directory
*/
fun customDirectory(sourceSetName: String, directoryProvider: Provider<Directory>) {
customResourceDirectories[sourceSetName] = directoryProvider
}
}
internal fun Provider<ResourcesExtension>.getResourcePackage(project: Project) = map { config ->

28
gradle-plugins/compose/src/test/test-projects/misc/commonResources/build.gradle.kts

@ -62,3 +62,31 @@ android {
targetCompatibility = JavaVersion.VERSION_11
}
}
abstract class GenerateAndroidRes : DefaultTask() {
@get:Inject
abstract val layout: ProjectLayout
@get:OutputDirectory
val outputDir = layout.buildDirectory.dir("generatedAndroidResources")
@TaskAction
fun run() {
val dir = outputDir.get().asFile
dir.deleteRecursively()
File(dir, "values/strings.xml").apply {
parentFile.mkdirs()
writeText(
"""
<resources>
<string name="android_str">Android string</string>
</resources>
""".trimIndent()
)
}
}
}
compose.resources.customDirectory(
sourceSetName = "androidMain",
directoryProvider = tasks.register<GenerateAndroidRes>("generateAndroidRes").map { it.outputDir.get() }
)

3
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/androidMain/composeResources/values/strings.xml

@ -1,3 +0,0 @@
<resources>
<string name="android_str">Android string</string>
</resources>
Loading…
Cancel
Save