10.4 C
Canberra
Friday, September 20, 2024

A distinct method 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 assist our work. The combination between Xcode growth and SwiftPM dependencies retains rising stronger and extra necessary.

Apple’s Enhancing a Package deal Dependency as a Native Package deal assumes you’ll drag in your package deal to an Xcode mission as an area package deal overrides one which’s imported by way of a standard package deal dependency.

In Creating a Swift Package deal in Tandem with an App, Apple writes, “To develop a Swift package deal in tandem with an app, you’ll be able to leverage the conduct whereby an area package deal overrides a package deal dependency with the identical title…for those who launch a brand new model of your Swift package deal or need to cease utilizing the native package deal, take away it from the mission to make use of the package deal dependency once more.”

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

Alternatively, opening the Package deal.swift file on to develop has drawbacks in that it doesn’t absolutely provide Xcode’s suite of IDE assist options but.

So I’ve been engaged on a private answer that finest works for me. I need my package deal growth and its checks to reside individually from any particular shopper app exterior a testbed. I want to make sure that my code will swift construct and swift take a look at correctly however I additionally need to use Xcode’s built-in compilation and unit testing with my joyful inexperienced checks.

I set out to determine how finest, a minimum of for me, to develop Swift packages below the xcodeproj umbrella.

I first explored  swift package deal generate-xcodeproj. This builds an Xcode library mission full with checks and a package deal goal. You should utilize the --type flag to set the package deal to executable, system-module, or manifest as a substitute of the default (library) throughout swift package deal init:

Generate% swift package deal init
Creating library package deal: Generate
Creating Package deal.swift
Creating README.md
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 package deal 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 all the time find yourself deleting the .gitignore as I take advantage of a custom-made world ignore file. That is what the ensuing mission seems to be like:

As you see, the generated Xcode mission has all the pieces 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 gradual, 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 unique method to construct my base package deal setting. It’s hacky however I a lot favor the end result. As a substitute of producing the mission, I begin with a testbed mission after which create my package deal. This method naturally packs a pattern with the package deal however none of that pattern leaks into the package deal itself:

I find yourself with three targets: the pattern app, a library constructed from my Sources, and my checks. 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 favor this set-up to the generate-xcodeproj method, 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 mission folder is structured. SwiftPM has its Sources and Exams. Xcode makes use of a supply folder named after the mission.

So I take away that folder, add a Sources group to the mission, and be sure that my construct phases sees and compiles these information. The Exams want related tweaks, plus I’ve so as to add a symbolic hyperlink from Xcode’s checks title (e.g. “ProjectNameExams”) to my SwiftPM Exams folder on the prime stage of my mission to get it to all dangle collectively. As soon as I’ve accomplished so my inexperienced checks are prepared and ready simply as if I had opened the Package deal.swift file immediately. However this time, I’ve all the fitting 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 package deal init doesn’t cowl the best way I like. I trash .gitignore however since I’ve Xcode set-up to robotically initialize model management, I don’t should git init by hand.

I believe it is a short-term workaround as I anticipate the mixing 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 would assist others. Let me know.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles