I write a framework and I like to divide framework to small separate submodules (targets). Apple provides a cool description for thing I want to achieve with CocoaPods:
Targets are the basic building blocks of a package. A target can define a module or a test suite. Targets can depend on other targets in this package, and on products in packages which this package depends on.
I could easily do it with Swift Package Manager:
targets: [
.target(name: "Network"),
.target(name: "Service", dependencies: ["Network"])
],
I could use import Network
in a Service
target and it's cool because they are separate modules, logic is clear.
How to achieve it in CocoaPods and Carthage (I write a framework, not the final application)?
I tried to use subspecs:
s.subspec 'Service' do |ss|
ss.dependency 'MyFramework/Network'
ss.source_files = 'Sources/Service/**/*.swift'
end
s.subspec 'Network' do |ss|
ss.source_files = 'Sources/Network/**/*.swift'
end
It doesn't work as I want because CocoaPods just merges all files into one framework (just divides it to separate folders), so:
import Network
inside Service
because there is no Network
target after pod install
. So I can't use one that framework with Swift Package Manager. CocoaPods just merges everything to one target MyFramework
as I mentioned before.It's the solution but it's very hard to maintain multiple separate git repositories and make separate commit and pushes. I want to keep everything in a one repo.
🎉 Cocoapods is a popular tool that simplifies installing and sharing Swift frameworks. In this post we will walk through the process of sharing a Swift framework through Cocoapods. Later we will consume it inside an app. We will host our framework in a git repository which Cocoapods will access.
I'm pretty sure that CocoaPods doesn't have the ability to create separate targets for one podspec.
As you mention above you can create separate podspecs, but in one repository like RxSwift. Then while you are developing you can use some Example project which includes all your podspecs. You don't need to manage different repositories.
Also, you can try to create a module map with different modules, e.g.:
framework module Something {
framework module Core { ... }
framework module Toolbox { ... }
...
}
And then assign it in podspec, e.g.:
Pod::Spec.new do |s|
s.module_map = "#{s.name}/#{s.name}.modulemap"
...
But I'm not sure about that. I've tried only the first way as in RxSwift
repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With