Skip to content

Commit

Permalink
Generate Res class if there is no common composeResource dir (#4176)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok authored Jan 26, 2024
1 parent f121805 commit fbab715
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ abstract class GenerateResClassTask : DefaultTask() {
@get:Input
abstract val packageName: Property<String>

@get:InputDirectory
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val resDir: DirectoryProperty
abstract val resDir: Property<File>

@get:OutputDirectory
abstract val codeDir: DirectoryProperty

init {
this.onlyIf { resDir.asFile.get().exists() }
}

@TaskAction
fun generate() {
try {
val rootResDir = resDir.get().asFile
val rootResDir = resDir.get()
logger.info("Generate resources for $rootResDir")

//get first level dirs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,

fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) })

val resDir = layout.dir(commonComposeResources)

//lazy check a dependency on the Resources library
val shouldGenerateResourceAccessors: Provider<Boolean> = provider {
if (ComposeProperties.alwaysGenerateResourceAccessors(providers).get()) {
Expand All @@ -65,7 +63,7 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,
GenerateResClassTask::class.java
) {
it.packageName.set(packageName)
it.resDir.set(resDir)
it.resDir.set(commonComposeResources)
it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin"))
it.onlyIf { shouldGenerateResourceAccessors.get() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ class ResourcesTest : GradlePluginTestBase() {
check.taskSuccessful(":copyFontsToAndroidAssets")
}
}

@Test
fun testEmptyResClass(): Unit = with(testProject("misc/emptyResources")) {
gradle("generateComposeResClass").checks {
assertEqualTextFiles(
file("build/generated/compose/resourceGenerator/kotlin/app/group/empty_res/generated/resources/Res.kt"),
file("expected/Res.kt")
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}

group = "app.group"

kotlin {
jvm("desktop")

sourceSets {
commonMain {
dependencies {
implementation(compose.runtime)
implementation(compose.material)
implementation(compose.components.resources)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app.group.empty_res.generated.resources

import kotlin.ByteArray
import kotlin.OptIn
import kotlin.String
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.readResourceBytes

@OptIn(org.jetbrains.compose.resources.InternalResourceApi::class)
@ExperimentalResourceApi
internal object Res {
/**
* 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")`
*
* @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.
*/
public suspend fun readBytes(path: String): ByteArray = readResourceBytes(path)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx8096M
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
rootProject.name = "empty_res"
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
google()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
plugins {
id("org.jetbrains.kotlin.multiplatform").version("KOTLIN_VERSION_PLACEHOLDER")
id("org.jetbrains.compose").version("COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER")
}
}
dependencyResolutionManagement {
repositories {
mavenLocal()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
mavenCentral()
gradlePluginPortal()
google()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import app.group.empty_res.generated.resources.Res

@Composable
fun App() {
val res = Res
Text("text")
}

0 comments on commit fbab715

Please sign in to comment.