Building packages with Swift Package Manager and Docker without Xcode
Swift Package Manager
Apple has recently open-sourced Swift and announced it’s own package management tool called “Swift Package Manager“. Swift has became cross platform and can be built on both macOS and Linux so Swift Package Manager can be
- Dependency Manager
- Build Tool
- Test Tool
IBM is currently working hard to provide centralised repository for hosting those package and creating server side frameworks. The best place to learn about all the recent activity is ‘swift.org‘. In this tutorial, we will create a Swift package on using Docker.
Swift Package and Docker
As Swift became server side, we can build it on the docker containers. In this short tutorial we will use IBM Swift3 Ubuntu Docker image to build a simple package. Let’s create a directory called ‘SwiftPM-Docker’ and create a Dockerfile
1 2 3 4 |
$ mkdir SwiftPM_Docker $ vim Dockerfile |
Now let’s create a simple docker image on top of the IBM ubuntu image.
1 2 3 4 5 6 7 8 9 |
FROM ibmcom/swift-ubuntu:latest EXPOSE 8090 USER root RUN apt-get install -y libhttp-parser-dev libcurl4-openssl-dev libhiredis-dev RUN mkdir SwiftPM-Docker RUN cd SwiftPM-Docker WORKDIR SwiftPM-Docker |
We have Dockerfile which will “SwiftPM-Docker” directory on the containers once launched. Let’s build the docker image which we call ‘swift-package-manager’
1 2 3 4 |
$ docker build -t swift-package-manager . $ docker run -it --security-opt seccomp=unconfined -p 8090:8090 -v $CWD:/root/SwiftPM-Docker -t swift-package-manager /bin/bash |
You will see the image will start to download
Once image is dowloaded, we will be inside the container with mounted directory from our local macOS.
Driving Swift from Docker Container
Now that we have fully provisioned Docker container running swift3. We can create Swift package template, build and test it by running following commands.
1 2 3 4 5 6 7 |
$ swift package init --type=library $ swift build $ swift test |
It will look something like this :
This is default “Hello World” Package generated by Swift Package Manager. Let’s build something simple package which will greet you with your name saying ” Welcome to Swift package manager #{your_name}”
Create Greetings package
Now we will create simple greetings package. So edit the “Sources/SwiftPM_Docker.Swift” and update to
1 2 3 4 5 6 7 8 9 |
extension String { public func greetMe()->String { return "Welcome to Swift Package Manager \(self)" } } |
And also update the test “Tests/SwiftPM_Docker/SwiftPM_DockerTests.swift” with following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import XCTest @testable import SwiftPM_Docker class SwiftPM_DockerTests: XCTestCase { func testExample() { XCTAssertEqual("Shashi".greetMe(), "Welcome to Swift Package Manager Shashi") } static var allTests : [(String, (SwiftPM_DockerTests) -> () throws -> Void)] { return [ ("testExample", testExample), ] } } |
Now that you have package returning greeting message with passing tests. Check out running ‘swift build and swift test’ commands.
Add CLI Interface
Now let’s add CLI interface so that we can execute package from command line. We will arrange the directory structure in the “Sources”. Let’s create two new directory.
1 2 3 4 |
$ mkdir Sources/SwiftPM_Docker $ mkdir Sources/cli |
Now move “SwiftPM_Docker.swift” file to “SwiftPM_Docker” directory and created “main.swift” in the cli directory with following content which jus print execution
1 2 3 4 5 |
import SwiftPM_Docker print(Process.arguments[1].greetMe()) |
Now we need to update ‘Package.Swift” file to let app know about the dependency package we created.
1 2 3 4 5 6 7 8 9 10 11 |
$ vim Sources/cli/main.swift import PackageDescription let package = Package( name: "SwiftPM-Docker", targets: [ Target(name: "cli", dependencies: ["SwiftPM_Docker"]) ] ) |
Now run the ‘swift build’ you will see debug output will be generated.
1 2 3 4 5 6 7 |
$ swift build $ .build/debug/cli Daddy Welcome to Swift package manager Daddy |
Watch the animated GIF for the above things
That’s it. We have our brand new package for the release on Gihub. Just push it and ship it.
Using Xcode
As of now, we haven’t touched Xcode but if you want to then we can use it in our local macOS development environment.
Fortunately, there. is a ability we can generate Xcode project from the Docker and which will be mapped to local macOS machine.
1 2 3 |
$ swift package generate-xcodeproj |
Source Code:
The Source Code is available on the Github :
https://github.com/Shashikant86/SwiftPackageManager-Docker
Look forward to create more packages in the near future !