Browse Source

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

pull/4185/head
Konstantin 10 months ago committed by GitHub
parent
commit
fbab715c20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  2. 4
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt
  3. 10
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt
  4. 20
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/build.gradle.kts
  5. 21
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt
  6. 1
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/gradle.properties
  7. 22
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/settings.gradle.kts
  8. 9
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/src/commonMain/kotlin/App.kt

10
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt

@ -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

4
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt

@ -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()) {
@ -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() }
}

10
gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

@ -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")
)
}
}
}

20
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/build.gradle.kts

@ -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)
}
}
}
}

21
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt

@ -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)
}

1
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/gradle.properties

@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx8096M

22
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/settings.gradle.kts

@ -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()
}
}

9
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/src/commonMain/kotlin/App.kt

@ -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…
Cancel
Save