Hello, I am a Swift beginner trying to build a simple command line tool.
I used swift package manager to initialize my tool, and built a Xcode project file then wrote code using Xcode. I want to access some configure files in run-time, so I figured out to add a
Copy Files Phase in
Build Phase of Xcode project setting. It works if I build my tool in Xcode, but not using
I am wondering what I should do to add a
Copy Files Phase for
swift build as well? Or are their any other approach I can access files in run-time?
Since the Xcode project is generated from the SPM package, modifications done in Xcode won't affect the package itself.
SPM does not currently support resources, so you can't currently include the files you need in the package directly. You could maybe move the file contents into string literals in a Swift file. If that is not an option you'll need to handle the files yourself and make sure they are shipped with the built utility.
Thank you so much ahti. I guess I will make a new project directly from Xcode.
I followed an online tutorial to build command line tool using SPM package, are there any advantages of doing that over starting from Xcode?
The big advantage of the SPM package is that it can be built on macOS, Linux and any other platforms supported by SPM. It also allows other SPM packages to depend on your code easily.
Since you're building a command line utility, you probably don't care about using your code as a dependency, so the question is: Do you want to build anywhere other than macOS?
Oh I see. My CLI is made for macOS users only, so I guess I have no any loss for using Xcode instead.
I have had a lot of problems with Xcode and SPM, have now given up and just use Xcode (suggest you do the same). Hopefully the two will be integrated shortly :)
I just found SPM has a very neat command argument parser utility, is it possible to include SPM as a framework and use that utility in a pure Xcode command line tool project?
SwiftPM exports a "Utility" product which can be used in other Swift packages. See: swift-package-manager/Package.swift at main · apple/swift-package-manager · GitHub
Note: The APIs are unstable and may change without notice.
Swift package manager is lacking several key features right now and resources is one of them. The SwiftPM developers will love to see some proposals from the community in this area. Related JIRA: [SR-2866] [SwiftPM] Need a way to include resources with targets · Issue #5199 · apple/swift-package-manager · GitHub