How to Marry Xcode and Swift Package Manager

Serg Dort
3 min readMar 4, 2016

--

Updated:

Now in order to “Marry Xcode and SPM” just follow this instructions. Have fun!

Old post:

Lately the are lot of talks going on regarding server side development using swift.

And there are some cool libraries on github already like: Kitura , Vapor

Of course they use Swift Package Manager to manage dependendencies in order to be able to build the app on Linux.

That’s why you as developer have to literally write the code in pure text editor. Sure, we all know and love Sublime, but since I am beginner in server side development, I want to be able to debug my bugs :) and to have handy code completions and continuous rebuilding to show errors.

Fortunately in the post of @NatashaTheRobot I found reference to this article but looks like there is some changes in latest release of SPM and that setup didn’t work for me

So with help of that article and lots of tries I’ve eventually managed to run my project in Xcode

Here are the steps:

  1. Setup Package.swift file like this . In my example I going to use Vapor.

2. Create “Public” and “Resources” directories in the root of the project directory, there going to be your CSS and .html files

3. Create empty Xcode project:

And put .xcodeproj file in the root directory

4. Add search import paths for Modules:

Great! Now Xcode sees our imports (don’t forget to add main.swift a to your target)

5. Add dependencies to the target they are in the (.build/debug/) press (command + shift + .) to display hidden files.

6. Make sure you’ve selected latest toolchain in the Xcode 7.3 beta settings

7. Add run scripts

These two will copy your Resources and Public folder near the executable

This one fixes the bug with SPM (removes tests folder from dependency)

And the last one will run swift build from your Project directory

Then make sure that your scripts will run before everything else

And now hit the precious run button=) and in your browser visit

http://localhost:8080

You can put some break point to make sure everything works

Any suggestions and improvements are welcome!

And in future, when you will add our Controllers or Middleware make sure that they are in the Sources folder in order to work with SPM

Here is the link the test project

Happy SwiftServer coding :)

--

--