Friday, August 12, 2022
HomeiOS DevelopmentA unique approach to develop SwiftPM Packages inside Xcode initiatives — Erica...

A unique approach to develop SwiftPM Packages inside Xcode initiatives — Erica Sadun

WWDC gave us many causes to each migrate libraries to SwiftPM and to develop new ones to help our work. The combination between Xcode improvement and SwiftPM dependencies retains rising stronger and extra vital.

Apple’s Enhancing a Bundle Dependency as a Native Bundle assumes you’ll drag in your bundle to an Xcode challenge as a neighborhood bundle overrides one which’s imported by a traditional bundle dependency.

In Growing a Swift Bundle in Tandem with an App, Apple writes, “To develop a Swift bundle in tandem with an app, you possibly can leverage the conduct whereby a neighborhood bundle overrides a bundle dependency with the identical title…in case you launch a brand new model of your Swift bundle or need to cease utilizing the native bundle, take away it from the challenge to make use of the bundle dependency once more.”

I don’t use this strategy. It’s not unhealthy or unsuitable, it simply doesn’t match my model.

Alternatively, opening the Bundle.swift file on to develop has drawbacks in that it doesn’t absolutely supply Xcode’s suite of IDE help options but.

So I’ve been engaged on a private answer that finest works for me. I would like my bundle improvement and its exams to stay individually from any particular shopper app exterior a testbed. I would like to make sure that my code will swift construct and swift check correctly however I additionally need to use Xcode’s built-in compilation and unit testing with my comfortable inexperienced checks.

I set out to determine how finest, not less than for me, to develop Swift packages below the xcodeproj umbrella.

I first explored  swift bundle generate-xcodeproj. This builds an Xcode library challenge full with exams and a bundle goal. You need to use the --type flag to set the bundle to executable, system-module, or manifest as an alternative of the default (library) throughout swift bundle init:

Generate% swift bundle init
Creating library bundle: Generate
Creating Bundle.swift
Creating .gitignore
Creating Sources/
Creating Sources/Generate/Generate.swift
Creating Exams/
Creating Exams/LinuxMain.swift
Creating Exams/GenerateTests/
Creating Exams/GenerateTests/GenerateTests.swift
Creating Exams/GenerateTests/XCTestManifests.swift
Generate% swift bundle generate-xcodeproj
generated: ./Generate.xcodeproj

Though SwiftPM creates a .gitignore file for you as you see, it doesn’t initialize a git repository. Additionally, I at all times find yourself deleting the .gitignore as I exploit a personalized world ignore file. That is what the ensuing challenge appears to be like like:

As you see, the generated Xcode challenge has every little thing however a testbed for you. I actually like having an on-hand testbed, whether or not a easy SwiftUI app or a command line utility to play with concepts. I appeared into utilizing a playground however let’s face it: too sluggish, too glitchy, too unreliable.

It’s a ache so as to add a testbed to this set-up, so I got here up with a special approach to construct my base bundle atmosphere. It’s hacky however I a lot choose the end result. As a substitute of producing the challenge, I begin with a testbed challenge after which create my bundle. This strategy naturally packs a pattern with the bundle however none of that pattern leaks into the bundle itself:

I find yourself with three targets: the pattern app, a library constructed from my Sources, and my exams. The library folder you see right here incorporates solely an Information.plist and a bridging header. It in any other case builds from no matter Sources I’ve added.

I a lot choose this set-up to the generate-xcodeproj strategy, though it takes barely longer to set-up. The explanation for that is that SwiftPM and Xcode use totally different philosophies for the way a challenge folder is structured. SwiftPM has its Sources and Exams. Xcode makes use of a supply folder named after the challenge.

So I take away that folder, add a Sources group to the challenge, and be sure that my construct phases sees and compiles these recordsdata. The Exams want related tweaks, plus I’ve so as to add a symbolic hyperlink from Xcode’s exams title (e.g. “ProjectNameExams”) to my SwiftPM Exams folder on the prime stage of my challenge to get it to all hold collectively. As soon as I’ve executed so my inexperienced checks are prepared and ready simply as if I had opened the Bundle.swift file instantly. However this time, I’ve all the correct instruments at hand.

Since I’m speaking about set-up, let me add that my duties additionally embody organising the README, including a license and creating the preliminary change log. These are SwiftPM setup duties that swift bundle init doesn’t cowl the way in which I like. I trash .gitignore however since I’ve Xcode set-up to routinely initialize model management, I don’t need to git init by hand.

I believe it is a short-term workaround as I anticipate the combination of SwiftPM and Xcode to proceed rising over the subsequent couple of years. Since WWDC, I’ve been significantly enthusiastic about creating, deploying, and integrating SwiftPM packages. I assumed I’d share this in case it’d assist others. Let me know.



Please enter your comment!
Please enter your name here

Most Popular