Information about how to use and install Sourcery

What is Sourcery?

Sourcery is a code generator that helps boilerplate code creation.

It is recommended, however the installation of this library is optional. If you want to install, follow the instructions below.


After you have integrated Beagle, now you can intall Sourcery in your computer using Homebrew.

$ brew install sourcery


It is necessary to configure the path files template, the generated files and the path where Sourcery will scan the font code. Follow the next steps:

Step 1: in Build Phases, create a new Run Script Phase;

Step 2: put ABOVE Compile Sources; If you don’t do this, sourcery won’t be able to compile the generated code.

Step 3: paste the following code:

if which sourcery >/dev/null; then
sourcery --config $SRCROOT
echo "warning: Sourcery not installed, download using brew install sourcery"

This script warns Xcode to look for a .sourcery.yml file in your project root. It will have some properties that Sourcery will have to know to generate the code.

The list below shows the attributes used in this file:

  • sources : path for your swift files. It is the root of the project.
  • templates : path for your template files that Sourcery will use to generate the code.
  • output : o path for generates files.

.yml file configuration

The .yml file configuration are different due to the way Cocoapods and Carthage deals with the Beagle’s files.

For the Sourcery to use the Beagle’s templates, you must specify the SourceryProtocols.swif in sources and the template path in templates.

    - RootOfYourProject
    - Pods/BeagleUI/iOS/Sources/BeagleUI/CodeGeneration/SourceryProtocols.swift
  - Pods/BeagleUI/iOS/Sources/BeagleUI/CodeGeneration/Templates
    - RootOfYourProject
    - Carthage/Checkouts/beagle/iOS/Sources/BeagleUI/CodeGeneration/SourceryProtocols.swift
  - Carthage/Checkouts/beagle/iOS/Sources/BeagleUI/CodeGeneration/Templates


By default, custom widgets need to implementinit(from decoder: Decoder) throws. This can be automated with Sourcery, like the example below:

import UIKit
import BeagleUI

enum ComponentColorTypes: String, Decodable {
    case veryColorful
    case blackAndWhite

struct SomeComponent: Widget {
    var widgetProperties: WidgetProperties
    var property1: String
    var property2: Double?
    var colorPallete: ComponentColorTypes = .blackAndWhite
    func toView(context: BeagleContext, dependencies: RenderableDependencies) -> UIView {
        return UIView()

Without the metaprogramming, it is necessary to write the following code:

extension SomeComponent {
    enum CodingKeys: String, CodingKey {
        case property1
        case property2
        case colorPallete

    internal init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        widgetProperties = try WidgetProperties(from: decoder)
        property1 = try container.decode(String.self, forKey: .property1)
        property2 = try container.decodeIfPresent(Double.self, forKey: .property2)
        colorPallete = try container.decode(ComponentColorTypes.self, forKey: .colorPallete)

It is necessary with Sourcery, to do the structs in accordance with AutoDecodable and the same code will be generated

This same process works for all protocols and it is also possible to create templates and customized protocols.

Available protocols

Some protocols are already on Beagle. They are located in SourceryProtocols.swift. See the list below:

AutoEquatableImplements equatable
AutoDecodableImplements customized decodable, according to Beagle’s defaults. It only works for structs
AutoInitiableImplements customized init, according to Beagle’s defaults
AutoInitiableAndDecodableProtocols combination AutoInitiable and AutoDecodable

Last modified February 11, 2021: create content (#298) (43225e15)