Skip to content
Meta-programming for Swift, stop writing boilerplate code.
Swift JavaScript Ruby Other
Branch: master
Clone or download

Latest commit

erichoracek Nest ProtocolCompositions docs under Types category (#836)
* Nest ProtocolCompositions docs under Types category

* Regenerate docs
Latest commit 4a6b73f May 18, 2020


Type Name Latest commit message Commit time
Failed to load latest commit information.
.bundle Cache base path configurable via yaml file (#612) May 7, 2018
.circleci Support building on Xcode 11-4-1 Apr 28, 2020
Pods Fix cocoapods setup (#776) Jul 24, 2019
Resources docs: update readme Dec 25, 2016
Scripts Replaced bash script with a swift script Oct 10, 2018
Sourcery-Example Fix running sourcery script (#389) Aug 2, 2017
Sourcery.xcodeproj docs: update metadata for 0.18.0 release Apr 28, 2020
Sourcery.xcworkspace Update project to run with Xcode 10.2 (#760) May 25, 2019
Sourcery Add support for accessing all typealiases, parse global typealiases (#… Mar 30, 2020
SourceryFramework Disallow protocol compositions from being considered as the rawType o… May 4, 2020
SourceryJS Allow using JS templates with SPM (#508) Feb 6, 2018
SourceryRuntime Add missing documentation for the `ProtocolComposition` type (#833) May 4, 2020
SourcerySwift Add missing documentation for the `ProtocolComposition` type (#833) May 4, 2020
SourceryTests Disallow protocol compositions from being considered as the rawType o… May 4, 2020
SourceryUtils docs: update metadata for 0.18.0 release Apr 28, 2020
Sources Fix SwiftPM build on case-sensitive file systems (#791) Oct 22, 2019
Templates Improve AutoMockable.stencil for functions (#802) Jan 10, 2020
TryCatch Move SwiftTryCatch into this repo directly (#737) Apr 27, 2019
bin Fix the creation of a symbolic link to bin/sourcery (#671) Sep 5, 2018
docs Nest ProtocolCompositions docs under Types category (#836) May 18, 2020
guides typo fix in documentation (#749) Apr 17, 2019
.codecov.yml chore: disable code generated code from codecov Dec 23, 2016
.gitignore Git-ignored IDEA based IDEs configurations. (#569) Mar 31, 2018
.gitmodules removed submodule Mar 25, 2017
.jazzy.yaml Nest ProtocolCompositions docs under Types category (#836) May 18, 2020
.ruby-version Support building on Xcode 11-4-1 Apr 28, 2020
.sourcery.yml Fixed invalid frameworks path when compiling swift template (#311) May 21, 2017
.swift-version Sync .swift-version as 5.0 (#782) Sep 16, 2019
.swiftlint.yml included SourceryRuntime in linting May 28, 2017 generated jazzy docs Apr 11, 2017 Disallow protocol compositions from being considered as the rawType o… May 4, 2020 chore: initial commit Dec 11, 2016 Stop pre-numbering things. (#449) Dec 4, 2017
Dangerfile [danger] Add changelog update requirement when changing templates (#484) Jan 15, 2018
Gemfile Update cocoapods gem to 1.6.1 (#755) Apr 29, 2019
Gemfile.lock Update docs Apr 28, 2020
LICENSE Initial commit Dec 11, 2016
Package.resolved Fix cocoapods setup (#776) Jul 24, 2019
Package.swift Fixed missing SourceryRuntime dependency of SourceryFramework (SPM) (#… Mar 29, 2020
Podfile Fix cocoapods setup (#776) Jul 24, 2019
Podfile.lock Fix cocoapods setup (#776) Jul 24, 2019 Allows using Swift templates with SPM-packaged builds (e.g. via Mint) ( Mar 4, 2019 Release to homebrew rake task; (#379) Sep 28, 2017
Rakefile Fix Cache issue (#775) Jul 18, 2019
Sourcery.podspec docs: update metadata for 0.18.0 release Apr 28, 2020

CircleCI codecov docs Version License Platform

Sourcery is a code generator for Swift language, built on top of Apple's own SourceKit. It extends the language abstractions to allow you to generate boilerplate code automatically.

It's used in over 30,000 projects on both iOS and macOS and it powers some of the most popular and critically-acclaimed apps you have used. Its massive community adoption was one of the factors that pushed Apple to implement derived Equality and automatic Codable conformance. Sourcery is maintained by a growing community of contributors.

Try Sourcery for your next project or add it to an existing one -- you'll save a lot of time and be happy you did!


Sourcery allows you to get rid of repetitive tasks. An example might be implementing Equatable, without Sourcery you need to implement stuff like this:

extension Person: Equatable {
    static func ==(lhs: Person, rhs: Person) -> Bool {
        guard lhs.firstName == rhs.firstName else { return false }
        guard lhs.lastName == rhs.lastName else { return false }
        guard lhs.birthDate == rhs.birthDate else { return false }
        return true

This is trivial code but imagine doing this across ten types. Across fifty. How many structs and classes are in your project?

Sourcery removes the need to write this code. And if you refactor or add properties, the equality code will be automatically updated for you, eliminating possible human errors.

Sourcery automation can be applied to many more domains, e.g.

  • Equality & Hashing
  • Enum cases & Counts
  • Lenses
  • Mocks & Stubs
  • LinuxMain
  • Decorators
  • JSON coding
  • NSCoding and Codable

It's trivial to write new templates to remove boilerplate that is specific to your projects.

How To Get Started

There are plenty of tutorials for different uses of Sourcery, and you can always ask for help in our Swift Forum Category.


  • Binary form

    Download the latest release with the prebuilt binary from release tab. Unzip the archive into the desired destination and run bin/sourcery

  • Homebrew

    brew install sourcery

  • CocoaPods

    Add pod 'Sourcery' to your Podfile and run pod update Sourcery. This will download the latest release binary and will put it in your project's CocoaPods path so you will run it with $PODS_ROOT/Sourcery/bin/sourcery

  • Mint

    Run mint run krzysztofzablocki/Sourcery.

  • Building from source

    Download the latest release source code from the release tab or clone the repository and build Sourcery manually.

    • Building with Swift Package Manager

      Run swift build -c release in the root folder. This will create a .build/release folder and will put the binary there. Move the whole .build/release folder to your desired destination and run with path_to_release_folder/sourcery

      Note: JS templates are not supported when building with SPM yet.

    • Building with Xcode

      Open Sourcery.xcworkspace and build with Sourcery-Release scheme. This will create in the Derived Data folder. You can copy it to your desired destination and run with path_to_sourcery_app/


Full documentation for the latest release is available here.


Sourcery is a command line tool; you can either run it manually or in a custom build phase using the following command:

$ ./sourcery --sources <sources path> --templates <templates path> --output <output path>

Note: this command differs depending on how you installed Sourcery (see Installing)

Command line options

  • --sources - Path to a source swift files or directories. You can provide multiple paths using multiple --sources option.
  • --templates - Path to templates. File or Directory. You can provide multiple paths using multiple --templates options.
  • --force-parse - File extensions of Sourcery generated file you want to parse. You can provide multiple extension using multiple --force-parse options. (i.e. file.toparse.swift will be parsed even if generated by Sourcery if --force-parse toparse). Useful when trying to implement a multiple phases generation.
  • --output [default: current path] - Path to output. File or Directory.
  • --config [default: current path] - Path to config file. File or Directory. See Configuration file.
  • --args - Additional arguments to pass to templates. Each argument can have an explicit value or will have implicit true value. Arguments should be separated with , without spaces (i.e. --args arg1=value,arg2). Arguments are accessible in templates via
  • --watch [default: false] - Watch both code and template folders for changes and regenerate automatically.
  • --verbose [default: false] - Turn on verbose logging
  • --quiet [default: false] - Turn off any logging, only emit errors
  • --disableCache [default: false] - Turn off caching of parsed data
  • --prune [default: false] - Prune empty generated files
  • --version - Display the current version of Sourcery
  • --help - Display help information

Configuration file

Instead of CLI arguments you can use a .sourcery.yml configuration file:

  - <sources path>
  - <sources path>
  - <templates path>
  - <templates path>
  - <string value>
  - <string value>
  <output path>
  <name>: <value>

Read more about this configuration file here.


Contributions to Sourcery are welcomed and encouraged!

It is easy to get involved. Please see the Contributing guide for more details.

A list of contributors is available through GitHub.

To clarify what is expected of our community, Sourcery has adopted the code of conduct defined by the Contributor Covenant. This document is used across many open source communities, and articulates my values well. For more, see the Code of Conduct.


Sourcery is available under the MIT license. See LICENSE for more information.


This tool is powered by

Thank you! to:

  • Mariusz Ostrowski for creating the logo.
  • Artsy Eidolon team, because we use their codebase as a stub data for performance testing the parser.
  • Olivier Halligon for showing me his setup scripts for CLI tools which are powering our rakefile.

Other Libraries / Tools

If you want to generate code for asset related data like .xib, .storyboards etc. use SwiftGen. SwiftGen and Sourcery are complementary tools.

Make sure to check my other libraries and tools, especially:

  • KZPlayground - Powerful playgrounds for Swift and Objective-C
  • KZFileWatchers - Daemon for observing local and remote file changes, used for building other developer tools (Sourcery uses it)

You can follow me on Twitter for news/updates about other projects I am creating.

You can’t perform that action at this time.