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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

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.

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
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?

@@ -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.

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