I'm writing a Swift script as a standalone Swift Package. Part of this script needs to generate some files, for which I have templates.
Now, these templates are not compilable (they're HTML files); so in order to include them in the package I've included them as .copy("Templates")
.
When debugging my script, I can access the templates just fine, but when I try to archive the product, the executable doesn't have access to them anymore.
Here's my Package.swift:
let package = Package(
name: "flucs",
products: [
.executable(name: "Flucs",
targets: ["flucs"])
],
dependencies: [
.package(url: "https://github.com/MrSkwiggs/Netswift", .exact(.init(0, 3, 1))),
],
targets: [
.target(
name: "flucs",
dependencies: ["Netswift"],
path: "Sources",
resources: [
.copy("Templates")
]),
.testTarget(
name: "flucsTests",
dependencies: ["flucs"]),
]
)
My folder structure:
How can I distribute my script so that it also includes its resources?
To distribute code in binary form as a Swift package, create an XCFramework bundle, or artifact, that contains the binaries. Then, make the bundle available locally or on a server: When you host the binaries on a server, create a ZIP archive with the XCFramework in its root directory and make it available publicly.
Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that's easy to use in your app's project. Each Swift package requires a Package.
To add a new Swift package dependency: Select your project in the Project navigator, then select your app target and navigate to its General pane. Click the + button in the "Frameworks, Libraries, and Embedded Content" section, select the local package's library product, and add it as a dependency.
SPM compiles your resources to a separate bundle but command line tool is just an executable without a bundle and any resources you add to your executable is simply ignored by Xcode (Build Phases > Copy Bundle Resources) for Release(Archive) builds.
If you look inside Bundle.module
you can find:
...
// For command-line tools.
Bundle.main.bundleURL,
...
Where Bundle.main.bundleURL
is a valid file url for the directory containing your command line executable so that it looks for your bundle next to your executable. And it works for Debug because XCode just compiles your resource bundle near your executable.
The simplest way to get Release executable with compiled .bundle
file is build your package from command line:
swift build --configuration release
And then you can find them both in .build/release
folder.
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