Documentation ¶
Overview ¶
Package generator deals with creating projects from template files.
A generator is a directory containing a definition file, template files, and partials.
In addition to metadata, the definition file, which must be a JSON document named `generator.json` at the root of the generator, can define variables and user inputs that will be made available to the templates and partials. An input is read from the user only when its value is needed for the first time.
The templates are files that should be placed in a `files` directory, and use the Go template syntax to produce project files. Every template will result in a file of the same name being created in the generated project directory. The path of the generated file within the generated project wil be the path of the template relative to the `files` directory.
Templates can include partials via the `partial` function. The partials should be placed in a `partials` directory. As opposed to templates, partials will not result in files being generated. The `partial` function expects the path of a partial relative to the `partials` directory, and optionally a variadic list of variable maps for the partial. The partials have access to the same functions and variables as the templates.
By default templates are evaluated in alphabetical order. You can have more control over the order by adding a `priorities` array to the definition file. This array should contain a list of files relative to the `files` directory that will be evaluated first. That way it is possible to control the order which inputs will be read from the user.
A basic definition file may look something like this:
{ "name": "basic", "version": "0.1.0", "description": "A basic generator", "author": "Stratumn", "license": "MIT", "inputs": { "name": { "type": "string", "prompt": "Project name:", "default": "{{.dir}}", "format": ".+" } } }
In this case, one input called `name` of type `string` is defined. Its default value is `{{.dir}}`, which should be a variable given to the definition file parser.
A template file in the `template` directory can access the user input for `name` using the template function `input`. For instance it could be a Markdown file containing the following:
# {{input "name"}} A basic project
A project can be generated from the generator this way:
// Directory where the project will be generated. dst := "path/to/generated/project" // Add a `dir` variable for the definition file set to the name // of the project directory. opts := generator.Options{ DefVars: map[string]interface{}{ "dir": filepath.Dir(dst), }, } // Load the generator. gen, err := generator.NewFromDir("path/to/generator", &opts) if err != nil { panic(err) } // Generate the project. if err := gen.Exec(dst); err != nil { panic(err) }
Index ¶
Constants ¶
const ( // DefinitionFile is the file containing the generator definition within // a generator. DefinitionFile = "generator.json" // PartialsDir is the directory containing partials within a generator. PartialsDir = "partials" // FilesDir is the directory containing files within a generator. FilesDir = "files" )
const ( // StringInputID is the string identifying a string input. StringInputID = "string" // StringSelectID is the string identifying a string select. StringSelectID = "select:string" )
Variables ¶
This section is empty.
Functions ¶
func StdDefinitionFuncs ¶
StdDefinitionFuncs returns the standard function map used when parsing a generator definition. It adds the following functions:
- now(format string) string: returns a formatted representation of the current date
- nowUTC(format string) string: returns a formatted representation of the current UTC date
- secret(length int) (string, error): returns a random secret string
Types ¶
type Definition ¶
type Definition struct { // Name is the name of the generator. // It should be short, lowercase, and contain only letters, numbers, and // dashes. Name string `json:"name"` // Version is the version string of the generator. Version string `json:"version"` // Description is a short description of the generator. Description string `json:"description"` // Author is the name of the author of the generator. Author string `json:"author"` // License is the license of the generator (not of the generated code). // If the generated project has a license, it should be defined within // the templates. License string `json:"license"` // Variables is a map of variables for the templates and partials. Variables map[string]interface{} `json:"variables"` // Inputs is a map of user inputs. Inputs InputMap `json:"inputs"` // Priorities is an array of files which should be parsed first. // The paths are relative to the `files` directory. Priorities []string `json:"priorities"` }
Definition contains properties for a template generator definition.
func NewDefinitionFromFile ¶
func NewDefinitionFromFile(path string, vars map[string]interface{}, funcs template.FuncMap) (*Definition, error)
NewDefinitionFromFile loads a generator definition from a file. The file is treated as a template and is fed the given variables and functions. Extra variables and functions given will be added to the template context.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator deals with parsing templates, handling user input, and outputting processed templates.
func NewFromDir ¶
NewFromDir create a new generator from a directory.
func (*Generator) Exec ¶
Exec parses templates, handles user input, and outputs processed templates to given dir.
func (*Generator) StdTmplFuncs ¶
StdTmplFuncs returns the standard function map used when parsing a template It adds the following functions:
- ask(json string) (interface{}, error): creates an input on-the-fly and returns its value
- input(id string) (interface{}, error): returns the value of an input
- now(format string) string: returns a formatted representation of the current date
- nowUTC(format string) string: returns a formatted representation of the current UTC date
- partial(path, vars []interface{}...) (string, error): executes the partial with given name and variables (path relative to `partials` directory)
- secret(length int) (string, error): returns a random secret string
type Input ¶
type Input interface { // Set must set the value of the input or return an error. // It should be able to, at the very least, set the value from a string. Set(interface{}) error // Get must return the value of the input. Get() interface{} // Msg must return a message that will be displayed when prompting the // value. Msg() string }
Input must be implemented by all input types.
func UnmarshalJSONInput ¶
UnmarshalJSONInput creates an input from JSON.
type InputMap ¶
InputMap is a maps input names to inputs.
func (*InputMap) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler.
type InputShared ¶
InputShared contains properties shared by all input types.
type Options ¶
type Options struct { // Variables for the definition file. DefVars map[string]interface{} // Functions for the definition file. DefFuncs template.FuncMap // Variables for the templates. TmplVars map[string]interface{} // Functions for the templates. TmplFuncs template.FuncMap // A reader for user input, default to stdin. Reader io.Reader }
Options contains options for a generator.
type StringInput ¶
type StringInput struct { // Default is the default value. Default string `json:"default"` // Format is a string containing a regexp the value must have. Format string `json:"format"` // contains filtered or unexported fields }
StringInput contains properties for string inputs.
func (*StringInput) Get ¶
func (in *StringInput) Get() interface{}
Get implements github.com/stratumn/go/generator.Input.
func (*StringInput) Msg ¶
func (in *StringInput) Msg() string
Msg implements github.com/stratumn/go/generator.Prompt.
func (*StringInput) Set ¶
func (in *StringInput) Set(val interface{}) error
Set implements github.com/stratumn/go/generator.Input.
type StringSelect ¶
type StringSelect struct { // Default is the default value. Default string `json:"default"` // Options is an array of possible values. Options []StringSelectOption `json:"options"` // contains filtered or unexported fields }
StringSelect contains properties for string select inputs.
func (*StringSelect) Get ¶
func (in *StringSelect) Get() interface{}
Get implements github.com/stratumn/go/generator.Input.
func (*StringSelect) Msg ¶
func (in *StringSelect) Msg() string
Msg implements github.com/stratumn/go/generator.Prompt.
func (*StringSelect) Set ¶
func (in *StringSelect) Set(val interface{}) error
Set implements github.com/stratumn/go/generator.Input.
type StringSelectOption ¶
type StringSelectOption struct { // Input is the string the user must enter to choose this option. Input string `json:"input"` // Value is the value the input will have if this option is selected. Value string `json:"value"` // Text will be displayed when presenting this option to the user. Text string `json:"text"` }
StringSelectOption contains properties for string select options.