From 9caef45f175460f9e4110506fb194d4652f1e1f8 Mon Sep 17 00:00:00 2001 From: Konstantin Tskhovrebov Date: Fri, 26 Jan 2024 14:43:43 +0100 Subject: [PATCH] Support compose resources for ios tests --- .../ios/configureSyncIosResources.kt | 39 +++++++++++++++---- .../tests/integration/GradlePluginTest.kt | 19 +++++++++ gradle-plugins/gradle.properties | 2 +- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ios/configureSyncIosResources.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ios/configureSyncIosResources.kt index 21e47e610a4..0b838cdebec 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ios/configureSyncIosResources.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ios/configureSyncIosResources.kt @@ -8,6 +8,7 @@ package org.jetbrains.compose.resources.ios import org.gradle.api.Project import org.gradle.api.file.Directory import org.gradle.api.provider.Provider +import org.gradle.api.tasks.Copy import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskContainer import org.gradle.api.tasks.TaskAction @@ -21,8 +22,7 @@ import org.jetbrains.compose.internal.utils.new import org.jetbrains.compose.internal.utils.registerOrConfigure import org.jetbrains.compose.internal.utils.uppercaseFirstChar import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.plugin.mpp.Framework -import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType +import org.jetbrains.kotlin.gradle.plugin.mpp.* import java.io.File private val incompatiblePlugins = listOf( @@ -30,6 +30,8 @@ private val incompatiblePlugins = listOf( "io.github.skeptick.libres", ) +private const val IOS_COMPOSE_RESOURCES_ROOT_DIR = "compose-resources" + internal fun Project.configureSyncTask(mppExt: KotlinMultiplatformExtension) { fun reportSyncIsDisabled(reason: String) { logger.info("Compose Multiplatform resource management for iOS is disabled: $reason") @@ -60,13 +62,13 @@ private class SyncIosResourcesContext( fun syncDirFor(framework: Framework): Provider { val providers = framework.project.providers return if (framework.isCocoapodsFramework) { - project.layout.buildDirectory.dir("compose/ios/${framework.baseName}/compose-resources/") + project.layout.buildDirectory.dir("compose/ios/${framework.baseName}/$IOS_COMPOSE_RESOURCES_ROOT_DIR/") } else { providers.environmentVariable("BUILT_PRODUCTS_DIR") .zip(providers.environmentVariable("CONTENTS_FOLDER_PATH")) { builtProductsDir, contentsFolderPath -> File(builtProductsDir) .resolve(contentsFolderPath) - .resolve("compose-resources") + .resolve(IOS_COMPOSE_RESOURCES_ROOT_DIR) .canonicalPath }.flatMap { framework.project.objects.directoryProperty().apply { set(File(it)) } @@ -74,6 +76,15 @@ private class SyncIosResourcesContext( } } + fun configureEachIosTestExecutable(fn: (TestExecutable) -> Unit) { + mppExt.targets.all { target -> + target.asIosNativeTargetOrNull()?.let { iosTarget -> + iosTarget.binaries.withType(TestExecutable::class.java).configureEach { bin -> + fn(bin) + } + } + } + } fun configureEachIosFramework(fn: (Framework) -> Unit) { mppExt.targets.all { target -> @@ -166,6 +177,18 @@ private fun SyncIosResourcesContext.configureSyncResourcesTasks() { } } } + configureEachIosTestExecutable { bin -> + val copyTestResourcesTask = "copyTestComposeResourcesFor${bin.target.targetName.uppercaseFirstChar()}" + val task = project.tasks.registerOrConfigure(copyTestResourcesTask) { + from({ + (bin.compilation.associateWith + bin.compilation).flatMap { compilation -> + compilation.allKotlinSourceSets.map { it.resources } + } + }) + into(bin.outputDirectory.resolve(IOS_COMPOSE_RESOURCES_ROOT_DIR)) + } + bin.linkTask.dependsOn(task) + } } private val Framework.isCocoapodsFramework: Boolean @@ -186,11 +209,11 @@ private fun extractPrefixFromBinaryName(name: String, buildType: NativeBuildType name.substringBeforeLast(suffix.uppercaseFirstChar()) } -private fun iosTargetResourcesProvider(framework: Framework): Provider { - val kotlinTarget = framework.target - val project = framework.project +private fun iosTargetResourcesProvider(bin: NativeBinary): Provider { + val kotlinTarget = bin.target + val project = bin.project return project.provider { - val resourceDirs = framework.compilation.allKotlinSourceSets + val resourceDirs = bin.compilation.allKotlinSourceSets .flatMap { sourceSet -> sourceSet.resources.srcDirs.map { it.canonicalPath } } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/GradlePluginTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/GradlePluginTest.kt index 608a2058e4d..711590195d6 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/GradlePluginTest.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/GradlePluginTest.kt @@ -93,6 +93,25 @@ class GradlePluginTest : GradlePluginTestBase() { } } + @Test + fun iosTestResources() { + Assumptions.assumeTrue(currentOS == OS.MacOS) + val testEnv = defaultTestEnvironment.copy( + useGradleConfigurationCache = TestProperties.gradleBaseVersionForTests.isAtLeastGradle8 + ) + + with(testProject(TestProjects.iosResources, testEnv)) { + gradle(":linkDebugTestIosX64", "--dry-run").checks { + check.taskSkipped(":copyTestComposeResourcesForIosX64") + check.taskSkipped(":linkDebugTestIosX64") + } + gradle(":copyTestComposeResourcesForIosX64").checks { + check.taskSuccessful(":copyTestComposeResourcesForIosX64") + file("build/bin/iosX64/debugTest/compose-resources/compose-multiplatform.xml").checkExists() + } + } + } + @Test fun iosMokoResources() { Assumptions.assumeTrue(currentOS == OS.MacOS) diff --git a/gradle-plugins/gradle.properties b/gradle-plugins/gradle.properties index a13f110c10d..eb6dcae08fd 100644 --- a/gradle-plugins/gradle.properties +++ b/gradle-plugins/gradle.properties @@ -2,7 +2,7 @@ org.gradle.parallel=true kotlin.code.style=official # Default version of Compose Libraries used by Gradle plugin -compose.version=1.6.0-dev1357 +compose.version=1.6.0-dev1383 # The latest version of Compose Compiler used by Gradle plugin. Used only in tests/CI. compose.tests.compiler.version=1.5.8-beta01 # The latest version of Kotlin compatible with compose.tests.compiler.version. Used only in tests/CI.