Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2017-01-29

Migrating a Xcode project to a Package | Swift Package Manager

SwifterSockets lives on github. You can find it here. Before version 0.9.8 it was a "plain" Xcode project. But since the Swift Package Manager is now out (included in Swift 3), I wanted to change that to an SPM package.

Important: The following is applicable only to frameworks (or frameworks to be). Not applications.

If you want to convert your project, be sure to have a backup - we will destroy the current Xcode project! (If your project is on github too, that should not be any problem...)

The first thing to realize is that SPM needs a git repository. I already had a repository but the content should change rather dramatically. This is how I did that:

Using the terminal, I create a directory with the same name as the project:

    $ mkdir SwifterSockets

In that directory I created a new SPM pakage:

    $ cd SwifterSockets
    $ swift package init

In that directory we now find the following files:

    .gitignore
    Package.swift
    Sources
    Tests

This is the content of the current location of the Xcode project:

    .git
    .gitignore
    SwifterSockets
    SwifterSockets.xcodeproj
    build

Next I merged the content of these two directories. Note that there was already a .gitignore present. That was overwritten with the new version.

    .git
    .gitignore
    Package.swift
    Sources
    SwifterSockets
    SwifterSockets.xcodeproj
    Tests
    build

Next I copied all the source code files that made up SwifterSockets to the Sources directory.

Then came the slightly nerving step: delete the old Xcode project and associated stuff:

    $ rm -rf SwifterSockets
    $ rm -rf SwifterSockets.xcodeproj
    $ rm -rf build

Poof... all gone... this is what remains:

    .git
    .gitignore
    Package.swift
    Sources
    Tests


(Remember, all the source code I need is still there, in the Sources directory)

At this point we can try a build:

    $ swift build

If the sources are complete, this should produce no errors. But your milage may vary if there were dependencies on other packages/libraries etc.

Since editing in vi is not everybody's cup of tea, lets create a new Xcode project:

    $ swift package generate-xcodeproj

The directory content is now:

    .git
    .gitignore
    Package.swift
    Sources
    SwifterSockets.xcodeproj
    Tests

When we open this xcode project, the sources are all there and we can edit/compile it all. Remember though that compiling in Xcode is not the same as compiling at the command line. That is a separate compilation and is an extra step. However for frameworks we don't often have to go there.

Four things left:
1) Make sure that in the new xcode project the Build Setting - Packaging - Defines Module is set to 'Yes'.
2) Update the .gitignore settings. Mine looks as follows:

    .DS_Store
    /.build
    /Packages
    xcuserdata
    build

3) Put everything in the repository:

    $ git add .
    $ git commit -m "Migration to SPM"
    $ git tag 10.0.0
    $ git push

The last bit is of course optional. And the version number is up to you.

The fourth step is using the fresh release.

That can be done as a package in other packages that are build with the SPM. Or it can be done as a Modular Framework.

Using it as a SPM Package should be clear, otherwise you may want to look up the SPM user manual.

Using it as a modular framework is done by creating the framework in the Xcode project and then importing it into another project as either Embedded Binaries (dynamic framework) or Linked Frameworks and Libaries (static framework).

If you create a framework, make sure the "Defines Module" setting in the "Build Settings" (subsection "Packaging") is set to 'yes' as this greatly simplifies the import in another project.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment