Documentation ¶
Index ¶
- Constants
- Variables
- func CreateTemplateHelpers(templatePath string, opts *options.BoilerplateOptions, tmpl *template.Template) template.FuncMap
- func PathRelativeToTemplate(templatePath string, filePath string) string
- func RenderJsonnetTemplate(templatePath string, variables map[string]interface{}, ...) (string, error)
- func RenderTemplateFromString(templatePath string, templateContents string, variables map[string]interface{}, ...) (string, error)
- func RenderTemplateWithPartials(templatePath string, partials []string, variables map[string]interface{}, ...) (string, error)
- func RenderVariables(opts *options.BoilerplateOptions, variablesToRender map[string]interface{}, ...) (map[string]interface{}, error)
- type InvalidSnippetArguments
- type InvalidTypeForMethodArgument
- type MaxRenderAttemptsErr
- type SnippetNotFound
- type SnippetNotTerminated
- 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 RenderJsonnetTemplate ¶ added in v0.3.5
func RenderJsonnetTemplate( templatePath string, variables map[string]interface{}, opts *options.BoilerplateOptions, ) (string, error)
RenderJsonnetTemplate renders the jsonnet template at templatePath to the target location specified in the boilerplate options, passing in the boilerplate variables as Top Level Arguments. Note that this function will also make available the following helper values as External variables:
- templateFolder - outputFolder
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 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 RenderVariables ¶
func RenderVariables( opts *options.BoilerplateOptions, variablesToRender map[string]interface{}, alreadyRenderedVariables map[string]interface{}, ) (map[string]interface{}, error)
RenderVariables will render each of the variables that need to be rendered by running it through the go templating syntax. 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.
This function supports nested variables references, but uses a heuristic based approach. Ideally, we can parse the Go template and build up a graph of variable dependencies to assist with the rendering process, but this takes a lot of effort to get right and maintain.
Instead, we opt for a simpler approach of rendering with multiple trials. In this approach, we continuously attempt to render the template on the variable until all of them render without errors, or we reach the maximum trials. To support this, we ignore the missing key configuration during this evaluation pass and always rely on the template erroring for missing variables. Otherwise, all the variables will render on the first pass.
Note that this is NOT a multi pass algorithm - that is, we do NOT attempt to render the template multiple times. Instead, we do a single template render on each run and reject any that return with an error.
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 MaxRenderAttemptsErr ¶ added in v0.4.1
type MaxRenderAttemptsErr struct{}
func (MaxRenderAttemptsErr) Error ¶ added in v0.4.1
func (err MaxRenderAttemptsErr) 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 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.