Sourcery

 

Today we are going to look at the library Sourcery from Krzysztof Zabłocki.

Sourcery is a tool that brings meta-programming to Swift, allowing you to code generate Swift code.

In the words of the author:

Sourcery scans your source code, applies your personal templates and generates Swift code for you, allowing you to use meta-programming techniques to save time and decrease potential mistakes.”

Why and when should we use it? Every time we need to write boilerplate code in Swift, this includes writing code to conform to the equatable and hashable protocol, adding support to NSCoding, or when implementing JSON serialization.

For this spike we created a example project available at our GitHub.

If you are using Cocoapods to use Sourcery in your project add it as a dependency in your Podfile as:

pod 'Sourcery'

Then you need to create two folders, one for the templates and another for the generated code, we called them “Templates” and “CodeGenerated”. Attention, don’t create them as Groups in Xcode, this will not create the folder in your file system that is needed for the library to work.

After you need to include a “Run script phase” before the “Compile Sources” step in the “Build Phases” of the project.

For this particular project the scripts if the following:

"${PODS_ROOT}/Sourcery/bin/sourcery" "${SRCROOT}/SourcerySpike" "${SRCROOT}/Templates" "${SRCROOT}/CodeGenerated"

Simplified it means:

./sourcery

After the first time that the script runs you have to add the generated swift file (.generated.swift) to the target you want to use.

After this initial configuration we are ready to start using the library.

For writing the templates we need to use Stencil, a template language for Swift created by Kyle Fuller.

On this spike we implemented the use cases in the read me, so we created templates to:

  • know how many elements are in each enum
  • generate Equality for types

From this quick spike it’s possible to understand the power of Sourcery and how much time (and mental health) it can save you.

The drawbacks we perceived is the learning curve to use Stencil in order to create templates, however it seems an investment that pays for itself. There is also a built-in daemon to make writing templates easier, you can write the templates and see the generated code updating.

What we would recommend is to introduce Sourcery in your project and start using it for simple things like equality and other utilities, and as you find yourself writing repetitive code try to learn the minimum in order to replace that with a template.

Another complementary and useful tool is SwiftGen. You can use it to generate code for asset related data like .xib, .storyboards etc. This tool is made by AliSoftware the creator of OHHTTPStubs.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s