Documentation
¶
Overview ¶
Package scaffold provides file and directory generation based on templates.
A template must be UTF8 without byte order marker and have \n (linefeed) as line terminator. It has a head and a body, separated by an empty line:
- head (must not contain an empty line)
- empty line
- body
The head might contain anything but empty lines, but it is recommended to put some annotated json string as example for the usage into it. Also authorship of the template and contact infos can be put there.
The syntax of the body is a superset of the Go text/template package (http://golang.org/pkg/text/template). The available functions inside the body are extended by the functions defined in the FuncMap variable.
Additionally to the functionality provide via the text/template package there are contexts.
Context ¶
The template body can have folder and file contexts. A context is started by a line with the prefix ">>>" and ends by a line with the prefix "<<<". Each context has a name that follows after ">>>" or "<<<" until the end of the line. For example the context ">>>a" is ended by "<<<a" and the name of the context would be "a".
If the name of the context ends with a slash (/), the context is a folder context otherwise it is a file context.
The name of a file context defines the name of the file into which the content of the context will be saved. The folder of the file is defined by the surrounding folder contexts. The name of a folder context defines the name of the folder inside which the inner folders and files (as defined by the inner contexts) are saved.
The outermost folder context is the baseDir parameter of the Run function (defaults to the current working directory in the CLI tool).
The following would create the file "fileZ.txt" inside the folder "[baseDir]/folder1/folderA". Any missing directories a created on the fly.
>>>folder1/ >>>folderA/ >>>fileZ.txt Hello World <<<fileZ.txt <<<folderA/ <<<folder1/
The placeholders inside the body are organized as a json object / map. When the Run function is called, the json objects is mixed to the template and after that the folders and files are created as defined in the result. That makes it possible to use placeholders as parts of folder or file names.
Escaping of double curly braces and dollar chars ¶
Curly braces and dollar chars are part of syntax of the go template engine and there is no syntax to replace them from inside the template. However, the scaffold package has included some helper functions that return them.
{{doubleCurlyOpen}}{{dollar}}{{doubleCurlyClose}}
will result in the string "{{$}}".
Most of the time this package will be used via the scaffold command sub package.
It can be installed via
go get gopkg.in/metakeule/scaffold.v1/lib/scaffold
Run
scaffold help
to see the available options.
For a complete example have a look at the example directory. The make.sh file contains the needed CLI command.
Index ¶
- Variables
- func Args() string
- func CamelCase1(src string) string
- func CamelCase2(src string) string
- func CurlyClose() string
- func CurlyOpen() string
- func Dollar() string
- func DoubleCurlyClose() string
- func DoubleCurlyOpen() string
- func FileName(in string) (out string)
- func FileNameLower(src string) string
- func Host() string
- func Now() string
- func Replace(s, old, new string) string
- func Run(baseDir path.Local, body string, json io.Reader, log io.Writer, verbose bool) (err error)
- func RunFS(fsys fs.FS, body string, json io.Reader, log io.Writer, verbose bool) (err error)
- func Scan(fsys fs.ReadOnly, dir path.Relative, opts ...ScanOption) (template []byte, err error)
- func SplitTemplate(template string) (head, body string)
- func User() string
- type ScanOption
Constants ¶
This section is empty.
Variables ¶
var FuncMap = template.FuncMap{ "user": User, "args": Args, "now": Now, "host": Host, "replace": Replace, "filename": FileName, "filenameLower": FileNameLower, "camelCase1": CamelCase1, "camelCase2": CamelCase2, "title": strings.Title, "toLower": strings.ToLower, "toUpper": strings.ToUpper, "trim": strings.Trim, "doubleCurlyOpen": DoubleCurlyOpen, "doubleCurlyClose": DoubleCurlyClose, "curlyOpen": CurlyOpen, "curlyClose": CurlyClose, "dollar": Dollar, }
FuncMap provides functions to the template. New functions can be added as needed. The usual restrictions for text/template.FuncMap apply (see http://golang.org/pkg/text/template/#FuncMap)
Functions ¶
func CamelCase1 ¶
CamelCase1 converts a string in snake_case to CamelCase where the first letter of each word is capitalized
func CamelCase2 ¶
CamelCase2 converts a string in snake_case to camelCase where the first letter of each but the first word is capitalized
func DoubleCurlyClose ¶
func DoubleCurlyClose() string
DoubleCurlyClose returns two closed curly braces
func FileName ¶
FileName converts a string to a safe filename. First leading and trailing spaces are removed Every other space is replaced by - Multiple following - are replaced by on single - Every character other than [-a-zA-Z_0-9] is removed
func FileNameLower ¶
same as FileName but transforms string to lowercase
func Run ¶
Run mixes the properties of the json object to the template body. The result is then used to create files and directories beneath baseDir. If log is not nil a list of files that will be created is written to log.
func Scan ¶
scans a directory recursively and creates a template based on the structure of the files and directories
func SplitTemplate ¶
SplitTemplate splits the given template on the first empty line. It returns the head and body of the template. Templates must be UTF8 without byte order marker and have \n (linefeed) as line terminator.
Types ¶
type ScanOption ¶
type ScanOption func(*scannerNew)
func SkipDirs ¶
func SkipDirs(regexstring string) ScanOption