---
title: "#292: Metaprogramming with Sourcery 🔮"
url: https://littlebitesofcocoa.com/292-metaprogramming-with-sourcery
author: Jake Marsh
date: 2017-01-20
published: 2017-01-20
topics: ["Libraries","Tools"]
description: "We cover plenty of libraries and developer tools here on LBOC. Many are useful not just on their surface, but also in terms of how they allow us to learn..."
---

# #292: Metaprogramming with Sourcery 🔮

By Jake Marsh, January 20, 2017. HTML: https://littlebitesofcocoa.com/292-metaprogramming-with-sourcery

We cover plenty of libraries and developer tools here on _LBOC_. Many are useful not just on their surface, but also in terms of how they allow us to <i>learn</i> from our fellow developers.

Through this process, we collectively explore new approaches and techniques. New ideas emerge all the time.

Every now and then, one of these ideas stands out.

Sometimes, an idea makes too much sense, or is simply too useful to ignore.

In modern times, things like [Fastlane](https://fastlane.tools/), [CocoaPods](https://cocoapods.org/), and [Carthage](https://github.com/Carthage/Carthage) come to mind. Slightly more seasoned folks may remember the emergence of [Pull to Refresh](https://en.wikipedia.org/wiki/Pull-to-refresh), or [BWToolkit](http://brandonwalkin.com/bwtoolkit/).

[Sourcery](https://github.com/krzysztofzablocki/Sourcery) from [Krzysztof Zabłocki](https://github.com/krzysztofzablocki) is the latest addition to this set.

It brings the concept of "[meta-programming](https://en.wikipedia.org/wiki/Metaprogramming)" to Swift, and it is definitely too useful to ignore.

Let's peer into our crystal ball, and see what it can do.

At its core, [Sourcery](https://github.com/krzysztofzablocki/Sourcery) generates **Swift** code from **template files**.

It elegantly brings together two other great developer tools: [SourceKitten](https://github.com/jpsim/SourceKitten) (for introspecting our code), and [Stencil](https://github.com/kylef/Stencil) (for templates).

It aims to solve a few problems:

- Reduce time spent writing so-called "boilerplate", or repetitive/obvious code.
- Provide a way to reason about the types in our code, and their properties.
- Reduce simple human errors, caused by things like typos.

Ok, enough introduction. Here's how [Sourcery](https://github.com/krzysztofzablocki/Sourcery) works:

- First, we'll write some code that looks <i>almost</i> like regular **Swift** code into "template" (`.stencil`) files.
- Then, we'll run the `sourcery` command-line tool. This will "render" our `.stencil` files into `.swift` files that we'll add to our project.
- Finally, when we build our app, our "generated" `.swift` files will get compiled just like any other `.swift` files we might add.

Immediately some ideas of how to use this begin coming to mind. Here's a few specific tasks that might cause us to reach for [Sourcery](https://github.com/krzysztofzablocki/Sourcery):

- Conforming our types to `NSCoding`.
- Conforming to `Equatable` or `Hashable`
- Writing JSON de-serialization code

Maintaining each of these implementations is a never-ending task. Anytime we add, remove, or change a property we'll need to potentially revist each of these bits of code as well. Bummer.

Ok. Let's try this thing.

First we'll need to get the `sourcery` command-line tool. The simplest way to do this is to [download the latest release binary here](https://github.com/krzysztofzablocki/Sourcery/releases).

Let's `cd` into the root directory of the download and copy the tool over to somewhere permanent:

``` bash
cp bin/sourcery /usr/local/bin
```

(Note: /usr/local/bin is a common place to put command line tools on **macOS** thanks largely to the fact that [Homebrew](http://brew.sh/) puts things there, so it's likely already in our `$PATH`).

Neat. Now we can use it anywhere.

We could also have simply copied the tool into our project, and added it to source control. Any approach is fine, we just need to be able to run it in the root directory of our project somehow.

Now, let's head into that root directory of our project, and create a couple directories:

``` bash
mkdir templates
mkdir generated
```

We're almost ready to try things out. First though, we'll need a template to generate from.

Let's add a new file in the `templates` directory called `Enum+Count.stencil`. Then, we'll write our first template code:

``` swift
{% for enum in types.enums %}
extension {{ enum.name }} {
  static var count: Int { return {{ enum.cases.count }} }
}
{% endfor %}
```

The `{{`'s, `}}`'s, `{%`'s, and `%}`'s are [Stencil](https://github.com/kylef/Stencil) template tags.

[Stencil](https://github.com/kylef/Stencil) deserves a full _Bite_ of it's own, but for now we just need to know that statements within these tags get evaluated by the `sourcery` command line tool, and iterated or replaced when generating **Swift** code.

The rest of the content is regular **Swift** code.

Anyone who has worked on a web app in recent years should feel right at home with this technique. Instead of generating HTML though, we're generating **Swift** code, neat!

Let's break down what's happening in our template:

First, we want to iterate through all the `enums` in our project's code:

``` swift
{% for enum in types.enums %}

{% endfor %}
```

Then, for each `enum` we find, we want to extend it to have a new `static` property called `count`.

``` swift
extension {{ enum.name }} {
  static var count: Int { return {{ enum.cases.count }} }
}
```

This property will return the number of `cases` in the `enum`. (Providing us a piece of functionality currently missing from Swift itself).

Finally, we can run `sourcery`.

``` bash
./sourcery . templates generated --watch
```

We've passed in the `--watch` flag to make `sourcery` watch our template files, and re-generate them anytime it sees a change. Neat.

This will scan our source code for a bit, then produce a new file in the `generated` directory called `Enum+Count.generated.swift`.

It will look like this:

``` swift
extension SpaceshipKind {
  static var count: Int { return 37 }
}

extension CrewRank {
  static var count: Int { return 10 }
}

extension HTTP.Method {
  static var count: Int { return 7 }
}
```

<img src="https://media.littlebitesofcocoa.com/292-1.jpg" style="max-width: 500px; border-radius: 4px;" />

<i>How cool is that?</i>

Now, we just need to add this generated file to our Xcode project like we would any other file. Its contents will be replaced anytime `sourcery` runs.

**Pro Tip:** We can also optionally add a new <i>"Run Script..."</i> Build Phase to our Xcode project to run the `sourcery` command (without `--watch` of course) at the beginning of each build of our app. Very cool.

The [Sourcery](https://github.com/krzysztofzablocki/Sourcery) Github repo offers a some very useful example templates for adding things like [Equatable](https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/AutoEquatable.stencil) and [Hashable](https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/AutoHashable.stencil). These examples are a great way to learn more about what's possible.

We've of course only barely scratched the surface of what's possible with [Sourcery](https://github.com/krzysztofzablocki/Sourcery). Look out for future Bites where we'll explore much more...

<img src="https://media.littlebitesofcocoa.com/292-2.gif" style="max-width: 600px; border-radius: 4px;" />

Learn more and find full documentation of [Sourcery](https://github.com/krzysztofzablocki/Sourcery) at [git.io/sourcery](https://git.io/sourcery)

<p></p>
