DEV.co
AI Coding Agents · krzysztofzablocki

Sourcery

Sourcery is a Swift code generator that automates boilerplate code generation using templates and AST analysis. It reduces repetitive coding tasks like creating mocks, equality implementations, and codable conformances by orders of magnitude.

Source: GitHub — github.com/krzysztofzablocki/Sourcery
8k
GitHub stars
635
Forks
Swift
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorykrzysztofzablocki/Sourcery
Ownerkrzysztofzablocki
Primary languageSwift
LicenseMIT — OSI-approved
Stars8k
Forks635
Open issues94
Latest release2.3.0 (2025-09-18)
Last updated2026-06-11
Sourcehttps://github.com/krzysztofzablocki/Sourcery

What Sourcery is

Built on Apple's SwiftSyntax, Sourcery parses Swift source code into an AST and applies Stencil-based templates to generate new code. It supports both Stencil and JavaScript templates, runs as a CLI tool or Xcode build phase, and integrates with SwiftPM as a plugin.

Quickstart

Get the Sourcery source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/krzysztofzablocki/Sourcery.gitcd Sourcery# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Mock and stub generation for testing

Automatically generate comprehensive mocks for all protocols with call tracking, argument capture, and closure support—eliminating hundreds of lines of manual mock code per protocol.

Equality, Hashing, and Codable conformances

Generate Equatable, Hashable, and advanced Codable implementations across types. Keeps implementations consistent and auto-updates when properties change.

Type-safe boilerplate patterns

Generate lenses, decorators, enum case utilities, diffable implementations, and property-level diffing. Any algorithmic pattern you can describe can be templated.

Implementation considerations

  • Templates use Stencil syntax; team must learn Stencil idioms and AST navigation. Documentation is extensive but learning curve exists.
  • Generated code should be committed or regenerated in CI/CD. Define a clear workflow: pre-commit hook, build phase, or CI step to avoid divergence.
  • Sourcery Pro (Mac App Store) offers live Stencil editing and Xcode integration; open-source CLI is free but lacks IDE feedback.
  • Template debugging requires understanding the AST structure Sourcery exposes. Errors in templates can silently produce incorrect or missing generated code.
  • Version control: decide whether to commit generated files or regenerate on clone. Committing reduces friction; regenerating keeps repo lean.

When to avoid it — and what to weigh

  • JavaScript template support is critical — The README notes JS templates are not supported when building with SPM. If your workflow requires JS templates and SPM distribution, this is a blocker.
  • Zero build-time overhead is non-negotiable — Code generation adds a build step. If your project has strict build-time constraints or very frequent incremental builds, the added overhead may be problematic.
  • Swift version pinning requirements — Sourcery parses using SwiftSyntax, which is tied to specific Swift versions. Rapid major Swift version migrations may create compatibility friction.
  • Heavy reliance on runtime reflection — If your architecture depends on runtime inspection rather than compile-time code generation, Sourcery's static approach may not align.

License & commercial use

MIT License. Permissive OSI-approved license allowing commercial use, modification, and distribution with no restriction other than license/copyright notice preservation.

MIT is a permissive open-source license. Commercial use is permitted without license fee or approval. However, confirm your internal policy on third-party OSS dependencies and ensure your usage complies with your organization's open-source guidelines.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

Sourcery is a code generation tool running locally during build time. Security posture depends on: (1) template validation—ensure custom templates are reviewed, as they have broad AST access; (2) source code access—runs on your source tree, no external data transmission claimed; (3) SwiftSyntax dependency—regularly updated by Apple. No known vulnerabilities reported in available data. As with any build tool, audit templates from third parties before use.

Alternatives to consider

Swift macros (Swift 5.9+)

Apple's native compile-time macro system provides similar boilerplate elimination with language-level support. Newer projects may prefer macros for derived Equatable/Codable, though Sourcery remains more flexible for complex patterns.

Xcode code snippets + manual templates

Low-tech alternative using Xcode's built-in snippet system. No build-time overhead, but no automation; suited for simple, infrequent patterns.

GYB (Generate Your Boilerplate, part of Swift toolchain)

Lightweight, Python-based code generation baked into Swift stdlib. Minimal dependencies but less user-friendly and smaller community than Sourcery.

Software development agency

Build on Sourcery with DEV.co software developers

Sourcery automates code generation for mocks, equality, and domain-specific patterns. Get started with templates and integrate into your build pipeline—no license fee, MIT open source.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

Sourcery FAQ

Can we use Sourcery in a CI/CD pipeline without Xcode?
Yes. Sourcery runs as a standalone CLI and supports Linux via pre-built binaries or Swift toolchain build. Use in CI step to regenerate code or validate checked-in generated files.
Do we have to commit generated code to git?
Not required. Common patterns: (1) commit generated code for clarity in diffs/reviews, (2) regenerate in CI and .gitignore output, (3) regenerate on dev machine via pre-commit hook. Choose based on team preference and code review workflow.
Is there IDE support beyond terminal?
Sourcery Pro (paid, Mac App Store) adds live Stencil editor and Xcode integration. Open-source CLI lacks live preview but integrates via build phases and is fully scriptable.
How do we migrate if Swift macros become sufficient?
Sourcery templates and macros address overlapping but distinct use cases. Simple cases (Equatable, Codable) can move to macros; complex or domain-specific patterns remain Sourcery-friendly. No forced migration path; gradual adoption is feasible.

Work with a software development agency

DEV.co helps companies turn open-source tools like Sourcery into production software. Our software development services cover the full lifecycle — architecture, web development, integration, and maintenance — delivered by software developers and web developers who ship. Engage our software development agency to implement or customize it for your ai coding agents stack.

Reduce boilerplate and accelerate iOS/macOS development

Sourcery automates code generation for mocks, equality, and domain-specific patterns. Get started with templates and integrate into your build pipeline—no license fee, MIT open source.