This document describes how to make your own templates for SwiftGen, so it can generate code more to your liking and code following your own coding conventions.
When you invoke SwiftGen, you can specify templates by name or by path.
If you use the --templatePath
(or -p
) option, you'll need to specify the full path to the template you want to use. This allows you to store your templates anywhere you want and name them anyhow you want, but can become quite annoying to type.
When you use the --template
(or -t
) option, you only specify a template name. SwiftGen then searches a matching template using the following rules (where <subcommand>
is one of colors
, images
, storyboards
or strings
depending on the subcommand you invoke):
- It searches for a file named
<subcommand>-<name>.stencil
in~/Library/Application Support/SwiftGen/templates
, which is supposed to contain your own custom templates - If it does not find one, it searches for a file named
<subcommand>-<name>.stencil
in<installdir>/share/swiftgen/templates
which contains the templates bundled with SwiftGen.
For example swiftgen images -t foo DIR
will search for a template named images-foo.stencil
in Application Support, then in /usr/local/share/SwiftGen/templates
(assuming you installed SwiftGen using Homebrew in /usr/local
)
If you don't specify neither -t
nor -p
, SwiftGen will assume you specified -t default
.
That means that swiftgen images DIR
will use the images-default.stencil
template.
Stencil comes with a default template for each of its subcommand, but given this rule, this means that you have the ability to make your own default template for each subcommand and store them in Application Support
named like images-default.stencil
, etc.
The swiftgen templates
command will list all the installed templates (as YAML output) for each subcommand, both for bundled templates and custom templates.
$ swiftgen templates
colors:
custom:
bundled:
- default
- rawValue
images:
custom:
bundled:
- allvalues
- default
storyboards:
custom:
- mytemplate
bundled:
- default
- lowercase
- swift3
strings:
custom:
- default
bundled:
- default
- swift3
Templates in SwiftGen are based on Stencil, a template engine inspired by Django and Mustache. The syntax of the templates is explained in Stencil's documentation.
Additionally to the tags and filters provided by Stencil, SwiftGen provides some additional ones, documented in the StencilSwiftKit framework.
When SwiftGen generates your code, it provides a context (a dictionary) with the variables containing what images/colors/strings/… the subcommand did detect, to render your Stencil template using those variables.
Below are the variables in the context generated by each SwiftGen subcommand. They are also visible as comments in the source code here.
enumName
:String
— name of the enum to generatecolors
:Array
of:name
:String
— name of each colorrgb
:String
— hex value of the form RRGGBB (like "ff6600")rgba
:String
— hex value of the form RRGGBBAA (like "ff6600cc")red
:String
— hex value of the red componentgreen
:String
— hex value of the green componentblue
:String
— hex value of the blue componentalpha
:String
— hex value of the alpha component
enumName
:String
— name of the enum to generateimages
:Array<String>
— list of image names
sceneEnumName
:String
segueEnumName
:String
storyboards
:Array
of:name
:String
initialScene
:Dictionary
(absent if not specified)customClass
:String
(absent if generic UIViewController/NSViewController)isBaseViewController
:Bool
, indicate if the baseType is 'viewController' or anything elsebaseType
:String
(absent if class is a custom class). The base class type on which the initial scene is base. Possible values include 'ViewController', 'NavigationController', 'TableViewController'…
scenes
:Array
(absent if empty)identifier
:String
customClass
:String
(absent if generic UIViewController/NSViewController)isBaseViewController
:Bool
, indicate if the baseType is 'ViewController' or anything elsebaseType
:String
(absent if class is a custom class). The base class type on which a scene is base. Possible values include 'ViewController', 'NavigationController', 'TableViewController'…
segues
:Array
(absent if empty)identifier
:String
class
:String
(absent if generic UIStoryboardSegue)
enumName
:String
tableName
:String
- name of the.strings
file (usually"Localizable"
)strings
:Array
key
:String
translation
:String
params
:Dictionary
— defined only if localized string has parameters; contains the following entries:count
:Int
— number of parameterstypes
:Array<String>
containing types like"String"
,"Int"
, etcdeclarations
:Array<String>
containing declarations like"let p0"
,"let p1"
, etcnames
:Array<String>
containing parameter names like"p0"
,"p1"
, etctypednames
: Arraycontaining typed declarations like
"let p0: String",
"let p1: Int"`, etc
keytail
:String
containing the rest of the key after the next first.
(useful to do recursion when splitting keys against.
for structured templates)
structuredStrings
:Dictionary
- contains strings structured by keys separated by '.' syntax