Browse Source

Introduce top level composeResources dir (#4127)

pull/4144/head
Konstantin 10 months ago committed by GitHub
parent
commit
747aab02f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 0
      components/resources/demo/shared/src/commonMain/composeResources/drawable/compose.png
  2. 0
      components/resources/demo/shared/src/commonMain/composeResources/drawable/droid_icon.xml
  3. 0
      components/resources/demo/shared/src/commonMain/composeResources/drawable/insta_icon.xml
  4. 0
      components/resources/demo/shared/src/commonMain/composeResources/drawable/land.webp
  5. 0
      components/resources/demo/shared/src/commonMain/composeResources/files/icon.xml
  6. 0
      components/resources/demo/shared/src/commonMain/composeResources/font/font_awesome.otf
  7. 0
      components/resources/demo/shared/src/commonMain/composeResources/values/strings.xml
  8. 2
      components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt
  9. 2
      components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FontRes.kt
  10. 2
      components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/ImagesRes.kt
  11. 2
      components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/StringRes.kt
  12. 4
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt
  13. 31
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt
  14. 4
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt
  15. 48
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt
  16. 29
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt
  17. 1
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/settings.gradle.kts
  18. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-au-rUS/vector.xml
  19. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-dark-ge/vector.xml
  20. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-en/vector.xml
  21. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/3-strange-name.xml
  22. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/vector.xml
  23. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/vector_2.xml
  24. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/files/file.txt
  25. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/font/emptyFont.otf
  26. 0
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml

0
components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/compose.png → components/resources/demo/shared/src/commonMain/composeResources/drawable/compose.png

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

0
components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/droid_icon.xml → components/resources/demo/shared/src/commonMain/composeResources/drawable/droid_icon.xml

0
components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/insta_icon.xml → components/resources/demo/shared/src/commonMain/composeResources/drawable/insta_icon.xml

0
components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/land.webp → components/resources/demo/shared/src/commonMain/composeResources/drawable/land.webp

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

0
components/resources/demo/shared/src/commonMain/resources/composeResources/files/icon.xml → components/resources/demo/shared/src/commonMain/composeResources/files/icon.xml

0
components/resources/demo/shared/src/commonMain/resources/composeResources/font/font_awesome.otf → components/resources/demo/shared/src/commonMain/composeResources/font/font_awesome.otf

0
components/resources/demo/shared/src/commonMain/resources/composeResources/values/strings.xml → components/resources/demo/shared/src/commonMain/composeResources/values/strings.xml

2
components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt

@ -19,7 +19,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import components.resources.demo.generated.resources.Res
import components.resources.demo.shared.generated.resources.Res
@Composable
fun FileRes(paddingValues: PaddingValues) {

2
components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FontRes.kt

@ -13,7 +13,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import components.resources.demo.generated.resources.Res
import components.resources.demo.shared.generated.resources.Res
import org.jetbrains.compose.resources.Font
@Composable

2
components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/ImagesRes.kt

@ -11,7 +11,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import components.resources.demo.generated.resources.Res
import components.resources.demo.shared.generated.resources.Res
import org.jetbrains.compose.resources.imageResource
import org.jetbrains.compose.resources.vectorResource
import org.jetbrains.compose.resources.painterResource

2
components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/StringRes.kt

@ -21,7 +21,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import components.resources.demo.generated.resources.Res
import components.resources.demo.shared.generated.resources.Res
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.resources.stringArrayResource
import org.jetbrains.compose.resources.readResourceBytes

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

@ -28,7 +28,7 @@ import org.jetbrains.compose.internal.mppExtOrNull
import org.jetbrains.compose.internal.service.ConfigurationProblemReporterService
import org.jetbrains.compose.internal.service.GradlePropertySnapshotService
import org.jetbrains.compose.internal.utils.currentTarget
import org.jetbrains.compose.resources.configureResourceGenerator
import org.jetbrains.compose.resources.configureComposeResources
import org.jetbrains.compose.resources.ios.configureSyncTask
import org.jetbrains.compose.web.WebExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
@ -65,7 +65,7 @@ abstract class ComposePlugin : Plugin<Project> {
project.plugins.apply(ComposeCompilerKotlinSupportPlugin::class.java)
project.configureNativeCompilerCaching()
project.configureResourceGenerator()
project.configureComposeResources()
project.afterEvaluate {
configureDesktop(project, desktopExtension)

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

@ -2,36 +2,45 @@ package org.jetbrains.compose.resources
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.jetbrains.compose.ComposeExtension
import org.jetbrains.compose.ComposePlugin
import org.jetbrains.compose.ExperimentalComposeLibrary
import org.jetbrains.compose.desktop.application.internal.ComposeProperties
import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import java.io.File
internal const val COMPOSE_RESOURCES_DIR = "composeResources"
private const val RES_GEN_DIR = "generated/compose/resourceGenerator"
internal fun Project.configureResourceGenerator() {
internal fun Project.configureComposeResources() {
pluginManager.withPlugin(KOTLIN_MPP_PLUGIN_ID) {
val kotlinExtension = project.extensions.getByType(KotlinProjectExtension::class.java)
val commonSourceSet = kotlinExtension.sourceSets.findByName(KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME) ?: return@withPlugin
val commonResourcesDir = provider { commonSourceSet.resources.sourceDirectories.first() }
val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
kotlinExtension.sourceSets.all { sourceSet ->
val sourceSetName = sourceSet.name
val composeResourcesPath = project.projectDir.resolve("src/$sourceSetName/$COMPOSE_RESOURCES_DIR")
sourceSet.resources.srcDirs(composeResourcesPath)
if (sourceSetName == KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME) {
configureResourceGenerator(composeResourcesPath, sourceSet)
}
}
}
}
private fun Project.configureResourceGenerator(commonComposeResourcesDir: File, commonSourceSet: KotlinSourceSet) {
val commonComposeResources = provider { commonComposeResourcesDir }
val packageName = provider {
buildString {
val group = project.group.toString().asUnderscoredIdentifier()
append(group)
if (group.isNotEmpty()) append(".")
append("generated.resources")
append(project.name.lowercase())
append(".generated.resources")
}
}
fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) })
val resDir = layout.dir(commonResourcesDir.map { it.resolve(COMPOSE_RESOURCES_DIR) })
val resDir = layout.dir(commonComposeResources)
//lazy check a dependency on the Resources library
val shouldGenerateResourceAccessors: Provider<Boolean> = provider {
@ -70,11 +79,9 @@ internal fun Project.configureResourceGenerator() {
val androidExtension = project.extensions.findByName("android")
if (androidExtension != null) {
configureAndroidResources(
commonResourcesDir,
commonComposeResources,
buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile },
shouldGenerateResourceAccessors
)
}
}
}

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

@ -125,7 +125,7 @@ internal fun getResFileSpec(
.addParameter("path", String::class)
.addModifiers(KModifier.SUSPEND)
.returns(ByteArray::class)
.addStatement("return %M(\"$COMPOSE_RESOURCES_DIR/\" + path)", readResourceBytes) //todo: add module ID here
.addStatement("return %M(path)", readResourceBytes) //todo: add module ID here
.build()
)
@ -163,7 +163,7 @@ private fun TypeSpec.Builder.addResourceProperty(name: String, items: List<Resou
add("%T(\n", resourceItemClass).withIndent {
add("setOf(").addQualifiers(item).add("),\n")
//file separator should be '/' on all platforms
add("\"$COMPOSE_RESOURCES_DIR/${item.path.invariantSeparatorsPathString}\"\n") //todo: add module ID here
add("\"${item.path.invariantSeparatorsPathString}\"\n") //todo: add module ID here
}
add("),\n")
}

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

@ -13,24 +13,24 @@ class ResourcesTest : GradlePluginTestBase() {
//check generated resource's accessors
gradle("generateComposeResClass").checks {
assertEqualTextFiles(
file("build/generated/compose/resourceGenerator/kotlin/app/group/generated/resources/Res.kt"),
file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt"),
file("expected/Res.kt")
)
}
//check resource's accessors were regenerated
file("src/commonMain/resources/composeResources/drawable/vector_2.xml").renameTo(
file("src/commonMain/resources/composeResources/drawable/vector_3.xml")
file("src/commonMain/composeResources/drawable/vector_2.xml").renameTo(
file("src/commonMain/composeResources/drawable/vector_3.xml")
)
gradle("generateComposeResClass").checks {
assertNotEqualTextFiles(
file("build/generated/compose/resourceGenerator/kotlin/app/group/generated/resources/Res.kt"),
file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt"),
file("expected/Res.kt")
)
}
file("src/commonMain/resources/composeResources/drawable-en").renameTo(
file("src/commonMain/resources/composeResources/drawable-ren")
file("src/commonMain/composeResources/drawable-en").renameTo(
file("src/commonMain/composeResources/drawable-ren")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -38,8 +38,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/drawable-ren").renameTo(
file("src/commonMain/resources/composeResources/drawable-rUS-en")
file("src/commonMain/composeResources/drawable-ren").renameTo(
file("src/commonMain/composeResources/drawable-rUS-en")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -47,8 +47,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/drawable-rUS-en").renameTo(
file("src/commonMain/resources/composeResources/drawable-rUS")
file("src/commonMain/composeResources/drawable-rUS-en").renameTo(
file("src/commonMain/composeResources/drawable-rUS")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -56,8 +56,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/drawable-rUS").renameTo(
file("src/commonMain/resources/composeResources/drawable-en-fr")
file("src/commonMain/composeResources/drawable-rUS").renameTo(
file("src/commonMain/composeResources/drawable-en-fr")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -65,8 +65,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/drawable-en-fr").renameTo(
file("src/commonMain/resources/composeResources/image")
file("src/commonMain/composeResources/drawable-en-fr").renameTo(
file("src/commonMain/composeResources/image")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -74,8 +74,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/image").renameTo(
file("src/commonMain/resources/composeResources/files-de")
file("src/commonMain/composeResources/image").renameTo(
file("src/commonMain/composeResources/files-de")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -83,8 +83,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/files-de").renameTo(
file("src/commonMain/resources/composeResources/strings")
file("src/commonMain/composeResources/files-de").renameTo(
file("src/commonMain/composeResources/strings")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -92,8 +92,8 @@ class ResourcesTest : GradlePluginTestBase() {
""".trimIndent())
}
file("src/commonMain/resources/composeResources/strings").renameTo(
file("src/commonMain/resources/composeResources/string-us")
file("src/commonMain/composeResources/strings").renameTo(
file("src/commonMain/composeResources/string-us")
)
gradle("generateComposeResClass").checks {
check.logContains("""
@ -102,11 +102,11 @@ class ResourcesTest : GradlePluginTestBase() {
}
//restore defaults
file("src/commonMain/resources/composeResources/string-us").renameTo(
file("src/commonMain/resources/composeResources/drawable-en")
file("src/commonMain/composeResources/string-us").renameTo(
file("src/commonMain/composeResources/drawable-en")
)
file("src/commonMain/resources/composeResources/drawable/vector_3.xml").renameTo(
file("src/commonMain/resources/composeResources/drawable/vector_2.xml")
file("src/commonMain/composeResources/drawable/vector_3.xml").renameTo(
file("src/commonMain/composeResources/drawable/vector_2.xml")
)
//TODO: check a real build after a release a new version of the resources library

29
gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt

@ -1,4 +1,4 @@
package app.group.generated.resources
package app.group.resources_test.generated.resources
import kotlin.ByteArray
import kotlin.OptIn
@ -22,8 +22,7 @@ internal object Res {
* @return The content of the file as a byte array.
*/
@OptIn(org.jetbrains.compose.resources.InternalResourceApi::class)
public suspend fun readBytes(path: String): ByteArray = readResourceBytes("composeResources/" +
path)
public suspend fun readBytes(path: String): ByteArray = readResourceBytes(path)
public object drawable {
public val _3_strange_name: DrawableResource = DrawableResource(
@ -31,7 +30,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/drawable/3-strange-name.xml"
"drawable/3-strange-name.xml"
),
)
)
@ -41,19 +40,19 @@ internal object Res {
setOf(
ResourceItem(
setOf(LanguageQualifier("au"), RegionQualifier("US"), ),
"composeResources/drawable-au-rUS/vector.xml"
"drawable-au-rUS/vector.xml"
),
ResourceItem(
setOf(ThemeQualifier.DARK, LanguageQualifier("ge"), ),
"composeResources/drawable-dark-ge/vector.xml"
"drawable-dark-ge/vector.xml"
),
ResourceItem(
setOf(LanguageQualifier("en"), ),
"composeResources/drawable-en/vector.xml"
"drawable-en/vector.xml"
),
ResourceItem(
setOf(),
"composeResources/drawable/vector.xml"
"drawable/vector.xml"
),
)
)
@ -63,7 +62,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/drawable/vector_2.xml"
"drawable/vector_2.xml"
),
)
)
@ -75,7 +74,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/font/emptyFont.otf"
"font/emptyFont.otf"
),
)
)
@ -88,7 +87,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/values/strings.xml"
"values/strings.xml"
),
)
)
@ -99,7 +98,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/values/strings.xml"
"values/strings.xml"
),
)
)
@ -110,7 +109,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/values/strings.xml"
"values/strings.xml"
),
)
)
@ -121,7 +120,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/values/strings.xml"
"values/strings.xml"
),
)
)
@ -132,7 +131,7 @@ internal object Res {
setOf(
ResourceItem(
setOf(),
"composeResources/values/strings.xml"
"values/strings.xml"
),
)
)

1
gradle-plugins/compose/src/test/test-projects/misc/commonResources/settings.gradle.kts

@ -1,3 +1,4 @@
rootProject.name = "resources_test"
pluginManagement {
repositories {
mavenLocal()

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable-au-rUS/vector.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-au-rUS/vector.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable-dark-ge/vector.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-dark-ge/vector.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable-en/vector.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable-en/vector.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable/3-strange-name.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/3-strange-name.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable/vector.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/vector.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/drawable/vector_2.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/drawable/vector_2.xml

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/files/file.txt → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/files/file.txt

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/font/emptyFont.otf → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/font/emptyFont.otf

0
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/resources/composeResources/values/strings.xml → gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/composeResources/values/strings.xml

Loading…
Cancel
Save