π
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 templatesArticles 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
LicenseLicensed under MIT license. See LICENSE for more info.
You might also like...
SwiftRegressor - A linear regression tool thatβs flexible and easy to use
SwiftRegressor - A linear regression tool thatβs flexible and easy to use
BudouX: the machine learning powered line break organizer tool
BudouX.swift BudouX Swift implementation. BudouX is the machine learning powered
A visual developer tool for inspecting your iOS application data structures.
Tree Dump Debugger A visual developer tool for inspecting your iOS application data structures. Features Inspect any data structure with only one line
A little beautifier tool for xcodebuild
xcbeautify xcbeautify is a little beautifier tool for xcodebuild. Similar to xcpretty, but faster. Features 2x faster than xcpretty. Human-friendly an
A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.
A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.
Swift library and command line tool that interacts with the mach-o file format.
MachO-Reader Playground project to learn more about the Mach-O file format. How to run swift run MachO-Reader path-to-binary You should see a simila
π A simple tool for updating Carthage script phase
Do you use Carthage? Are you feel tired of adding special script and the paths to frameworks (point 4, 5 and 6 in Getting Started guide) manually? Me
A tool to convert Apple PencilKit data to Scribble Proto3.
ScribbleConverter Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation
This is a command line tool to extract an app icon. this sample will extract the icon 16x16 from Safari app.
π X-BundleIcon This is a command line tool to extract an app icon. this sample will extract the icon 16x16 from Safari app. xbi com.apple.Safari 16 /
Comments
-
StencilSwiftKit filters donβt work
When I try using the filter added by StencilSwiftKit, I get the following error:
Error: Unknown filter'lowerFirstWord'. A similar filter was found:'lowercase'.
opened by komaji 3
Owner
Joshua Kaplan
Software Engineer (Mobile) @ Mercari US. Swift, ζ₯ζ¬θͺ, TypeScript, English. He/him/his.
A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript
Dollar Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to L
4.2k Jan 2, 2023
A Swift micro library for generating Sunrise and Sunset times.
Solar A Swift helper for generating Sunrise and Sunset times. Solar performs its calculations locally using an algorithm from the United States Naval
493 Dec 25, 2022
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures. Have the best of both worlds
132 Jan 6, 2023
qr code generator tool
qr code generator tool Small command line tool for generate and reconition qr codes written in Swift Using Usage: ./qrgen [options]
-m, --mode:
3 Jul 15, 2022
Showcase new features after an app update similar to Pages, Numbers and Keynote.
WhatsNew Description WhatsNew automatically displays a short description of the new features when users update your app. This is similar to what happe
1.5k Jan 4, 2023
β² A tiny package to measure code execution time. Only 20 lines of code.
Measure β² A tiny package to measure code execution time. Measure.start("create-user")
let user = User()
Measure.finish("create-user") Console // β² Mea
3 Oct 1, 2022
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan
127 Jun 3, 2022
A SARS-CoV-2 Mutation Pattern Query Tool
vdb A SARS-CoV-2 Mutation Pattern Query Tool 1. Purpose The vdb program is designed to query the SARS-CoV-2 mutational landscape. It runs as a command
13 Oct 25, 2022
This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app.
This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app. It will also, however, contain extra encoding/decoding methods (new encoding/decoding)
0 Oct 16, 2021
AnalyticsKit for Swift is designed to combine various analytical services into one simple tool.
?? AnalyticsKit AnalyticsKit for Swift is designed to combine various analytical services into one simple tool. To send information about a custom eve
6 Jan 14, 2022