Beagle v1.7 is no longer actively maintained. The documented version you are viewing may contain deprecated functionality. For up-to-date documentation, see the latest version .
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
unset SDKROOT
sourcery --config $SRCROOT
else
echo "warning: Sourcery not installed, download using brew install sourcery"
fi
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 configurationThe .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.
.yml
consider that Cartfile or Podfile file are in the same directory that .xcodeproj
or .xcworkspace
sources:
include:
- RootOfYourProject
- Pods/BeagleUI/iOS/Sources/BeagleUI/CodeGeneration/SourceryProtocols.swift
templates:
- Pods/BeagleUI/iOS/Sources/BeagleUI/CodeGeneration/Templates
output:
RootOfYourProject/CodeGeneration/Generated
sources:
include:
- RootOfYourProject
- Carthage/Checkouts/beagle/iOS/Sources/BeagleUI/CodeGeneration/SourceryProtocols.swift
templates:
- Carthage/Checkouts/beagle/iOS/Sources/BeagleUI/CodeGeneration/Templates
output:
RootOfYourProject/CodeGeneration/Generated
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.
Some protocols are already on Beagle. They are located in SourceryProtocols.swift.
See the list below:
Protocol | Use |
---|---|
AutoEquatable | Implements equatable |
AutoDecodable | Implements customized decodable, according to Beagle’s defaults. It only works for structs |
AutoInitiable | Implements customized init, according to Beagle’s defaults |
AutoInitiableAndDecodable | Protocols combination AutoInitiable and AutoDecodable |
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.