Skip to content

Commit

Permalink
[gradle] Add a customization for resources directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Jun 24, 2024
1 parent 4c6fe13 commit 193ebab
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,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 = config.flatMap { ext ->
val dirProp = objects.directoryProperty()
dirProp.convention(project.layout.projectDirectory.dir("src/${sourceSet.name}/$COMPOSE_RESOURCES_DIR"))
ext.customResourceDirectories[sourceSet.name]?.let { dirProp.set(it) }
dirProp
}

val preparedComposeResourcesDir = layout.buildDirectory.dir(
"$RES_GEN_DIR/preparedResources/${sourceSet.name}/$COMPOSE_RESOURCES_DIR"
)

val convertXmlValueResources = tasks.register(
"convertXmlValueResourcesFor${sourceSet.name.uppercaseFirstChar()}",
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
)

This file was deleted.

0 comments on commit 193ebab

Please sign in to comment.