Sourcery in iOS

Konstantinos Nikoloutsos
Dev Genius
Published in
3 min readDec 8, 2021

--

Ready to be a sourcerer? 🪄

Sourcery is a code generator for Swift language that is guaranteed to save you time. It’s used in over 40,000 projects on both iOS and macOS and it powers some of the most popular and critically-acclaimed apps you have used (including Airbnb, Bumble, New York Times)

Use-case:

With a little magic we converted this:

into this 🪄:

We all know how important mocks are when unit testing.
As developers we don’t want to spend our time writing boilerplate code.
In the above case sourcery was instructed to create a mock implementation for a protocol. But you can do more than that.

Learn how to use Sourcery

Fun fact: You can understand and setup Sourcery faster than watching a harry potter Movie. 🍿

1. Download Sourcery

We’re going to download Sourcery with cocoapods.

Add pod 'Sourcery' to your Podfile and then pod install

Remember: Sourcery is not a dependency in your project source code it will not be shipped into the final archived app. It’s like swiftlint. So Cocoapods will only downoad the executable for us and then we are going to use. Without it we cannot do magic 🪄

Sourcery executable Cocoapods downloaded for us.

2. Use Sourcery

Now that we have the tool for doing magic (executable) we should know how to run it.

Pods/Sourcery/bin/sourcery --sources "PROJECT_NAME/Source" \--templates "PROJECT_NAME/TemplateMagic" \--output "PROJECT_NAME/Source"
  • Sources → The input files that Sourcery is to read in order to create other files. (e.g in above example FooProtocol.swift is inside PROJECT_NAME/Source)
  • Templates → Are instructions for Sourcery to know for which classes/enums to create code and what code.
  • Output → Is the folder that sourcery will save the generated files.

Tip: If you want the Sourcery to run in every build you can add it in buildphases.

Sourcery added in build phase

3. Learn how to write your own templates (spells)

Stencil is the most popular template language to write your custom templates.
Let’s now create our own custom template so that we make Harry Potter ⚡️ proud:)

Tip: What is between {% and %} is stencil template code.

Tip: First for iterates all protocols with annotation/comment MyFirstSpell

Every magician starts with simple spells for tranforming objects into frogs 🐸

Now let’s run 🪄

Observation: The first line comment is nessecary otherwise we wouldn’t have output (no frog functions) .

And after the spell:

Generated file.

You did it 🎉. Kudos to you. You’re officially a new magician. I expect to see more spells from you.

Learn more about how to write even more powerful spells here:
https://www.youtube.com/watch?v=GNmLVq14LC8

If you found this article helpful don’t forget to leave some claps 👏, you help a fellow magician to continue writing articles like this!

And and I almost forgot..Here are some spells I use all the time and want to share it with you.

AutoMockable

AutoEquatable:

Tranform enum to conform into equatable protocol.

More content at blog.devgenius.io.

--

--