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

Now let’s create a simple docker image on top of the IBM ubuntu image.

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’

You will see the image will start to download

Screen Shot 2016-08-23 at 22.31.00

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.

It will look something like this :

docker-swift

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

 

And also update the test “Tests/SwiftPM_Docker/SwiftPM_DockerTests.swift” with following

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.

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

Now we need to update ‘Package.Swift” file to let app know about the dependency package we created.

Now run the ‘swift build’ you will see debug output will be generated.

Watch the animated GIF for the above things

spm-docker

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.

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 !