Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal fun Project.configureComposeResourcesGeneration(
}
}
val packageName = config.getResourcePackage(project)
val resClassName = config.map { it.nameOfResClass }
val makeAccessorsPublic = config.map { it.publicResClass }
val packagingDir = config.getModuleResourcesDir(project)

Expand All @@ -53,6 +54,7 @@ internal fun Project.configureComposeResourcesGeneration(
sourceSet,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic,
packagingDir,
generateModulePath
Expand All @@ -67,13 +69,20 @@ internal fun Project.configureComposeResourcesGeneration(
preparedResources,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic,
packagingDir,
generateModulePath
)
}

configureResourceCollectorsGeneration(kotlinExtension, shouldGenerateCode, packageName, makeAccessorsPublic)
configureResourceCollectorsGeneration(
kotlinExtension,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic
)

//setup task execution during IDE import
tasks.configureEach { importTask ->
Expand All @@ -87,6 +96,7 @@ private fun Project.configureResClassGeneration(
resClassSourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
packagingDir: Provider<File>,
generateModulePath: Boolean
Expand All @@ -98,6 +108,7 @@ private fun Project.configureResClassGeneration(
GenerateResClassTask::class.java
) { task ->
task.packageName.set(packageName)
task.resClassName.set(resClassName)
task.makeAccessorsPublic.set(makeAccessorsPublic)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/commonResClass"))

Expand All @@ -120,6 +131,7 @@ private fun Project.configureResourceAccessorsGeneration(
resourcesDir: Provider<File>,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
packagingDir: Provider<File>,
generateModulePath: Boolean
Expand All @@ -131,6 +143,7 @@ private fun Project.configureResourceAccessorsGeneration(
GenerateResourceAccessorsTask::class.java
) { task ->
task.packageName.set(packageName)
task.resClassName.set(resClassName)
task.sourceSetName.set(sourceSet.name)
task.makeAccessorsPublic.set(makeAccessorsPublic)
task.resDir.set(resourcesDir)
Expand Down Expand Up @@ -159,6 +172,7 @@ private fun Project.configureResourceCollectorsGeneration(
kotlinExtension: KotlinProjectExtension,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>
) {
if (kotlinExtension is KotlinMultiplatformExtension) {
Expand All @@ -169,6 +183,7 @@ private fun Project.configureResourceCollectorsGeneration(
commonMainSourceSet,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic
)
}
Expand All @@ -180,6 +195,7 @@ private fun Project.configureResourceCollectorsGeneration(
androidMain,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic,
true
)
Expand All @@ -190,6 +206,7 @@ private fun Project.configureResourceCollectorsGeneration(
compilation.defaultSourceSet,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic,
true
)
Expand All @@ -205,6 +222,7 @@ private fun Project.configureResourceCollectorsGeneration(
compilation.defaultSourceSet,
shouldGenerateCode,
packageName,
resClassName,
makeAccessorsPublic,
false
)
Expand All @@ -217,6 +235,7 @@ private fun Project.configureExpectResourceCollectorsGeneration(
sourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>
) {
logger.info("Configure expect resource collectors generation for ${sourceSet.name}")
Expand All @@ -227,6 +246,7 @@ private fun Project.configureExpectResourceCollectorsGeneration(
GenerateExpectResourceCollectorsTask::class.java
) { task ->
task.packageName.set(packageName)
task.resClassName.set(resClassName)
task.makeAccessorsPublic.set(makeAccessorsPublic)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors"))
task.onlyIf { shouldGenerateCode.get() }
Expand All @@ -244,6 +264,7 @@ private fun Project.configureActualResourceCollectorsGeneration(
sourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
useActualModifier: Boolean
) {
Expand All @@ -269,6 +290,7 @@ private fun Project.configureActualResourceCollectorsGeneration(
GenerateActualResourceCollectorsTask::class.java
) { task ->
task.packageName.set(packageName)
task.resClassName.set(resClassName)
task.makeAccessorsPublic.set(makeAccessorsPublic)
task.useActualModifier.set(useActualModifier)
task.resourceAccessorDirs.from(accessorDirs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import org.jetbrains.compose.internal.IdeaImportTask
import java.io.File

internal abstract class GenerateResClassTask : IdeaImportTask() {
companion object {
private const val RES_FILE_NAME = "Res"
}

@get:Input
abstract val packageName: Property<String>

@get:Input
abstract val resClassName: Property<String>
Copy link
Collaborator

@igordmn igordmn Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we name it just className? To be consistent with packageName and to avoid double wording in resources { res... =

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because it changes the Res class name only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the consistency I'll rename it to nameOfResClass


@get:Input
@get:Optional
abstract val packagingDir: Property<File>
Expand All @@ -31,11 +30,12 @@ internal abstract class GenerateResClassTask : IdeaImportTask() {
dir.deleteRecursively()
dir.mkdirs()

logger.info("Generate $RES_FILE_NAME.kt")
val resClassName = resClassName.get()
logger.info("Generate $resClassName.kt")

val pkgName = packageName.get()
val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: ""
val isPublic = makeAccessorsPublic.get()
getResFileSpec(pkgName, RES_FILE_NAME, moduleDirectory, isPublic).writeTo(dir)
getResFileSpec(pkgName, resClassName, moduleDirectory, isPublic).writeTo(dir)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
@get:Input
abstract val packageName: Property<String>

@get:Input
abstract val resClassName: Property<String>

@get:Input
abstract val sourceSetName: Property<String>

Expand Down Expand Up @@ -68,9 +71,15 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {

val pkgName = packageName.get()
val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: ""
val resClassName = resClassName.get()
val isPublic = makeAccessorsPublic.get()
getAccessorsSpecs(
resources, pkgName, sourceSet, moduleDirectory, isPublic
resources,
pkgName,
sourceSet,
moduleDirectory,
resClassName,
isPublic
).forEach { it.writeTo(kotlinDir) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask()
@get:Input
abstract val packageName: Property<String>

@get:Input
abstract val resClassName: Property<String>

@get:Input
abstract val makeAccessorsPublic: Property<Boolean>

Expand All @@ -31,8 +34,14 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask()
logger.info("Generate expect ResourceCollectors for $kotlinDir")

val pkgName = packageName.get()
val resClassName = resClassName.get()
val isPublic = makeAccessorsPublic.get()
val spec = getExpectResourceCollectorsFileSpec(pkgName, "ExpectResourceCollectors", isPublic)
val spec = getExpectResourceCollectorsFileSpec(
packageName = pkgName,
fileName = "ExpectResourceCollectors",
resClassName = resClassName,
isPublic = isPublic
)
spec.writeTo(kotlinDir)
}
}
Expand All @@ -41,6 +50,9 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask()
@get:Input
abstract val packageName: Property<String>

@get:Input
abstract val resClassName: Property<String>

@get:Input
abstract val makeAccessorsPublic: Property<Boolean>

Expand Down Expand Up @@ -89,14 +101,16 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask()
}.groupBy({ it.first }, { it.second })

val pkgName = packageName.get()
val resClassName = resClassName.get()
val isPublic = makeAccessorsPublic.get()
val useActual = useActualModifier.get()
val spec = getActualResourceCollectorsFileSpec(
pkgName,
"ActualResourceCollectors",
isPublic,
useActual,
funNames
packageName = pkgName,
fileName = "ActualResourceCollectors",
resClassName = resClassName,
isPublic = isPublic,
useActualModifier = useActual,
typeToCollectorFunctions = funNames
)
spec.writeTo(kotlinDir)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo

internal fun getResFileSpec(
packageName: String,
fileName: String,
className: String,
moduleDir: String,
isPublic: Boolean
): FileSpec {
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL
return FileSpec.builder(packageName, fileName).also { file ->
return FileSpec.builder(packageName, className).also { file ->
file.addAnnotation(
AnnotationSpec.builder(ClassName("kotlin", "OptIn"))
.addMember("%T::class", internalAnnotationClass)
Expand All @@ -142,7 +142,7 @@ internal fun getResFileSpec(
.addMember("%S", "REDUNDANT_VISIBILITY_MODIFIER")
.build()
)
file.addType(TypeSpec.objectBuilder("Res").also { resObject ->
file.addType(TypeSpec.objectBuilder(className).also { resObject ->
resObject.addModifiers(resModifier)

//readFileBytes
Expand All @@ -153,7 +153,7 @@ internal fun getResFileSpec(
"""
Reads the content of the resource file at the specified path and returns it as a byte array.
Example: `val bytes = Res.readBytes("files/key.bin")`
Example: `val bytes = ${className}.readBytes("files/key.bin")`
@param path The path of the file to read in the compose resource's directory.
@return The content of the file as a byte array.
Expand All @@ -174,7 +174,7 @@ internal fun getResFileSpec(
"""
Returns the URI string of the resource file at the specified path.
Example: `val uri = Res.getUri("files/key.bin")`
Example: `val uri = ${className}.getUri("files/key.bin")`
@param path The path of the file in the compose resource's directory.
@return The URI string of the file.
Expand Down Expand Up @@ -208,6 +208,7 @@ internal fun getAccessorsSpecs(
packageName: String,
sourceSetName: String,
moduleDir: String,
resClassName: String,
isPublic: Boolean
): List<FileSpec> {
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL
Expand All @@ -225,6 +226,7 @@ internal fun getAccessorsSpecs(
sourceSetName.uppercaseFirstChar() + type.accessorName.uppercaseFirstChar() + index,
packageName,
moduleDir,
resClassName,
resModifier,
idToResources.subMap(ids.first(), true, ids.last(), true)
)
Expand All @@ -241,6 +243,7 @@ private fun getChunkFileSpec(
chunkClassName: String,
packageName: String,
moduleDir: String,
resClassName: String,
resModifier: KModifier,
idToResources: Map<String, List<ResourceItem>>
): FileSpec {
Expand Down Expand Up @@ -281,7 +284,7 @@ private fun getChunkFileSpec(
.build()

val accessor = PropertySpec.builder(resName, type.getClassName(), resModifier)
.receiver(ClassName(packageName, "Res", type.accessorName))
.receiver(ClassName(packageName, resClassName, type.accessorName))
.delegate(initializer)
.build()
chunkFile.addProperty(accessor)
Expand Down Expand Up @@ -309,6 +312,7 @@ private fun getChunkFileSpec(
internal fun getExpectResourceCollectorsFileSpec(
packageName: String,
fileName: String,
resClassName: String,
isPublic: Boolean
): FileSpec {
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL
Expand All @@ -323,7 +327,7 @@ internal fun getExpectResourceCollectorsFileSpec(
KModifier.EXPECT,
resModifier
)
.receiver(ClassName(packageName, "Res"))
.receiver(ClassName(packageName, resClassName))
.build()
)
}
Expand All @@ -333,6 +337,7 @@ internal fun getExpectResourceCollectorsFileSpec(
internal fun getActualResourceCollectorsFileSpec(
packageName: String,
fileName: String,
resClassName: String,
isPublic: Boolean,
useActualModifier: Boolean, //e.g. java only project doesn't need actual modifiers
typeToCollectorFunctions: Map<ResourceType, List<String>>
Expand Down Expand Up @@ -370,7 +375,7 @@ internal fun getActualResourceCollectorsFileSpec(
MAP.parameterizedBy(String::class.asClassName(), typeClassName),
mods
)
.receiver(ClassName(packageName, "Res"))
.receiver(ClassName(packageName, resClassName))
.delegate(initBlock)
.build()
file.addProperty(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ abstract class ResourcesExtension {
*/
var packageOfResClass: String = ""

/**
* The name of the generated resources accessors class.
*
* The default is "Res".
*/
var nameOfResClass: String = "Res"

enum class ResourceClassGeneration { Auto, Always, Never }

//to support groovy DSL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class ResourcesTest : GradlePluginTestBase() {
compose.resources {
publicResClass = true
packageOfResClass = "my.lib.res"
nameOfResClass = "MyRes"
}
""".trimIndent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jetbrains.compose.resources.StringResource

private const val MD: String = "composeResources/my.lib.res/"

public val Res.string.android_str: StringResource by lazy {
public val MyRes.string.android_str: StringResource by lazy {
StringResource("string:android_str", "android_str", setOf(
ResourceItem(setOf(), "${MD}values/strings.androidMain.cvr", 10, 39),
))
Expand Down
Loading
Loading