Skip to content
yonaskolbPublic

About

Templating, scaffolding and generation tool

Topics

Resources

Stars

202 stars

Watchers

4 watching

Forks

Repository files navigation

Genesis

SPM Git Version license

Genesis is a templating and scaffolding tool.

A Genesis template is a manifest of options and files written in yaml or json. Option values can be provided in various ways otherwise you will be interactively asked for inputs. Template files are written in Stencil. The list of files can make use of dynamic paths and be generated multiple times depending on the format of the input.

  • ✅ Template manifests in easy to read and write yaml or json
  • ✅ Template files written in Stencil templating language
  • ✅ Provide options via ENV, command line, file or interactively
  • ✅ Powerful command line interactivity with choices, lists and branching
  • ✅ Write dynamic file paths

The very simple example:

options:
  - name: project
    description: The name of the project
    question: What is the name of your project?
    required: true
    type: string
files:
  - template: project.stencil
    path: "{{ project }}.project"

And then run like this:

$ genesis generate template.yml
What is the name of your project? MyProject
Generated files:
  MyProject.project

Or you can provide the required options via arguments

$ genesis generate template.yml --options name:MyProject
Generated files:
  MyProject.project

Much more powerful templates are possible. See more complete documentation here

Installing

Make sure Xcode 13 is installed first.

$ mint install yonaskolb/genesis

Make

$ git clone https://github.com/yonaskolb/Genesis.git
$ cd Genesis
$ make

Swift Package Manager

Use CLI

$ git clone https://github.com/yonaskolb/Genesis.git
$ cd Genesis
$ swift run mint

Use as dependency

Add the following to your Package.swift file's dependencies:

.package(url: "https://github.com/yonaskolb/Genesis.git", from: "0.1.0"),

And then import GenesisKit. See GenesisKit for more information.

Usage

Run genesis help for usage instructions

Usage: genesis generate <templatePath> [options]

