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

Fix resources with cocoapods integration #4783

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -81,19 +81,24 @@ internal fun Project.configureSyncIosComposeResources(
plugins.withId(COCOAPODS_PLUGIN_ID) {
(kotlinExtension as ExtensionAware).extensions.getByType(CocoapodsExtension::class.java).apply {
framework { podFramework ->
val syncDir = podFramework.getFinalResourcesDir().get().asFile.relativeTo(projectDir)
val specAttr = "['${syncDir.path}']"
extraSpecAttributes["resources"] = specAttr
project.tasks.named("podInstall").configure {
it.doFirst {
if (extraSpecAttributes["resources"] != specAttr) {
error("""
// DIRTY HACK: we don't have an API to configure PodspecTask.
// We need to use the cocoapods DSL only but to provide a correct resources path we need lazy read a framework baseName.
// Which may be configured be the same DSL
podFramework.project.afterEvaluate {
terrakok marked this conversation as resolved.
Show resolved Hide resolved
val syncDir = podFramework.getFinalResourcesDir().get().asFile.relativeTo(projectDir)
val specAttr = "['${syncDir.path}']"
extraSpecAttributes["resources"] = specAttr
project.tasks.named("podInstall").configure {
it.doFirst {
if (extraSpecAttributes["resources"] != specAttr) {
error("""
|Kotlin.cocoapods.extraSpecAttributes["resources"] is not compatible with Compose Multiplatform's resources management for iOS.
| * Recommended action: remove extraSpecAttributes["resources"] from '${project.buildFile}' and run '${project.path}:podInstall' once;
| * Alternative action: turn off Compose Multiplatform's resources management for iOS by adding '${ComposeProperties.SYNC_RESOURCES_PROPERTY}=false' to your gradle.properties;
""".trimMargin())
}
syncDir.mkdirs()
}
syncDir.mkdirs()
}
}
}
Expand All @@ -113,7 +118,10 @@ private fun Framework.isCocoapodsFramework() = name.startsWith("pod")
private fun Framework.getFinalResourcesDir(): Provider<Directory> {
val providers = project.providers
return if (isCocoapodsFramework()) {
project.layout.buildDirectory.dir("compose/ios/$baseName/$IOS_COMPOSE_RESOURCES_ROOT_DIR/")
val frameworkBaseNameProvider = project.provider { baseName }
frameworkBaseNameProvider.flatMap { name ->
project.layout.buildDirectory.dir("compose/ios/$name/$IOS_COMPOSE_RESOURCES_ROOT_DIR/")
}
} else {
providers.environmentVariable("BUILT_PRODUCTS_DIR")
.zip(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,13 @@ class ResourcesTest : GradlePluginTestBase() {
file("build/compose/ios/shared/compose-resources/drawable/compose-multiplatform.xml").checkExists()
file("build/compose/ios/shared/compose-resources/drawable/icon.xml").checkExists()
}

gradle(":podspec", "-Pkotlin.native.cocoapods.generate.wrapper=true").checks {
assertEqualTextFiles(
file("iosResources.podspec"),
file("expected/iosResources.podspec")
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Pod::Spec.new do |spec|
spec.name = 'iosResources'
spec.version = '1.0'
spec.homepage = 'Link to a Kotlin/Native module homepage'
spec.source = { :http=> ''}
spec.authors = ''
spec.license = ''
spec.summary = 'Some description for a Kotlin/Native module'
spec.vendored_frameworks = 'build/cocoapods/framework/shared.framework'
spec.libraries = 'c++'

spec.dependency 'Base64', '1.1.2'

if !Dir.exist?('build/cocoapods/framework/shared.framework') || Dir.empty?('build/cocoapods/framework/shared.framework')
raise "

Kotlin framework 'shared' doesn't exist yet, so a proper Xcode project can't be generated.
'pod install' should be executed after running ':generateDummyFramework' Gradle task:

./gradlew :generateDummyFramework

Alternatively, proper pod installation is performed during Gradle sync in the IDE (if Podfile location is set)"
end

spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => '',
'PRODUCT_MODULE_NAME' => 'shared',
}

spec.script_phases = [
{
:name => 'Build iosResources',
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED" ]; then
echo "Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
SCRIPT
}
]
spec.resources = ['build/compose/ios/shared/compose-resources']
end
Loading