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

Resources improvements for native macOS app #5169

Merged
merged 5 commits into from
Dec 9, 2024

Conversation

Thomas-Vos
Copy link
Contributor

@Thomas-Vos Thomas-Vos commented Nov 27, 2024

First commit fixes compose resources for native macOS app (see currently broken chat app using runDebugExecutableMacosArm64).

Second commit adds support for embedding resources into native macOS framework. Similar to how it works on iOS. This allows using the macOS compose code in an Xcode project. Just like iOS, using kotlin code as framwork. Then call the main function from the Xcode project to run the compose app.

Testing

First commit: run chat app using runDebugExecutableMacosArm64
Second commit: Tested by adding macOS support to the iOS Xcode project in chat app (not in this PR). Can add the sample app if needed.

@MatkovIvan MatkovIvan requested a review from terrakok November 28, 2024 09:23
Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for the new logic are required

Comment on lines 130 to 131
val pathSuffix = if (target.isMacTarget()) "/Resources" else ""
File("$builtProductsDir/$contentsFolderPath$pathSuffix/$IOS_COMPOSE_RESOURCES_ROOT_DIR").canonicalPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea to hardcode a mac specific path here.
It is better to make new function for mac targets and support cocoapods as well then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the path by changing CONTENTS_FOLDER_PATH to UNLOCALIZED_RESOURCES_FOLDER_PATH. Now it is the same for iOS and macOS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a test for cocoapods (copied from iOS with minor changes), so I think all should work fine.

@Thomas-Vos Thomas-Vos force-pushed the resources-improvements-macos branch 2 times, most recently from 4111be3 to d5c4dc5 Compare November 29, 2024 17:06
@Thomas-Vos Thomas-Vos force-pushed the resources-improvements-macos branch from d5c4dc5 to 8f07e0e Compare November 29, 2024 18:15
@Thomas-Vos
Copy link
Contributor Author

@terrakok thanks a lot for your review! Thanks for helping me with reviewing some fixes for macOS issues so I can start using it in my app.

I added tests to the PR as you asked. However, the tests are failing on CI because they use an outdated snapshot of the resources library. For the tests to pass, they must use the latest version of the resources library. This is because there are changes in the resources library in order to support macOS. When forcing a local build of the library, it passes on my own mac:

Screenshot 2024-11-30 at 18 35 26

Is there any way to fix this? Or is this expected to happen until the next compose snapshot is released with the changes of this PR?

@Thomas-Vos Thomas-Vos requested a review from terrakok November 30, 2024 19:39
@@ -39,12 +39,15 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou
private fun getPathOnDisk(path: String): String {
val fm = NSFileManager.defaultManager()
val currentDirectoryPath = fm.currentDirectoryPath
val pathFix = path.removePrefix("composeResources/").substringAfter("/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this line too dangerous. It mixes removePrefix and substringAfter that is hard to predict a final result:
"composeResources/foo/bar/baz" -> "bar/baz"
"/foo/bar/baz" -> "foo/bar/baz"
"/composeResources/foo/bar/baz" -> "composeResources/foo/bar/baz"

Could you please refactor it to use Path API and add a comment here what and why was fixed, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrakok Thanks, I understand this is dangerous. However, as far as I know there is no path API that can be used for this on macOS. I did however move this into a separate function with some comments. Also improved the code a bit so now it checks that no slash prefix is present and it only uses substringAfter. What do you think about this?

Remember that this code only fixes existing implementation when running macOS app using executable binary, and is temporary until resources are actually bundled in the executable binary. I guess this was added in the past for easy debugging. This code is not required for the framework binary.

@Thomas-Vos Thomas-Vos requested a review from terrakok December 2, 2024 16:31
@terrakok
Copy link
Member

terrakok commented Dec 4, 2024

We will merge the changes a bit later (at the moment we stabilize for 1.8.0).

@terrakok terrakok merged commit 5870305 into JetBrains:master Dec 9, 2024
9 of 18 checks passed
terrakok added a commit that referenced this pull request Dec 10, 2024
@terrakok
Copy link
Member

terrakok commented Dec 10, 2024

I reverted the commit because there is a problem with Skiko publication https://youtrack.jetbrains.com/issue/CMP-3859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants