Swift Package Manager(SPM) Part 2
In this blog we will see how we can create our own swift package and published it as private and how can we use it on some other project
Create A Swift Package
Packages are a great way to share code both in your workspace with your team or with a wider open source community
As shown in Figure 1 Go to File → New → Swift Package → Home
As shown in Figure 2 we created Home
package (module). Before we go further let’s do some theory parts
Package
- A package is a directory that contains a Swift Package manifest file. As shown in Figure 2
Package.swift
is a manifest file - The manifest is a file called
Package.swift
and it identifies that directory is a Swift Package - It also contains the
Source
andTests
directory - Underneath the source directory, there are subdirectories for each of the separate targets in the package. These are the separately buildable component of the package and similarly, under the Test, there is a separate subdirectory for every test suite. Currently our
Home
package only have onetarget
and onelibrary
which is also called Products. If You don’t get point 4, it’s OK I will refer to this point in the future as well when we work on a complex manifest file
Manifest File
// swift-tools-version:5.3
, the first line in the manifest file. This is the minimum version of the Swift compiler that is required to build your package- After the first line, we define package configuration using package description API
- The package description API is a declarative API that is provided by the package manager package description library and by importing that then the rest of the contents of the file can declare the characteristics of the package
- By using the Package constructor we first define the
name
of the package,products
thentargets
. We will explore more this later when we will be playing with manifest file - products → This section lists product package vends to the client, So the package can control which parts of your code can be directory imported by the client and in this case, we only have one library with the same name as
package
called “Home
”. In short, library publishes theHome
target to the client as a library - Target → list individual buildable/executable parts of the package
- Test target are under directory called Tests ad they also have their own subdirectory
- They are declared using the test target API and since test target are usually testing another Target , so you need to provide dependency on the target that it is testing
- Note: Test target will not actually be linked to the client
As shown in Figure 3, we write some unit test and added some code
Publish Swift Package
As shown in Figure 4, I just created a private repo in github repo
Open Terminal ,
- Change the current working directory to your local project
- run
git init
Run
git add .
git commit -m “First commit”
git remote add origin git@github-aliakhtar49:aliakhtar49/Home.git (
At the top of your GitHub repository’s Quick Setup page, click to copy the remote repository URL., since I am using mulitple ssh I usedgithub-aliakhtar49)
git push -u origin master
Now tap on create new release
Then public release
As shown in figure 10 we published a release with tag 1.0.0
Used Published Swift Package
File → New → Swift Packages → Add Package Dependency
Select Main App target
Choose Package repo
As shown in Figure 13 Xcode are unable to fetch , since it’s a private repo we need to add github account in Xcode as well
Go to Xcode → Preference → Account → + → Select Github → Continue
As shown in Figure 15, we need to generate access token for that
Go to github account → Setting → Developer settings → Personal access tokens. → Generate token → Make sure to copy your new personal access token now. You won’t be able to see it again!
As shown in Figure 17, I configured Xcode with ssh and point it to the right ssh file
As shown in Figure 18 , Xcode now successfully fetch repo through ssh
you can use https
as well but I prefer ssh
, we need to configure github
on Xcode since repo is private
As shown in Figure 20 we successfully imported our Home module
As shown in Figure 20 , we imported package since Xcode fetch package from repo and linked automatically package to our project, so in next section we will discuss how Xcode linked packages
Linking Package Libraries
- Your project contain source files and you package also contains source files under directory Source
- What Xcode does it takes all of these source files and it compiles them and particularly compiles the package code in a way that is compatible with the app code in your project. These includes architecture , platform etc
- It recompile multiple times, if needed depending on what your app needs
- Then it links it in and combines all of that into the application
- Package library are static by default and so all the code is linked together and this is repeated for various apps in your project that use the same package Like if you have an iOS app and watch OS app, they use the same package. Xcode might build the code multiple times as needed for each of those apps
Next
In the next part we will see how we can edit package by locally adding in our project
Useful Links
https://developer.apple.com/videos/play/wwdc2019/410/?time=1432