Assume you init a swift package with command:
swift package init - name MyApp - type executable
and want to add package dependency, for example, `the composable architecture`.
For `dependencies` in Package, it is obvious. Just fill url and other specification in `.package()`.
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture/blob/main/Package.swift", branch: "main"),
],
targets: [
.executableTarget(
name: "MyCLI",
dependencies: [
],
path: "Sources"),
]
)
But for `dependencies` in `targets`, when you use `.product(name: , package: )` initializer, it is not that straid forward.
As in the `.product(name:, product:)`, the `name` parameter means the name of the product, the `package` parameter means the name of the package. The method to find the right property is, get to the package source code you want to add.
// Package.swift of the composable architecture
// swift-tools-version:5.9
import CompilerPluginSupport
import PackageDescription
let package = Package(
name: "swift-composable-architecture",
…
products: [
.library(
name: "ComposableArchitecture",
targets: ["ComposableArchitecture"]
)
]
…
)
In the `Package.swift`, the `name` property of Package as in `Package(name: “”)` should be the package name. the `name` property in `executableTarget()` could fill in the name parameter of `.product()`.
Therefore, the final config should be as follow.
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture/blob/main/Package.swift", branch: "main"),
],
targets: [
.executableTarget(
name: "MyCLI",
dependencies: [
.product(name: "ComposableArchitecture", package: "swift-composable-architecture")
],
path: "Sources"),
]
)