Swift Package Manager Logo

Implementing Swift Package Manager –Example 1

This entry is part 7 of 14 in the series Swift Package Manager

Simple, Single-Package, Static Library

In this example, the PackageConsumer utility simply consumes Package_A, Version 1.0.0.

Here is the GitHub link to the PackageConsumer project Version 1.0.0. You can check out that version, and reproduce the results here.

Fig. 1: The Hierarchy

Here is the source code for Package_A, version 1.0.0:

import Foundation

public protocol PackageProtocol {
    var text: String { get }
    var indent: Int { get }
}

public struct Package_A: PackageProtocol {
    public let indent: Int
    public let text: String
    public init(indent inIndent: Int = 0) {
        indent = inIndent
        let prefix = String(repeating: "\t", count: inIndent)
        text =  "\(prefix)Package_A, Version: 1.0.0"
    }
}

And here is its Package.swift file:

import PackageDescription

let package = Package(
    name: "Package_A",
    platforms: [
        .iOS(.v11),
        .tvOS(.v11),
        .macOS(.v10_14),
        .watchOS(.v5)
    ],
    products: [
        .library(
            name: "Package-A",
            targets: ["Package_A"]),
    ],
    targets: [
        .target(
            name: "Package_A",
            dependencies: []),
        .testTarget(
            name: "Package_ATests",
            dependencies: ["Package_A"]),
    ]
)

And this is the PackageConsumer.swift file:

import Foundation
import Package_A

struct PackageConsumer {
    let text: String
    init(text intext: String = "PackageConsumer, Version 1.0.0") {
        text = intext + "\n" + Package_A(indent: 1).text
    }
}

If we start up Xcode, and run the command-line utility, we will get something that looks like this, in the debug console:

PackageConsumer, Version 1.0.0
	Package_A, Version: 1.0.0