I'm just trying to play with swift language and thinking of build a swift UI library. I created a library project for this requirement and I would like to integrate this library with an executable project. Now for quick testing, development and prototyping what is the recommended way to setup the library project and executable project?
XCode Project (maybe with Tuist), a local Swift Package dependency and an xcworkspace. Drop projects into the workspace's project navigator in order to work on all of them simultaneously.
@Akazm I'm using linux (Ubuntu 24.04) for my development. Is there a way I can configure both a library and an executable project for better developer experience?
Are you asking how to add a second executable target to an existing Package.swift file?
If yes, extend your Package.swift file so it looks something like this:
// swift-tools-version: 6.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyLibrary",
products: [
// Your existing library product
.library(name: "MyLibrary", targets: ["MyLibrary"]),
],
targets: [
// Your existing library target and test target
.target(name: "MyLibrary"),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
+ // The new executable target
+ .executableTarget(
+ name: "MyCLI",
+ // Make sure to add your library target as a dependency
+ dependencies: ["MyLibrary"],
+ ),
],
swiftLanguageModes: [.v6]
)
Then create a new directory Sources/MyCLI and add your executable’s source files there.
swift run will automatically run the executable (if there's only one executable in the package. Otherwise you have to specify the name explicitly).
You can optionally also add an additional .executable(…) under the products: array in Package.swift, but that's not necessary. SwiftPM will pick the executable target for running.