Options:
  -d, --destination <value>    Path to the directory where output will be generated. Defaults to the current directory
  -h, --help                   Show help information for this command
  -a, --ask-all                Also ask options that have a default value, offering it as the default answer. Without this, defaults are used without asking
  -n, --non-interactive        Do not prompt for required options
  -o, --options <value>        Provide option overrides, in the format --options "option1: value 1, option2: [value 2, value 3]". Escape literal commas as \,.
  -p, --option-path <value>    Path to a yaml or json file containing options
  --on-conflict <value>        How to resolve files where the template and local edits changed the same lines: merge (default), prompt, overwrite, keep, or error
  --resolver <value>           Command to run when conflicts remain, e.g. "claude -p"
  --track / --no-track         Record (or don't record) this generation in .genesis/ for later merging. Overrides the template's track setting
  --verify                     Run the template's verify command after generation

Previewing a regeneration

genesis diff takes the same arguments as generate and shows what generating would change, without writing anything:

genesis diff template.yml -d MyApp -p options.yml            # summary and diffs of your files
genesis diff template.yml -d MyApp -p options.yml --stat     # summary only
genesis diff template.yml -d MyApp -p options.yml --template-only  # only what the template's output changed

It exits with 1 when something would change, so genesis diff -n --stat can check in CI that a project is up to date with its template.

Testing a template

A template can declare variants: named sets of options, each one a test generation of the template. genesis test generates every variant non-interactively, runs the template's post_gen and verify commands in each, and prints a pass or fail summary:

genesis test template.yml                          # every variant, in a temporary directory
genesis test template.yml --variant minimal        # only some variants (repeatable)
genesis test template.yml -d generated             # keep the output in generated/<variant>
genesis test template.yml --no-verify              # generate and run post_gen only

Every variant runs even if an earlier one fails, and the command exits with 1 if any failed. With --destination, each <destination>/<variant> directory is deleted before it's generated.

Providing options

Options will be passed to the template in the following order, merging along the way, with duplicate options overriding the previous ones.

1. Environment Variables

Set any environment variables before you call generate, or use already existing ones from your development environment

name="My Name" genesis generate template.yml

2. Options File

Pass a path to a json or yaml file with --option-file This can include more structures and complex data. For example:

name: MyProject
targets:
  - name: MyTarget
    type: application
  - name: MyFramework
    type: framework  

3. Options Argument

Pass specific options with --options like this

--options "option1: value 1, option2: value 2"

Inline array values can be passed with brackets:

--options "actions: [tap, refresh, dismiss]"

Only the first : separates a key from its value, so values like URLs can be passed directly. Escape commas that are part of a scalar value with \.

--options "url: https://example.com/path:segment, title: Hello\, world"

4. Interactive input

Genesis will ask you for any missing options if they are required. You can turn off interactive input with --non-interactive.

5. Default value

Each option can have a default value which be used as a fallback. Options with a value aren't asked interactively, so the common case only asks what has no default. Pass --ask-all to be asked every option, with its value shown as the default answer that pressing return accepts.

Missing value

If an option is required and still hasn't received a value from anywhere, generation will fail.

Template

A genesis template is a yaml or json file that includes a list of options and files

Options

Options are structured input for the Stencil templates. They serve as documentation and allow for Genesis to interactively ask for input.

  • name: This is the name that is referenced in the template as well as the command line
  • value: This is the default value that will be used if none are provided. This can have Stencil tags in it
  • question: The question that is asked when asking for input. This can have Stencil tags in it
  • description: An extended description of the option and what it does
  • required: Whether this option is required or not for the template to generate. If it is not provided via the command line, option file, or input, generation will fail
  • type: This is the type of option. It defaults to string but can be any one of the following:
    • string a simple string
    • boolean a boolean
    • choice a string from a list of choices. Requires choices to be defined
    • array an array of other options. If child options are defined, if will be an array of options, otherwise it will split a string by command, removing any whitespace between
  • options: An array of child options. Used if the array type contains objects
  • include: Whether the option is asked at all, based on earlier options. This is a Stencil if tag without the braces, like a file's include. For example include: include_tests only asks the option when include_tests is true. An excluded option isn't asked, isn't required, and has no value, even if one was passed in

Files

  • path: This is the path the file will be generated at. It can include Stencil tags to make it dynamic.
  • contents: A file template string
  • template: A path to a file template
  • copy: A path to copy. This can be a file or a directory
  • context: An optional context property path that will be passed to the template. If this resolves to an array, a file for each element will be created, using tags in path to differentiate them.
  • include: Whether the file should be written. This is a Stencil if tag without the braces. For example instead of {% if type == 'framework' %} you would write type == 'framework'

Hooks and variants

  • pre_gen: A shell command run in the destination before files are written
  • post_gen: A shell command run in the destination after files are written, e.g. xcodegen generate
  • verify: A shell command that checks the generated output, e.g. swift build. It runs after post_gen with genesis generate --verify and in genesis test
  • variants: A dictionary of named variants for genesis test. Each has an optional description and an options dictionary, given to the template just like an options file. YAML anchors work for sharing values between variants
variants:
  full:
    description: Every optional feature on
    options:
      name: Example
      include_tests: true
  minimal:
    options:
      name: Example
      include_tests: false

Regenerating

Templates that scaffold a whole project can set track: true at the top level (or pass --track). Genesis then records what it generated in .genesis/ in the destination, so you can regenerate the project after changing the template or its options, even if you've edited the generated files since. Check .genesis/ into source control.

Templates that generate one of many components into a project should leave tracking off (the default, or --no-track). Each run just writes its files and leaves other components alone. Existing files that differ are still never overwritten silently: they're handled as conflicts as described below.

  • .genesis/manifests/{template}.json lists each generated file and its hash, plus the options used.
  • .genesis/base/{template}/ mirrors the plain template output for each generated text file. This is the base for a 3-way merge, and its diff in a PR shows exactly how the template's output changed.
  • .genesis/.gitignore ignores .genesis/conflicts/, which only exists until conflicts are resolved.

On regeneration each file is handled like this:

  • Only the template changed it: it's updated.
  • Only you changed it: your version is kept.
  • Both changed it: your changes and the template's are merged with git merge-file. If they touch the same lines, the file gets git-style conflict markers (<<<<<<< yours, ||||||| base, =======, >>>>>>> template).
  • The template now generates it at a different path, because an option in its path changed (e.g. renaming the project): it's moved to the new path, with your edits merged in.
  • The template no longer generates it: it's deleted, unless you've edited it, in which case it's kept.

Anything left to resolve is listed in .genesis/conflicts/{template}.json, with the base, your version, the new template output, and the template file each came from. The command exits with an error until the conflict markers are resolved.

To have an agent resolve conflicts, pass a command with --resolver. It runs in the destination with resolution instructions on stdin and the report path in $GENESIS_CONFLICT_REPORT:

genesis generate template.yml --resolver "claude -p --permission-mode acceptEdits"

Stencil Templates

Each Stencil template has all the filters available in StencilSwiftKit, and has access to all the options. See Stencil for more info about tags.

Small example:

{% if name %}
name: {{ name }}
{% endif %}

GenesisKit

The library GenesisKit can be used to easily provide generation in your own tools.

import GenesisKit

// create a context
let context: [String: Any] = ["name": "hello"]

// create a template, either from a file or programmatically
let template = try GenesisTemplate(path: "template.yml")

// Create the generator
let generator = try TemplateGenerator(template: template)

// generate the files
let generationResult = try generator.generate(context: context, interactive: false)

// write the files to disk
try generationResult.writeFiles(path: "destination")

Contributions

Pull requests and issues are welcome

License

Genesis is licensed under the MIT license. See LICENSE for more info.

About

Templating, scaffolding and generation tool

Topics

Resources

Stars

202 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages