Scaffold is a tool for generating code from Stencil templates, similar to rails gen.

Related tags

Utility scaffold
Overview

🏭 Scaffold

Swift Version

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

  1. Make some templates (The Stencil documentation is very helpful)
  2. Define templates and groups in a .scaffold.yml file (see Configuration)
  3. 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