Categorygithub.com/code-gorilla-au/pyrotic
modulepackage
1.0.0
Repository: https://github.com/code-gorilla-au/pyrotic.git
Documentation: pkg.go.dev

# README

pyrotic

code generator inspired by https://www.hygen.io/ for golang.

Motivation

Why not use hygen? great question! I would recommend hygen over this, however hygen is written in js. This project is for people who want to use a code generator and not have to install node. Pyrotic also is specifically written to generate go code, all templates are parsed using go's builtin template parser and output is formatted using go's built in code formatter.

Install

go install github.com/code-gorilla-au/pyrotic@latest

initial setup creates a _templates directory at the root of the project to hold the generators

pyrotic init

create your first generator

pyrotic new cmd

Run

default template path is _templates and default file extension is .tmpl

pyrotic generate <name of generator> --name <name-to-pass-in>

eg: pyrotic generate cmd --name setup

Use different directory

pyrotic --path example/_templates generate cmd --name setup
pyrotic -p example/_templates generate cmd --name setup

Use different file extension

default file extension is .tmpl

pyrotic --extension ".template" generate cmd --name setup
pyrotic -x ".template" generate cmd --name setup

Dry run mode

dry run will log to console rather than write to file

pyrotic -d generate cmd --name setup
pyrotic --dry-run generate cmd --name setup

Different shared folder

default shared templates path is _templates/shared

pyrotic --shared foo/bar generate cmd --name setup
pyrotic -s foo/bar generate cmd --name setup

Formatter properties

Formatter will pick up any of these variables within the --- block and hydrate the metadata for the template. Any properties matching the signature will be added to the Meta property, for example foo: bar will be accessible by {{ Meta.foo }}. View more examples.

PropertyTypeDefaultExample
to:string (path)""src/lib/utils/readme.md
append:boolfalsefalse
inject:boolfalsefalse
before:string""type config struct
after:string""// commands

Using shared templates

In some instances you will want to reuse some templates across multiple generators. This can be done by having a shared directory within the _templates directory. Any templates that are declared in the shared directory will be loading along with the generator. Reference the shared template within your generator directory in order to inject / append / create file.

Built in template functions

ships with some already built in template funcs, some examples

func namedescriptioncode exampleresult
caseSnakeconvert to snake case{{ MetaData | caseSnake }}meta_data
caseKebabconvert to kebab case{{ MetaData | caseKebab }}meta-data
casePascalconvert to pascal case{{ meta_data | casePascal }}MetaData
caseLowerconvert to lower case{{ MetaData | caseLower }}metadata
caseTitleconvert to title case{{ MetaData | caseTitle }}METADATA
caseCamelconvert to camel case{{ MetaData | caseCamel }}metaData
splitByDelimitersplits string by delimiter{{ splitByDelimiter "long,list" "," }}[]string{"long" "list"}
splitAfterDelimitersplits string after delimiter{{ splitAfterDelimiter "a,long,list" "," }}[]string{"a," "long," "list"}
containschecks if string contains substring{{ contains "foobarbin" "bar" }}true
hasPrefixchecks if string has the prefix{{ contains "foobarbin" "foo" }}true
hasSuffixchecks if string has the suffix{{ contains "foobarbin" "bin" }}true

we also provide some Inflections using flect

  • pluralise
  • singularise
  • ordinalize
  • titleize
  • humanize

Pass in meta via cmd

you can pass in meta data via the --meta or -m flag, which takes in a comma (,) delimited list, and overrides the {{ .Meta.<your-property> }} within the template.


pyrotic generate fakr --meta foo=bar,bin=baz
pyrotic generate fakr -m foo=bar,bin=baz

Dev mode

provides the short file name with logging


ENV=DEV ./pyrotic -p example/_templates generate fakr --meta foo=bar,bin=baz

# Packages

No description provided by the author