Skip to content

Commit a82ee3e

Browse files
authored
Update ios resource copying to an iOS app bundle to support SwiftExport libraries (#5480)
Fixes https://youtrack.jetbrains.com/issue/CMP-8873 ## Testing Tested manually. Swift export is experimental ## Release Notes ### Fixes - Resources - Update iOS resource copying to an iOS app bundle to support Swift Export libraries
1 parent 9286ead commit a82ee3e

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private fun Project.configureResClassGeneration(
102102
packagingDir: Provider<File>,
103103
generateModulePath: Boolean
104104
) {
105-
logger.info("Configure '$resClassName' class generation for ${resClassSourceSet.name}")
105+
logger.info("Configure Res object generation for ${resClassSourceSet.name}")
106106

107107
val genTask = tasks.register(
108108
"generateComposeResClass",

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

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import org.jetbrains.compose.internal.utils.joinLowerCamelCase
1111
import org.jetbrains.compose.internal.utils.registerOrConfigure
1212
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
1313
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
14+
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
1415
import org.jetbrains.kotlin.gradle.plugin.cocoapods.CocoapodsExtension
1516
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
1617
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
18+
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBinary
19+
import org.jetbrains.kotlin.gradle.plugin.mpp.StaticLibrary
1720
import org.jetbrains.kotlin.gradle.plugin.mpp.TestExecutable
1821
import org.jetbrains.kotlin.konan.target.KonanTarget
1922
import java.io.File
@@ -34,38 +37,48 @@ internal fun Project.configureSyncIosComposeResources(
3437

3538
kotlinExtension.targets.withType(KotlinNativeTarget::class.java).all { nativeTarget ->
3639
if (nativeTarget.isIosOrMacTarget()) {
37-
nativeTarget.binaries.withType(Framework::class.java).all { iosFramework ->
38-
val frameworkClassifier = iosFramework.getClassifier()
39-
val checkNoSandboxTask = tasks.registerOrConfigure<CheckCanAccessComposeResourcesDirectory>(
40-
"checkCanSync${frameworkClassifier}ComposeResourcesForIos"
41-
) {}
42-
43-
val frameworkResources = files()
44-
iosFramework.compilation.allKotlinSourceSets.forAll { ss ->
45-
frameworkResources.from(ss.resources.sourceDirectories)
40+
nativeTarget.binaries.withType(NativeBinary::class.java)
41+
.matching { binary ->
42+
binary is Framework || binary.isSwiftExportStaticLibrary()
4643
}
47-
val syncComposeResourcesTask = tasks.registerOrConfigure<SyncComposeResourcesForIosTask>(
48-
iosFramework.getSyncResourcesTaskName()
49-
) {
50-
dependsOn(checkNoSandboxTask)
51-
dependsOn(frameworkResources) //!!! explicit dependency because targetResources is not an input
44+
.all { binary ->
45+
val binClassifier = binary.getClassifier()
46+
val checkNoSandboxTask = tasks.registerOrConfigure<CheckCanAccessComposeResourcesDirectory>(
47+
"checkCanSync${binClassifier}ComposeResourcesForIos"
48+
) {}
49+
val frameworkResources = files()
50+
binary.compilation.allKotlinSourceSets.forAll { ss ->
51+
frameworkResources.from(ss.resources.sourceDirectories)
52+
}
53+
val syncComposeResourcesTask = tasks.registerOrConfigure<SyncComposeResourcesForIosTask>(
54+
binary.getSyncResourcesTaskName()
55+
) {
56+
dependsOn(checkNoSandboxTask)
57+
dependsOn(frameworkResources) //!!! explicit dependency because targetResources is not an input
58+
59+
outputDir.set(binary.getFinalResourcesDir())
60+
targetResources.put(binary.target.konanTarget.name, frameworkResources)
61+
}
5262

53-
outputDir.set(iosFramework.getFinalResourcesDir())
54-
targetResources.put(iosFramework.target.konanTarget.name, frameworkResources)
55-
}
63+
val externalTaskName = when {
64+
binary.isCocoapodsFramework() -> "syncFramework"
65+
binary.isSwiftExportStaticLibrary() -> "embedSwiftExportForXcode"
66+
else -> "embedAndSign${binClassifier}AppleFrameworkForXcode"
67+
}
5668

57-
val externalTaskName = if (iosFramework.isCocoapodsFramework()) {
58-
"syncFramework"
59-
} else {
60-
"embedAndSign${frameworkClassifier}AppleFrameworkForXcode"
61-
}
69+
project.tasks.configureEach { task ->
70+
if (task.name == externalTaskName) {
71+
task.dependsOn(syncComposeResourcesTask)
72+
}
6273

63-
project.tasks.configureEach { task ->
64-
if (task.name == externalTaskName) {
65-
task.dependsOn(syncComposeResourcesTask)
74+
//FIXME: https://youtrack.jetbrains.com/issue/KT-82332
75+
if (binary.isSwiftExportStaticLibrary() &&
76+
task.name.let { it.startsWith("copy") && it.endsWith("SPMIntermediates") }
77+
) {
78+
task.dependsOn(syncComposeResourcesTask)
79+
}
6680
}
6781
}
68-
}
6982

7083
nativeTarget.binaries.withType(TestExecutable::class.java).all { testExec ->
7184
val copyTestResourcesTask = tasks.registerOrConfigure<Copy>(
@@ -109,17 +122,18 @@ internal fun Project.configureSyncIosComposeResources(
109122
}
110123
}
111124

112-
private fun Framework.getClassifier(): String {
125+
private fun NativeBinary.getClassifier(): String {
113126
val suffix = joinLowerCamelCase(buildType.getName(), outputKind.taskNameClassifier)
114127
return if (name == suffix) ""
115128
else name.substringBeforeLast(suffix.uppercaseFirstChar()).uppercaseFirstChar()
116129
}
117130

118-
internal fun Framework.getSyncResourcesTaskName() = "sync${getClassifier()}ComposeResourcesForIos"
131+
private fun NativeBinary.getSyncResourcesTaskName() = "sync${getClassifier()}ComposeResourcesForIos"
119132

120-
private fun Framework.isCocoapodsFramework() = name.startsWith("pod")
133+
private fun NativeBinary.isCocoapodsFramework() = this is Framework && name.startsWith("pod")
134+
private fun NativeBinary.isSwiftExportStaticLibrary() = this is StaticLibrary && baseName == "SwiftExportBinary"
121135

122-
private fun Framework.getFinalResourcesDir(): Provider<Directory> {
136+
private fun NativeBinary.getFinalResourcesDir(): Provider<Directory> {
123137
val providers = project.providers
124138
return if (isCocoapodsFramework()) {
125139
project.layout.buildDirectory.dir("compose/cocoapods/$IOS_COMPOSE_RESOURCES_ROOT_DIR/")

0 commit comments

Comments
 (0)