diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/compose.png b/components/resources/demo/shared/src/commonMain/composeResources/drawable/compose.png similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/compose.png rename to components/resources/demo/shared/src/commonMain/composeResources/drawable/compose.png diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/droid_icon.xml b/components/resources/demo/shared/src/commonMain/composeResources/drawable/droid_icon.xml similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/droid_icon.xml rename to components/resources/demo/shared/src/commonMain/composeResources/drawable/droid_icon.xml diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/insta_icon.xml b/components/resources/demo/shared/src/commonMain/composeResources/drawable/insta_icon.xml similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/insta_icon.xml rename to components/resources/demo/shared/src/commonMain/composeResources/drawable/insta_icon.xml diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/land.webp b/components/resources/demo/shared/src/commonMain/composeResources/drawable/land.webp similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/drawable/land.webp rename to components/resources/demo/shared/src/commonMain/composeResources/drawable/land.webp diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/files/icon.xml b/components/resources/demo/shared/src/commonMain/composeResources/files/icon.xml similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/files/icon.xml rename to components/resources/demo/shared/src/commonMain/composeResources/files/icon.xml diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/font/font_awesome.otf b/components/resources/demo/shared/src/commonMain/composeResources/font/font_awesome.otf similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/font/font_awesome.otf rename to components/resources/demo/shared/src/commonMain/composeResources/font/font_awesome.otf diff --git a/components/resources/demo/shared/src/commonMain/resources/composeResources/values/strings.xml b/components/resources/demo/shared/src/commonMain/composeResources/values/strings.xml similarity index 100% rename from components/resources/demo/shared/src/commonMain/resources/composeResources/values/strings.xml rename to components/resources/demo/shared/src/commonMain/composeResources/values/strings.xml diff --git a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt b/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt index 86675ce6a5..57f1c65b59 100644 --- a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt +++ b/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) { diff --git a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FontRes.kt b/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FontRes.kt index 93deb00e23..593bdbc0ae 100644 --- a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FontRes.kt +++ b/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 diff --git a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/ImagesRes.kt b/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/ImagesRes.kt index 0ef3d25891..3ba6cd0211 100644 --- a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/ImagesRes.kt +++ b/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 diff --git a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/StringRes.kt b/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/StringRes.kt index 4bb1111a1a..7de1a70d86 100644 --- a/components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/StringRes.kt +++ b/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 diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt index d9e9b39b7a..f4fa6199a6 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt +++ b/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.plugins.apply(ComposeCompilerKotlinSupportPlugin::class.java) project.configureNativeCompilerCaching() - project.configureResourceGenerator() + project.configureComposeResources() project.afterEvaluate { configureDesktop(project, desktopExtension) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt index 5323355095..3244537b07 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt @@ -2,79 +2,86 @@ 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 packageName = provider { - buildString { - val group = project.group.toString().asUnderscoredIdentifier() - append(group) - if (group.isNotEmpty()) append(".") - append("generated.resources") + 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) } } + } +} - fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) }) +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(project.name.lowercase()) + append(".generated.resources") + } + } - val resDir = layout.dir(commonResourcesDir.map { it.resolve(COMPOSE_RESOURCES_DIR) }) + fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) }) - //lazy check a dependency on the Resources library - val shouldGenerateResourceAccessors: Provider = provider { - if (ComposeProperties.alwaysGenerateResourceAccessors(providers).get()) { - true - } else { - configurations - .getByName(commonSourceSet.implementationConfigurationName) - .allDependencies.any { dep -> - val depStringNotation = dep.let { "${it.group}:${it.name}:${it.version}" } - depStringNotation == ComposePlugin.CommonComponentsDependencies.resources - } - } - } + val resDir = layout.dir(commonComposeResources) - val genTask = tasks.register( - "generateComposeResClass", - GenerateResClassTask::class.java - ) { - it.packageName.set(packageName) - it.resDir.set(resDir) - it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin")) - it.onlyIf { shouldGenerateResourceAccessors.get() } + //lazy check a dependency on the Resources library + val shouldGenerateResourceAccessors: Provider = provider { + if (ComposeProperties.alwaysGenerateResourceAccessors(providers).get()) { + true + } else { + configurations + .getByName(commonSourceSet.implementationConfigurationName) + .allDependencies.any { dep -> + val depStringNotation = dep.let { "${it.group}:${it.name}:${it.version}" } + depStringNotation == ComposePlugin.CommonComponentsDependencies.resources + } } + } - //register generated source set - commonSourceSet.kotlin.srcDir(genTask.map { it.codeDir }) + val genTask = tasks.register( + "generateComposeResClass", + GenerateResClassTask::class.java + ) { + it.packageName.set(packageName) + it.resDir.set(resDir) + it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin")) + it.onlyIf { shouldGenerateResourceAccessors.get() } + } - //setup task execution during IDE import - tasks.configureEach { - if (it.name == "prepareKotlinIdeaImport") { - it.dependsOn(genTask) - } - } + //register generated source set + commonSourceSet.kotlin.srcDir(genTask.map { it.codeDir }) - val androidExtension = project.extensions.findByName("android") - if (androidExtension != null) { - configureAndroidResources( - commonResourcesDir, - buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, - shouldGenerateResourceAccessors - ) + //setup task execution during IDE import + tasks.configureEach { + if (it.name == "prepareKotlinIdeaImport") { + it.dependsOn(genTask) } } -} + val androidExtension = project.extensions.findByName("android") + if (androidExtension != null) { + configureAndroidResources( + commonComposeResources, + buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, + shouldGenerateResourceAccessors + ) + } +} diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt index 8d3768ae39..c3fc0bf61f 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt +++ b/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