Sourcery for Swift: Complete Guide to Code Generation
\n\n1. What is it and why is it needed?
\nSourcery is a powerful code generation tool for the Swift language, created by Krzysztof Zabłocki and his team. It allows you to automatically generate boilerplate code based on annotations and the structure of your source files. Instead of manually writing repetitive protocol implementations, Equatable, Hashable, Codable, mock objects for testing, or code for working with Auto Layout, you describe the generation rules once, and Sourcery does the rest.
\n\nWhy is this needed? In real-world Swift projects, a significant portion of the code is mechanical and repetitive. This not only wastes developer time but is also a source of errors: forgot to add a new field to Equatable — you get a bug. Sourcery solves this problem by handling the routine. It analyzes your code (AST — Abstract Syntax Tree) and, based on templates (Stencil or Swift-based), generates new files. This is especially useful in large projects with dozens or hundreds of models, ViewControllers, or services.
Sourcery is not a runtime library — it is a compile-time tool. You run it as part of the build process (via a Build Phase in Xcode or through SPM/Tuist), it generates files, and then those files are compiled together with the rest of the code. This approach guarantees zero runtime overhead and complete predictability.
\n\n2. Installation
\nThere are several ways to install Sourcery. The simplest and recommended method is via Homebrew:
\nbrew install sourcery\n\nFor projects using Swift Package Manager (SPM), you can add Sourcery as a plugin. However, it is most often used as a separate binary called from a build script. For installation via Mint (a package manager for Swift tools):
\nmint install krzysztofzablocki/Sourcery\n\nYou can also download a pre-built binary from GitHub Releases and place it in your project folder. After installation, make sure the command is available:
\nsourcery --version\n\n3. Quick Start — Minimal Working Example
\nLet's create a simple example: automatic generation of the Equatable protocol implementation for a struct.
Step 1: Create a source code file (e.g., Sources/Models/User.swift):
import Foundation\n\n// sourcery: AutoEquatable\nstruct User {\n let id: Int\n let name: String\n let email: String\n}\n\nNote the comment // sourcery: AutoEquatable — this is an annotation that tells Sourcery to generate an Equatable implementation for this struct.
Step 2: Create a template (e.g., Templates/AutoEquatable.stencil):
// Generated by Sourcery\n// swiftlint:disable all\n\n{% for type in types.all where type.annotations.AutoEquatable %}\nextension {{ type.name }}: Equatable {\n static func == (lhs: {{ type.name }}, rhs: {{ type.name }}) -> Bool {\n {% for variable in type.storedVariables %}\n {% if not forloop.first %} && {% endif %}lhs.{{ variable.name }} == rhs.{{ variable.name }}\n {% endfor %}\n }\n}\n{% endfor %}\n\nStep 3: Run Sourcery:
\nsourcery --sources Sources/Models --templates Templates --output Generated\n\nStep 4: Result — a file AutoEquatable.generated.swift will appear in the Generated folder:
// Generated by Sourcery\ Also in library
Rate limiting in Rust: governor, leaky-bucket libraries and middleware for web servers
Tonic for Rust: Complete Guide with Examples
Task Queues in C# (.NET): System.Threading.Channels and TPL Dataflow
validator for Go: Complete Guide with Examples
Machine Learning in Rust: Overview of Libraries for Data Science