-
Notifications
You must be signed in to change notification settings - Fork 990
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
feat(spm): Support plugins as Swift packages #1515
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1515 +/- ##
==========================================
+ Coverage 81.41% 82.03% +0.61%
==========================================
Files 16 17 +1
Lines 1862 1926 +64
==========================================
+ Hits 1516 1580 +64
Misses 346 346 ☔ View full report in Codecov by Sentry. |
I would think it's not acceptable to point it to master branch as that would effectively be using the dev build. At minimum it would need to be using the
I think this is more of a side issue because our plugins are not proper modules. They are just loose source code that eventually makes their way part of the App project. And I assume SPM packages are importing these packages as their own build targets, which means they have their own build configurations (and thus needs a reference to the CordovaLib framework). This in itself shouldn't be a blocker though, as long as
Extending on this, a year or two ago I was experimenting with an alternate approach in authoring plugins, so that plugins can have their own native projects and can compile (and potentially run) independently of a Cordova project. Would be very useful to have for using native unit test features. This involved having plugins be in their own projects and having their own build targets, so each plugin would produce their own Just some thoughts. I've been experimenting with SPM with my own framework, and will be sharing what worked for me once I have something but it is a very different architecture than Cordova. |
This actually works if CordovaLib is a static library for the app target, but not when it's consumed as a Swift package itself (which is how we're using it in 8.0.0)
This is correct, when a plugin is a Swift package, it becomes its own framework target that is linked to the app project, rather than copying plugin source files into the project directly. |
I would have thought that this would create duplicate symbols in the app binary. If we have 3 targets:
CordovaLib itself is standalone. This configuration would work, but only if you have no plugins, as a plugin that is linking with a static CordovaLib would cause duplicate symbols assuming that the app is also linking against the same static library. Doesn't this happen? |
In the case where it worked with the static CordovaLib, the plugin did not specify a dependency on CordovaLib as part of its Package.swift file When using Package.swift, even if the app asks to consume CordovaLib at the top level as a static library, plugins cannot find Cordova headers unless they also include a dependency on CordovaLib |
Co-Authored-By: jcesarmobile <[email protected]>
328dd50
to
b92ceaa
Compare
This helps keep the top level project directory a bit cleaner by hiding our CordovaPlugins SPM accumulator package inside the packages directory.
Okay, I've updated this into something that I hope might be workable. There are essentially 2 main changes:
This seems to work in my (fairly basic) testing with the cordova-plugin-device branch linked in the PR description. TODO
|
I want to add a note that this might be partially correct. There might be a case where the If the host machine preparing the platforms is either Windows or Linux and the plugin contains pods, the process might not install and prepare the plugin correctly. I believe during Additionally, on a successful If the host machine is macOS then this should not be a problem. This would need testing for confirmation. If my assumptions are correct, we should document this behavior so it would be made aware. |
This is great work. On the topic of plugin consumption, considering the "tale of two audiences:" I would imagine that native devs with "platform centered approach" projects would prefer to include the plugins as swift packages directly. This begs the question of how the other bits in that native project (e.g., the post-processed config.xml) would get updated, other than manually. |
The complication here (if managing plugins manually in a project's Package.swift) becomes making sure that all those plugins refer to the same version of cordova-ios for their CordovaLib dependency. That's why we're needing to rewrite the Package.swift when we install them in this PR. SwiftPM doesn't have particularly good support for overriding library versions from sub-dependencies or a concept like npm's peerDependencies 😞 |
Platforms affected
iOS
Motivation and Context
Ref GH-1089.
Swift package manager is the built-in way to handle 3rd party dependencies for Xcode and Swift projects, and Cocoapods is no longer in active development. We want to provide a way for plugins to express and manage their dependencies as Swift packages.
The best way to do that seems to be for the plugins themselves to (optionally) become Swift packages.
Description
When a plugin declares in its plugin.xml file that it supports
<platform name="ios" package="swift">
and contains a Package.swift file in the plugin root, we will treat it as a Swift package and add it as a dependency of the app project (technically a dependency of a stub dependency, because that's easier than monkeying further with Xcodeproj files).Why This is Complicated
Each Swift package is treated as its own self-contained library, with access only to the dependencies that it declares. This means that plugins cannot access CordovaLib headers unless the declare a dependency on CordovaLib in their own Package.swift file. That would end up tying them to a very specific version of Cordova and potentially resulting in conflicts due to multiple plugins requesting different versions that don't match the app's version.
It feels like the only workaround is for the plugin install process to edit the Package.swift of each plugin and adjust the CordovaLib dependency to point to the local app one, but then we need to be able to parse and modify arbitrary Package.swift files.
Even with the plan to rewrite, it's not clear (for our own Apache plugins) if we can point them at the master branch of cordova-ios and still pass a release vote, because they would be pointing to a dependency that has not had a release vote. This could result in needing to churn the version dependency in every plugin every time we release a new cordova-ios version (even if we don't need to publish those changes).
Approach Taken
Rather than editing Package.swift in the top-level plugins folder (which we hope one day to get rid of), we've added a packages directory to the platforms/ios folder and will copy plugins there if they are using Swift Package Manager. The added advantage here is that we don't have files in the platform project pointing to things in node_modules, so the project is more self-contained.
Since we now have a copy of the plugin inside the platform folder, we can edit its Package.swift to update any references to CordovaLib to point to the same module as the rest of the app so there are no issues with version mismatches.
Remaining Work
(By this, I mean, what we should do when a plugin wants to consume a library through CocoaPods for compatibility with older releases but wants to consume the same library through SwiftPM going forward)
Testing
Tested locally against this branch of cordova-plugin-device: https://github.com/dpogue/cordova-plugin-device/tree/spm
Checklist