Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Res class if there is no common composeResource dir #4176

Merged
merged 1 commit into from
Jan 26, 2024
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 @@ -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")
}
Loading