Categories
Swift Xcode

Swift Package Manager.1

Today I would like to write about Swift Package Manager (SPM), which was introduced already several years ago, but I’ve never had a chance to try as I was mostly using Cocoa Pods for managing dependencies.

I will split my experience over several posts (4), because there is a lot to write, so this one will be the first one covering the basics.

What is SPM?

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

So in fact, if you have some code, you want to reuse in multiple projects, you can easily create a Swift package, import it into your various projects easily. You can use directly from a git branch or a specific released version.

Let’ create a package

First thing, it should be in a git repo. It works well with GitHub and Bitbucket from what I have tried. So first, start by creating a repository into your favourite Git host. For our example, we’ll call it FluentCoding.MediaControl

Ps: Let’s make the repo Private, because in a later post, we’ll discuss some specific cases (in Part 4)

After you do so, checkout your repo into your machine and open a terminal.

$ cd FluentCoding.MediaControl
$ swift package init
 Creating library package: FluentCoding.MediaControl
 Creating Package.swift
 Creating README.md
 Creating Sources/
 Creating Sources/FluentCoding.MediaControl/FluentCoding_MediaControl.swift
 Creating Tests/
 Creating Tests/LinuxMain.swift
 Creating Tests/FluentCoding.MediaControlTests/
 Creating Tests/FluentCoding.MediaControlTests/FluentCoding_MediaControlTests.swift
 Creating Tests/FluentCoding.MediaControlTests/XCTestManifests.swift
$ swift package generate-xcodeproj
 generated: ./FluentCoding.MediaControl.xcodeproj
 

That’s it, great 👍 You just created your first package. You can then commit and push everything to git now.

What does the Swift package contain?

3 important things:

  • Package.swift: This file will contain the definition of the package. The name, it’s dependencies, the minimal supported versions, and so on
  • A Sources folder: There, you will be able to put your own code that you want to ship with the package.
  • A Test folder: There, you are able to create unit test code, which can be run within Xcode, or on command line when using a automated process (We’ll check this in a later post)

Basically that’s all you need to get started for you. Note, we added the command swift package generate-xcodeproj, which will create the xcode project, but this is not done by default. I prefer to have it and manage everything within xcode.

Links

  • SPM repo on github: https://github.com/apple/swift-package-manager
  • Documentation: https://swift.org/package-manager/

Thank you for reading until here…  👊 Have a good week end…  🍺