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

Support compose resources for ios tests #4185

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 @@ -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
Expand All @@ -21,15 +22,16 @@ 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(
"dev.icerock.mobile.multiplatform-resources",
"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")
Expand Down Expand Up @@ -60,20 +62,29 @@ private class SyncIosResourcesContext(
fun syncDirFor(framework: Framework): Provider<Directory> {
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)) }
}
}
}

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 ->
Expand Down Expand Up @@ -166,6 +177,18 @@ private fun SyncIosResourcesContext.configureSyncResourcesTasks() {
}
}
}
configureEachIosTestExecutable { bin ->
val copyTestResourcesTask = "copyTestComposeResourcesFor${bin.target.targetName.uppercaseFirstChar()}"
val task = project.tasks.registerOrConfigure<Copy>(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
Expand All @@ -186,11 +209,11 @@ private fun extractPrefixFromBinaryName(name: String, buildType: NativeBuildType
name.substringBeforeLast(suffix.uppercaseFirstChar())
}

private fun iosTargetResourcesProvider(framework: Framework): Provider<IosTargetResources> {
val kotlinTarget = framework.target
val project = framework.project
private fun iosTargetResourcesProvider(bin: NativeBinary): Provider<IosTargetResources> {
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 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugins/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading