Ad-hoc File Gen



hof gen joins CUE with Go’s text/template system and diff3. This section focuses on the ad-hoc, one-liners you can write to generate any file from any data.


Learn about writing templates, with extra functions and helpers

Template writing docs


Check the tests for complete examples

https://github.com/hofstadter-io/hof/tree/_dev/test/render


Want to use and compose code gen modules and dependencies?

Create and use generator modules.

hof gen app.cue -G frontend -G backend -G migrations

See the first-example to learn how.


Command Help


$ hof help gen

hof unifies CUE with Go's text/template system and diff3
  create on-liners to generate any file from any 
  build reusable and modular generators
  edit and regenerate those files while keeping changes

# Render a template
  hof gen input.cue -T template.txt
  hof gen input.yaml schema.cue -T template.txt > output.txt

# Add partials to the template context
  hof gen input.cue -T template.txt -P partial.txt

# The template flag as code gen mappings
  hof gen input.cue -T ... -T ...

  # Generate multiple templates at once
  -T templateA.txt -T templateB.txt

  # Select a sub-input value by CUEpath
  -T templateA.txt:foo
  -T templateB.txt:sub.val

  # Choose a schema with @
  -T templateA.txt:foo@Foo
  -T templateB.txt:sub.val@schemas.val

  # Writing to file with = (semicolon)
  -T templateA.txt=a.txt
  -T templateB.txt:sub.val@schema=b.txt

  # Templated output path, braces need quotes
  -T templateA.txt:='{{ .name | lower }}.txt'

	# Data Files are created when no template
  -T :sub.val='{{ .name | lower }}.json'

  # Repeated templates are used when
  # 1. the output has a '[]' prefix
  # 2. the input is a list or array
  #   The template will be processed per entry
  #   This also requires using a templated outpath
  -T template.txt:items='[]out/{{ .filepath }}.txt'
  -T :items='[]out/{{ .filepath }}.yaml'

  # Output everything to a directory (out name is the same)
  -O out -T types.go -T handlers.go

  # Watch files and directories, doing full or Xcue-less reloads
  -W *.cue -X *.go -O out -T types.go -T handlers.go

# Turn any hof gen flags into a reusable generator module
  hof gen [entrypoints] flags... --as-module [name]
  hof gen [entrypoints] -G [name]

# Bootstrap a new generator module
  hof gen init github.com/hofstadter-io/demos

# List availabel generators
	hof gen list

# Learn about writing templates, with extra functions and helpers
  https://docs.hofstadter.io/code-generation/template-writing/

# Check the tests for complete examples
  https://github.com/hofstadter-io/hof/tree/_dev/test/render

# Compose code gen mappings into reusable modules with
  hof gen app.cue -G frontend -G backend -G migrations -T ...
  https://docs.hofstadter.io/first-example/

Usage:
  hof gen [files...] [flags]
  hof gen [command]

Aliases:
  gen, G

Available Commands:
  info        print details for about generators
  init        initialize a new generator
  list        print available generators

Flags:
      --as-module string          <github.com/username/<name>> like value for the generator module made from the given flags
  -3, --diff3                     enable diff3 support for custom code
      --exec                      enable pre/post-exec support when generating code
  -G, --generator stringArray     generator tags to run, default is all, or none if -T is used
  -h, --help                      help for gen
      --keep-deleted              keep files that would be deleted after code generation
      --no-format                 disable formatting during code gen (ad-hoc only)
  -O, --outdir string             base directory to write all output u
  -P, --partial stringArray       file globs to partial templates to register with the templates
  -T, --template stringArray      template mapping to render, see help for format
  -w, --watch                     run in watch mode, regenerating when files change, implied by -W/X
  -X, --watch-fast stringArray    filepath globs to watch for changes and trigger fast regen
  -W, --watch-globs stringArray   filepath globs to watch for changes and trigger full regen

Global Flags:
  -E, --all-errors               print all available errors
  -i, --ignore-errors            turn off output and assume defaults at prompts
  -D, --include-data             auto include all data files found with cue files
  -V, --inject-env               inject all ENV VARs as default tag vars
  -I, --input stringArray        extra data to unify into the root value
  -p, --package string           the Cue package context to use during execution
  -l, --path stringArray         CUE expression for single path component when placing data files
  -q, --quiet                    turn off output and assume defaults at prompts
  -d, --schema stringArray       expression to select schema to apply to data files
      --stats                    print generator statistics
  -0, --stdin-empty              A flag that ensure stdin is zero and does not block
  -t, --tags stringArray         @tags() to be injected into CUE code
  -U, --user-files stringArray   file globs to embed into the root value (<cue-path>=<file-glob>), use % as slash to trim before
  -v, --verbosity int            set the verbosity of output
      --with-context             add extra context for data files, usable in the -l/path flag

Use "hof gen [command] --help" for more information about a command.