Useful commands

The list of commands for this is not that broad like other dependency managers. An important command is as follows:

swift build

It automatically downloads all dependencies defined in the manifest file. Then, they are compiled and linked to the current module, but to use it, you need a working manifest file—Package.swift.

If you want to develop an executable Terminal project  from scratch, then you have to use the following command:

swift package init --type executable

We can use --type library if we want to develop a Swift library and get the following output:

The following command will generate an empty executable project which can be executed with:

swift run

If everything is working, then you should see Hello World! printed in the Terminal, similar to the following screenshot:

The name of the project is the same as the folder name.

With the following command, you can generate an Xcode project which can be used to develop the app:

swift package generate-xcodeproj

The generated project includes all the files which are described in the manifest file. The actual project structure is simple as shown in the following screenshot:

You have to add other source files in the correct Sources subfolders. By convention, the Sources folder should contain all the source files.

Now, let's try to create a library module in Swift. We can create a new folder—swift-lib—on the disc and open the folder in the Terminal. Then, by executing the following command which we already know:

swift package init --type library

We end up with the following project structure:

The SPM supports testing of the code. This modern approach ensures the higher quality of the code if tests are developed with the library. To start the tests, you have to run the following command:

swift test

Here is an example of the output after a successful execution:

Let's add a structure to the library and a few tests. We can generate an Xcode project:

swift package generate-xcodeproj

Then, we will open the swift_lib.swift file and we add the following structure:

//public to be accessible from other modules
public struct Toy {
public var name = "Unknown"
public var age = 1
public var price = 1.0

public init(name: String, age:Int, price:Double) {
self.name = name
self.age = age
self.price = price
}
}

Then, we open the test file, swift_libTests.swift. In that file we add two new tests, which are checking the default construction of a toy instance:

    func testToyDefaultValues() {
let toy = Toy()
XCTAssertEqual(toy.name, "Unknown")
XCTAssertEqual(toy.age, 1)
XCTAssertEqual(toy.price, 1.0)
}

func testToy() {
let toy = Toy(name: "Rex", age: 2, price:99)
XCTAssertEqual(toy.name, "Rex")
XCTAssertEqual(toy.age, 2)
XCTAssertEqual(toy.price, 99.0)
}
//update this for Linux
static var allTests = [
("testExample", testExample),
("testToyDefaultValues", testToyDefaultValues),
("testToy", testToy),
]

Everything looks clean and neat in the Xcode:

Don't forget to add every test case to the static property—allTests—these tests will be executed on Linux.

We have two different modules which are living next to each other on the filesystem. In your case, it depends where you have created both folders. Now, we will show you how to integrate the library module in the executable application. To achieve this, we have to open the library project and create a Git repository. We should execute the following commands in the Terminal:

git add .
git commit -m "Initial Commit"
git tag 1.0.0

If you have other commits or you have been using Git, you need at least one version tag, like 1.0.0. The local Git repository will be used to fetch the dependency, but first, we have to update the manifest file of the executable project:

let package = Package(
name: "swift-executable",
dependencies: [
// update the url if your folders are located on different place
.package(url:"../swift-lib/", from: "1.0.0"),
],
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: "swift-executable",
dependencies: ["swift-lib"]),
]
)

Now, we can update the executable file and try to use our structure defined in the module. Here is the new version of main.swift:

import swift_lib

let toy = Toy(name: "Rex", age: 2, price: 99)
print("Hello, (toy.name)!")

Then, to run the new example, you have to execute the following command:

swift run

Here is the result which you should see in the Terminal:

We have learned how to use the Swift Package Manager from the Terminal. It's not limited to macOS, so we can use it when we are working on the Linux platform. We can create empty library projects and executable projects, and we can use Git and link together many projects.

Keep the executable project minimal. Implement the solution in a separate library module(s) and reuse it in different projects.

We have said that there are many open source projects, which we can use in our apps. Let's discuss some popular ones and the easiest way to find others.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.186.83