← iOS

Sourcery

Overview

Sourcery is a meta-programming library for Swift. It analyzes Swift source code, exposes that analysis to a template engine, and then renders a code result.

Installing Sourcery

brew install sourcery

Integration

Stencil is a template language for Swift. The template directory is Banno/Templates. Sourcery compiles these templates into Swift. The output location is Banno/Banno/Gen.

The autogenerated source must be recreated and committed. This can be done automatically with a pre-commit hook. You can install the pre-commit hook by running ln -s ../../scripts/render_stencil.precommit.hook .git/hooks/pre-commit from the project’s root directory. This will render the template prior to each commit, and add the results to the index being committed.

Developing Templates

Sourcery can watch template and source directories, re-rendering when they update. A variant of the run script called ./scripts/develop_stencil.sh has this flag enabled. To use it, call the script with two arguments.

  1. Path to the stencil file being developed
  2. Output location (will be constantly kept updated)

For example: ./scripts/develop_stencil.sh Banno/Templates/ThemeRegistrations.stencil ~/Desktop/test.swift