Skip to content

Commit

Permalink
Support generated custom resources class name
Browse files Browse the repository at this point in the history
  • Loading branch information
Omico committed Feb 11, 2025
1 parent c83a7d8 commit 4925231
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
2 changes: 2 additions & 0 deletions gradle-plugins/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ jacoco.exec
# Layout Inspector creates it.
captures/
__pycache__

.kotlin/
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.resClassName }
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,9 +96,10 @@ private fun Project.configureResClassGeneration(
resClassSourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
packagingDir: Provider<File>,
generateModulePath: Boolean
generateModulePath: Boolean,
) {
logger.info("Configure Res class generation for ${resClassSourceSet.name}")

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,9 +131,10 @@ private fun Project.configureResourceAccessorsGeneration(
resourcesDir: Provider<File>,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
packagingDir: Provider<File>,
generateModulePath: Boolean
generateModulePath: Boolean,
) {
logger.info("Configure resource accessors generation for ${sourceSet.name}")

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,7 +172,8 @@ private fun Project.configureResourceCollectorsGeneration(
kotlinExtension: KotlinProjectExtension,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
) {
if (kotlinExtension is KotlinMultiplatformExtension) {
kotlinExtension.sourceSets
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,7 +235,8 @@ private fun Project.configureExpectResourceCollectorsGeneration(
sourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>
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,8 +264,9 @@ private fun Project.configureActualResourceCollectorsGeneration(
sourceSet: KotlinSourceSet,
shouldGenerateCode: Provider<Boolean>,
packageName: Provider<String>,
resClassName: Provider<String>,
makeAccessorsPublic: Provider<Boolean>,
useActualModifier: Boolean
useActualModifier: Boolean,
) {
val taskName = "generateActualResourceCollectorsFor${sourceSet.name.uppercaseFirstChar()}"
if (tasks.names.contains(taskName)) {
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 All @@ -282,4 +304,4 @@ private fun Project.configureActualResourceCollectorsGeneration(
if (flag) listOf(task.codeDir) else emptyList()
}
)
}
}
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>

@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,10 @@ 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,9 @@ 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(pkgName, "ExpectResourceCollectors", resClassName, isPublic)
spec.writeTo(kotlinDir)
}
}
Expand All @@ -41,6 +45,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,11 +96,13 @@ 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",
resClassName,
isPublic,
useActual,
funNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.withIndent
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import java.nio.file.Path
import java.util.*
import java.util.TreeMap
import kotlin.io.path.invariantSeparatorsPathString

internal enum class ResourceType(val typeName: String, val accessorName: String) {
Expand Down Expand Up @@ -129,19 +129,19 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo

internal fun getResFileSpec(
packageName: String,
fileName: String,
className: String,
moduleDir: String,
isPublic: Boolean
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("org.jetbrains.compose.resources.InternalResourceApi::class")
.addMember("org.jetbrains.compose.resources.ExperimentalResourceApi::class")
.build()
)
file.addType(TypeSpec.objectBuilder("Res").also { resObject ->
file.addType(TypeSpec.objectBuilder(className).also { resObject ->
resObject.addModifiers(resModifier)

//readFileBytes
Expand Down Expand Up @@ -209,7 +209,8 @@ internal fun getAccessorsSpecs(
packageName: String,
sourceSetName: String,
moduleDir: String,
isPublic: Boolean
resClassName: String,
isPublic: Boolean,
): List<FileSpec> {
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL
val files = mutableListOf<FileSpec>()
Expand All @@ -226,6 +227,7 @@ internal fun getAccessorsSpecs(
sourceSetName.uppercaseFirstChar() + type.accessorName.uppercaseFirstChar() + index,
packageName,
moduleDir,
resClassName,
resModifier,
idToResources.subMap(ids.first(), true, ids.last(), true)
)
Expand All @@ -242,8 +244,9 @@ private fun getChunkFileSpec(
chunkClassName: String,
packageName: String,
moduleDir: String,
resClassName: String,
resModifier: KModifier,
idToResources: Map<String, List<ResourceItem>>
idToResources: Map<String, List<ResourceItem>>,
): FileSpec {
return FileSpec.builder(packageName, fileName).also { chunkFile ->
chunkFile.addAnnotation(
Expand Down Expand Up @@ -282,7 +285,7 @@ private fun getChunkFileSpec(

idToResources.forEach { (resName, items) ->
val accessor = PropertySpec.builder(resName, type.getClassName(), resModifier)
.receiver(ClassName(packageName, "Res", type.accessorName))
.receiver(ClassName(packageName, resClassName, type.accessorName))
.getter(FunSpec.getterBuilder().addStatement("return $chunkClassName.%N", resName).build())
.build()
chunkFile.addProperty(accessor)
Expand Down Expand Up @@ -321,7 +324,8 @@ private fun getChunkFileSpec(
internal fun getExpectResourceCollectorsFileSpec(
packageName: String,
fileName: String,
isPublic: Boolean
resClassName: String,
isPublic: Boolean,
): FileSpec {
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL
return FileSpec.builder(packageName, fileName).also { file ->
Expand All @@ -336,7 +340,7 @@ internal fun getExpectResourceCollectorsFileSpec(
resModifier
)
.addAnnotation(experimentalAnnotation)
.receiver(ClassName(packageName, "Res"))
.receiver(ClassName(packageName, resClassName))
.build()
)
}
Expand All @@ -346,9 +350,10 @@ 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>>
typeToCollectorFunctions: Map<ResourceType, List<String>>,
): FileSpec = FileSpec.builder(packageName, fileName).also { file ->
val resModifier = if (isPublic) KModifier.PUBLIC else KModifier.INTERNAL

Expand Down Expand Up @@ -384,7 +389,7 @@ internal fun getActualResourceCollectorsFileSpec(
mods
)
.addAnnotation(experimentalAnnotation)
.receiver(ClassName(packageName, "Res"))
.receiver(ClassName(packageName, resClassName))
.delegate(initBlock)
.build()
file.addProperty(property)
Expand All @@ -407,4 +412,4 @@ private fun sortResources(
result[type] = typeResult
}
return result
}
}
Loading

0 comments on commit 4925231

Please sign in to comment.