π
Scaffold
Scaffold is a tool for generating code from Stencil templates, similar to rails gen. It happens to be written in Swift, but it can output source files for any language.
Here's how it works:
$ scaffold --template ViewModel --name Search # Output one specific template
π Rendering templates...
π¨ SearchViewModel.swift created at Sources/Search/SearchViewModel.swift
β
Complete
$ scaffold --group Feature --name Search # Output multiple templates at the same time
π Rendering templates...
π¨ SearchViewController.swift created at Sources/Search/SearchViewController.swift
π¨ SearchViewModel.swift created at Sources/Search/SearchViewModel.swift
π¨ SearchRouter.swift created at Sources/Search/SearchRouter.swift
π¨ SearchViewModelTests.swift created at Tests/ViewModels/SearchViewModelTests.swift
β
Complete
Templates are written in a language called Stencil:
// {{ name }}ViewModel.swift
import Foundation
import RxSwift
import RxRelay
protocol {{ name }}ViewModelInputs {
func fooButtonDidTap()
}
protocol {{ name }}ViewModelOutputs: AutoTestableOutputs {
var fooButtonIsEnabled: Driver<Bool> { get }
var shouldHideKeyboard: Signal<Bool> { get }
}
protocol {{ name }}ViewModelType {
var inputs: {{ name }}ViewModelInputs { get }
var outputs: {{ name }}ViewModelOutputs { get }
}
final class {{ name }}ViewModel: {{ name }}ViewModelType, {{ name }}ViewModelOutputs {
//...
Also, you can use extensions to Stencil defined in StencilSwiftKit.
Installing
Homebrew
$ brew tap yhkaplan/scaffold https://github.com/yhkaplan/scaffold.git
$ brew install scaffold
Manually
$ git clone [email protected]:yhkaplan/scaffold.git
$ cd scaffold
$ make install
Usage
Setup
- Make some templates (The Stencil documentation is very helpful)
- Define templates and groups in a
.scaffold.yml
file (see Configuration) - You're ready to go!
If you want to pass in complex values from the your local development environment, a Makefile like below can be useful.
SCAFFOLD_CONTEXT="name=$(name),user=$$(git config user.name),date=$$(date -u +"%Y/%m/%d")"
feature:
scaffold --group Feature --context $(SCAFFOLD_CONTEXT)
template:
scaffold --template $(template) --context $(SCAFFOLD_CONTEXT)
To use it, just call make template template=View name=Hoge
or make feature name=Search
Configuration
Config file
templates: # Required (array of Templates)
- name: ViewController # Required (string)
templatePath: Templates/ # Required (string)
fileName: "{{ name }}ViewController.swift" # Required (string)
outputPath: Sources/Controllers/ # Optional (string)
- name: ViewModel
templatePath: Templates/
fileName: "{{ name }}ViewModel.swift"
outputPath: Sources/ViewModels/
groups: # Optional (array of TemplateGroups)
- name: Feature # Required (string)
templateNames: # Required (array of template names as strings)
- ViewController
- ViewModel
outputPath: # Optional (string)
Command line options
OPTIONS:
-d, --dry-run Print the output without writing the file(s) to disk.
Default is false.
-o, --output-path
Path to output folder(s).
-t, --template
Single template or comma-separated list of templates
to generate from the config file
-g, --group Group from config file with list of templates
--config-file-path
Path to config file. Default is .scaffold.yml
-n, --name Value to pass to the name variable in the stencil
template
-c, --context String with context values to pass to template
(overrides name).
-C, --context-file-path
Path to JSON file with context values to pass to
template (overrides name)
-h, --help Show help information.
Alternatives
Xcode's templates
Articles like those below discuss how to make custom templates for Xcode, but the biggest issues with custom Xcode templates are as below. That said, they may be enough for you.
- The format can change with any new version of Xcode, breaking your templates
- There's a limited range of variables and customizability available
- Sharing templates with team members involves awkward scripts that symlink to the correct folder
- They can't be used from the command line (more of a personal preference)
- How to create a custom Xcode template for coordinators
- Streamlining your development workflow with Xcode Templates
Genesis
Genesis also uses Stencil for templating, but it seems more focused on new projects and interactive commands whereas Scaffold is more focused on continual use.
Kuri
Kuri does not use Stencil for templating, instead choosing an Xcode-like DSL. This is enough for many use-cases, but I wanted something with the flexibility of Stencil where I could feed in any kind of information I want into the templates. Also, Kuri has a nice feature where it can add generated files to Xcode automatically. I may add this feature in later, but I am still considering whether I should.
Credits/Inspiration
- Stencil for a delightful templating language
- StencilSwiftKit for a powerful Stencil library
- Sourcery for its deeply thought-out example of StencilSwiftKit
- Yams for effortless yml parsing
- PathKit for easy-to-use path operations
- ArgumentParser for Swifty argument-parsing
- Genesis for inspiration on balancing config and template files
- Kuri for inpiration of how the CLI should work and config file should look
- pointfree.co and their episodes on Parser combinators, a very direct inspiration for the Parser target
License
Licensed under MIT license. See LICENSE for more info.