Using the bowling kata as an example. Step by step instruction to set up a project on the command line with the Swift package manager. Tests with Quick and Nimble.
mkdir kata
cd kata
swift package init --type executable
--type executable
is important, otherwise the package is created as a library
At this point the project structure looks like this:
.
├── Package.swift
├── README.md
├── Sources
│ └── kata
│ └── main.swift
└── Tests
cd Sources
mkdir bowling
cd bowling
touch bowling.swift
Move back to the root folder
cd Tests
mkdir bowlingSpec
cd bowlingSpec
touch bowlingSpec.swift
Move back to the root folder
At this point the project structure looks like this:
.
├── Package.swift
├── README.md
├── Sources
│ ├── bowling
│ │ └── bowling.swift
│ └── kata
│ └── main.swift
└── Tests
└── bowlingSpec
└── bowlingSpec.swift
vim Package.swift
Edit the file to include Quick and Nimble as dependencies. The file should look like this in the end:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "kata",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/Quick/Quick.git", from: "1.2.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "7.0.3"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "kata",
dependencies: ["bowling"]),
.target(
name: "bowling",
dependencies: []),
.testTarget(
name: "bowlingSpec",
dependencies: ["bowling", "Quick", "Nimble"]),
]
)
swift package update
swift build
swift package generate-xcodeproj
To run a swift project: swift run
To run all tests: swift test