tux

Package manager is the swift system to build libraries and executable to share projects. First of all we test the functionality of the tool (I'm using ubuntu server 16.04.3 LTS with swift 4.0.2 installed as described in Swift Ubuntu 16.04 post):

$ swift package
OVERVIEW: Perform operations on Swift packages

Installing swift on a new server might miss the curl package, in the case:

$ sudo apt-get install libcurl4-openssl-dev

We create the directory of the new package then initialize the package:

$ mkdir myfirst
$ cd myfirst
$ swift package init

The following structure is created.

.
├── Package.swift
├── README.md
├── Sources
│   └── myfirst
│   └── myfirst.swift
└── Tests
├── LinuxMain.swift
└── myfirstTests
└── myfirstTests.swift

4 directories, 5 files

The Package.swift file is called a manifest file, it contains all the references for the package for compilation.

// 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: "myfirst",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "myfirst",
targets: ["myfirst"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, 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: "myfirst",
dependencies: []),
.testTarget(
name: "myfirstTests",
dependencies: ["myfirst"]),
]
)

The source code is stored in the Sources/myfirst/myfirst.swift file

struct myfirst {
var text = "Hello, World!"
}

that the system kindly initialized with sample code. Type swift build to compile the package by automatically downloading the dependencies listed in the manifest file. To test the package, type swift test.

$ swift test
Compile Swift Module 'myfirstTests' (1 sources)
Linking ./.build/debug/myfirstPackageTests.xctest
Test Suite 'All tests' started at 16:05:19.192
Test Suite 'debug.xctest' started at 16:05:19.271
Test Suite 'myfirstTests' started at 16:05:19.271
Test Case 'myfirstTests.testExample' started at 16:05:19.271
Test Case 'myfirstTests.testExample' passed (0.0 seconds)
Test Suite 'myfirstTests' passed at 16:05:19.272
Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
Test Suite 'debug.xctest' passed at 16:05:19.273
Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
Test Suite 'All tests' passed at 16:05:19.273
Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds

A package is considered executable if it contains a file named main.swift. Let's look at the steps above to create an executable package. After deleting any previously created files in the myfirst directory, type the following commands.

$ swift package init --type executable
Creating executable package: myfirst
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/myfirst/main.swift
Creating Tests/
$ swift build
Compile Swift Module 'myfirst' (1 sources)
Linking ./.build/x86_64-unknown-linux/-debug/myfirst

At this point, the executable program will be located in the ./build/x86_64-unknown-linux/debug directory.

One last thing about how to work with multiple source files. For example, defining a function in a file other than the main.swift file, you can recall the function without any import since the new file resides on the same main module. Let's see an example:

/Sources/myfirst$ touch greetings.swift

Into greetings.swift type with an editor the following function:

func TellHello(name: String) {
print("Hello \(name)")
}

Modify main.swift as follow:

if CommandLine.arguments.count != 2 {
print("Usage: myfirst NAME")
} else {
let name = CommandLine.arguments[1]
TellHello(name: name)
}

Compile the package by typing swift build then go to the directory ./build/x86_64-unknown-linux/debug and launch myfirst program:

.build/debug$ ./myfirst Paolo
Hello Paolo