Set up a Swift project

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.

  1. mkdir kata
  2. cd kata
  3. 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
  1. cd Sources
  2. mkdir bowling
  3. cd bowling
  4. touch bowling.swift

Move back to the root folder

  1. cd Tests
  2. mkdir bowlingSpec
  3. cd bowlingSpec
  4. 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
  1. 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"]),
    ]
)

  1. swift package update
  2. swift build
  3. swift package generate-xcodeproj

To run a swift project: swift run

To run all tests: swift test