Documentation ¶
Index ¶
- Constants
- Variables
- func CreateTemplateHelpers(templatePath string, opts *options.BoilerplateOptions, tmpl *template.Template) template.FuncMap
- func PathRelativeToTemplate(templatePath string, filePath string) string
- func RenderTemplateFromString(templatePath string, templateContents string, variables map[string]interface{}, ...) (string, error)
- func RenderTemplateRecursively(templatePath string, templateContents string, variables map[string]interface{}, ...) (string, error)
- func RenderTemplateWithPartials(templatePath string, partials []string, variables map[string]interface{}, ...) (string, error)
- func RenderVariable(variable interface{}, variables map[string]interface{}, ...) (interface{}, error)
- func RenderVariables(variables map[string]interface{}, opts *options.BoilerplateOptions) (map[string]interface{}, error)
- type InvalidSnippetArguments
- type InvalidTypeForMethodArgument
- type SnippetNotFound
- type SnippetNotTerminated
- type TemplateContainsInfiniteLoop
- type TemplateHelper
Constants ¶
const MaxRenderAttempts = 15
const SHELL_DISABLED_PLACEHOLDER = "replace-me"
Variables ¶
var CAMEL_CASE_REGEX = regexp.MustCompile( "(^([[:lower:]]|[[:digit:]])+)|" + "([[:upper:]]*([[:lower:]]|[[:digit:]]|$)*)") // Handle normal camel case
This regex can be used to split CamelCase strings into "words". That is, given a string like FooBarBaz, you can use this regex to split it into an array ["Foo", "Bar", "Baz"]. It also handles lower camel case, which is the same as camel case, except it starts with a lower case word, such as fooBarBaz.
To capture lowercase camel case, we just look for words that consist of lower case letters and digits at the start of the string. To capture all other camel case, we look for "words" that start with one or more consecutive upper case letters followed by one or more lower case letters or digits.
var ENV_VAR_REGEX = regexp.MustCompile("^ENV:(.+?)=(.*)$")
var NoArgsPassedToShellHelper = fmt.Errorf("The shell helper requires at least one argument")
var PUNCTUATION_OR_WHITESPACE_REGEX = regexp.MustCompile("([[:space:]]|[[:punct:]])+")
var SNIPPET_MARKER_REGEX = regexp.MustCompile("boilerplate-snippet:\\s*(.+?)(?:\\s|$)")
var WHITESPACE_REGEX = regexp.MustCompile("[[:space:]]+")
Functions ¶
func CreateTemplateHelpers ¶
func CreateTemplateHelpers(templatePath string, opts *options.BoilerplateOptions, tmpl *template.Template) template.FuncMap
Create a map of custom template helpers exposed by boilerplate
func PathRelativeToTemplate ¶
Returns the given filePath relative to the given templatePath. If filePath is already an absolute path, returns it unchanged.
Example:
pathRelativeToTemplate("/foo/bar/template-file.txt, "../src/code.java")
Returns: "/foo/src/code.java"
func RenderTemplateFromString ¶ added in v0.3.1
func RenderTemplateFromString(templatePath string, templateContents string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)
Render the template at templatePath, with contents templateContents, using the Go template engine, passing in the given variables as data.
func RenderTemplateRecursively ¶
func RenderTemplateRecursively(templatePath string, templateContents string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)
Render the template at templatePath, with contents templateContents, using the Go template engine, passing in the given variables as data. If the rendered result contains more Go templating syntax, render it again, and repeat this process recursively until there is no more rendering to be done.
The main use case for this is to allow boilerplate variables to reference other boilerplate variables. This can obviously lead to an infinite loop. The proper way to prevent that would be to parse Go template syntax and build a dependency graph, but that is way too complicated. Therefore, we use hacky solution: render the template multiple times. If it is the same as the last time you rendered it, that means no new interpolations were processed, so we're done. If it changes, that means more interpolations are being processed, so keep going, up to a maximum number of render attempts.
func RenderTemplateWithPartials ¶ added in v0.3.1
func RenderTemplateWithPartials(templatePath string, partials []string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)
RenderTemplateWithPartials renders the template at templatePath with the contents of the root template (the template named by the user on the command line) as well as all of the partials matched by the provided globs using the Go template engine, passing in the given variables as data.
func RenderVariable ¶
func RenderVariable(variable interface{}, variables map[string]interface{}, opts *options.BoilerplateOptions) (interface{}, error)
Variable values are allowed to use Go templating syntax (e.g. to reference other variables), so here, we render those templates and return a new map of variables that are fully resolved.
func RenderVariables ¶
func RenderVariables(variables map[string]interface{}, opts *options.BoilerplateOptions) (map[string]interface{}, error)
Variable values are allowed to use Go templating syntax (e.g. to reference other variables), so this function loops over each variable value, renders each one, and returns a new map of rendered variables.
Types ¶
type InvalidSnippetArguments ¶
type InvalidSnippetArguments []string
func (InvalidSnippetArguments) Error ¶
func (args InvalidSnippetArguments) Error() string
type InvalidTypeForMethodArgument ¶
type InvalidTypeForMethodArgument struct { MethodName string ExpectedType string ActualType string }
func (InvalidTypeForMethodArgument) Error ¶
func (err InvalidTypeForMethodArgument) Error() string
type SnippetNotFound ¶
type SnippetNotFound string
func (SnippetNotFound) Error ¶
func (snippetName SnippetNotFound) Error() string
type SnippetNotTerminated ¶
type SnippetNotTerminated string
func (SnippetNotTerminated) Error ¶
func (snippetName SnippetNotTerminated) Error() string
type TemplateContainsInfiniteLoop ¶
type TemplateContainsInfiniteLoop struct { TemplatePath string TemplateContents string RenderAttempts int }
func (TemplateContainsInfiniteLoop) Error ¶
func (err TemplateContainsInfiniteLoop) Error() string
type TemplateHelper ¶
type TemplateHelper func(templatePath string, opts *options.BoilerplateOptions, args ...string) (string, error)
All boilerplate template helpers implement this signature. They get the path of the template they are rendering as the first arg, the Boilerplate Options as the second arg, and then any arguments the user passed when calling the helper.