Swift Package Manager Logo

Implementing Swift Package Manager –Example 4

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

Simple Static Packages (Second Version)

In this example, the PackageConsumer utility simply consumes Package_BPrime, Version 2.0.0, which is a static library, that, in turn, consumes Package_A Version 2.0.0 (also a static library).

This is pretty much exactly the same as Example 2, but with different versions specified.

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

Fig. 1: The Hierarchy

Here is the source code for Package_BPrime, version 2.0.0:

import Foundation
import Package_A

public struct Package_BPrime: 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_BPrime, Version: 2.0.0\n" + Package_A(indent: inIndent + 1).text
    }
}

Here is the source for the Package_BPrime, Version 2.0.0 Package.swift file:

import PackageDescription

let package = Package(
    name: "Package_BPrime",
    platforms: [
        .iOS(.v11),
        .tvOS(.v11),
        .macOS(.v10_14),
        .watchOS(.v5)
    ],
    products: [
        .library(
            name: "Package-BPrime",
            targets: ["Package_BPrime"]),
    ],
    dependencies: [
        .package(name: "Package_A", url: "git@github.com:LittleGreenViper/SPMArticle-Package_A.git", from: "2.0.0")
    ],
    targets: [
        .target(
            name: "Package_BPrime",
            dependencies: [
                .product(name: "Package-A", package: "Package_A")
            ]
        ),
        .testTarget(
            name: "Package_BPrimeTests",
            dependencies: ["Package_BPrime"]
        ),
    ]
)

Here is the source code for Package_A, version 2.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: 2.0.0"
    }
}

Here is the source for PackageConsumer.swift, Version 4.0.0:

import Foundation
import Package_BPrime

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

Running that, gives us something like this:

PackageConsumer, Version 4.0.0
	Package_BPrime, Version: 2.0.0
		Package_A, Version: 2.0.0