scaffolder

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 9 Imported by: 4

README

A general-purpose project scaffolding library and tool inspired by cookiecutter

stability-experimental Go Reference CI

Scaffolder evaluates the scaffolding files at the given destination against ctx using the following rules:

  • Templates are evaluated using the Go template engine.
  • Both path names and file contents are evaluated.
  • If a file name ends with .tmpl, the .tmpl suffix is removed.
  • If a file or directory name evalutes to the empty string it will be excluded.
  • If a file named template.js exists in the root of the template directory, all functions defined in this file will be available as Go template functions.
  • Directory and file names in templates can be expanded multiple times using the push function. This function takes two arguments, the file/directory name and the context to use when evaluating templates within the file/directory.

Examples

Multiple directories
template/
  {{ range .Modules }}{{ push .Name  . }}{{ end }}/
    file.txt

Documentation

Overview

Package scaffolder is a general purpose file-system based scaffolding tool inspired by cookiecutter.

Index

Constants

This section is empty.

Variables

View Source
var ErrSkip = errors.New("skip directory")

ErrSkip can be returned by WalkDir callbacks to skip a file or directory.

Functions

func Scaffold

func Scaffold(source, destination string, ctx any, options ...Option) error

Scaffold evaluates the scaffolding files at the given source using ctx, while copying them into destination.

func WalkDir added in v0.6.0

func WalkDir(dir string, fn func(path string, d fs.DirEntry) error) error

WalkDir performs a depth-first walk of dir, executing fn before each file or directory.

If fn returns ErrSkip, the directory will be skipped.

Types

type AfterEachExtensionFunc added in v0.7.0

type AfterEachExtensionFunc func(path string) error

AfterEachExtensionFunc is a convenience type for creating an Extension.AfterEach from a function.

func (AfterEachExtensionFunc) AfterEach added in v0.7.0

func (f AfterEachExtensionFunc) AfterEach(path string) error

func (AfterEachExtensionFunc) Extend added in v0.7.0

func (f AfterEachExtensionFunc) Extend(mutableConfig *Config) error

type Config added in v0.6.0

type Config struct {
	Context any
	Funcs   FuncMap
	Exclude []string
	// contains filtered or unexported fields
}

Config for the scaffolding.

func (*Config) Source added in v0.6.0

func (c *Config) Source() string

func (*Config) Target added in v0.6.0

func (c *Config) Target() string

type Extension added in v0.6.0

type Extension interface {
	Extend(mutableConfig *Config) error
	AfterEach(path string) error
}

Extension's allow the scaffolder to be extended.

type ExtensionFunc added in v0.6.0

type ExtensionFunc func(mutableConfig *Config) error

ExtensionFunc is a convenience type for creating an Extension.Extend from a function.

func (ExtensionFunc) AfterEach added in v0.6.0

func (f ExtensionFunc) AfterEach(path string) error

func (ExtensionFunc) Extend added in v0.6.0

func (f ExtensionFunc) Extend(mutableConfig *Config) error

type FuncMap added in v0.6.0

type FuncMap = template.FuncMap

FuncMap is a map of functions to use in scaffolding templates.

The key is the function name and the value is a function taking a single argument and returning either `string` or `(string, error)`.

type Option

type Option func(*scaffoldOptions)

Option is a function that modifies the behaviour of the scaffolder.

func AfterEach added in v0.6.0

func AfterEach(after func(path string) error) Option

AfterEach configures Scaffolder to call "after" for each file or directory created.

Useful for setting file permissions, etc.

Each AfterEach function is called in order.

func Exclude added in v0.3.0

func Exclude(paths ...string) Option

Exclude the given regex paths from scaffolding.

Matching occurs before template evaluation and .tmpl suffix removal.

func Extend added in v0.6.0

func Extend(plugin Extension) Option

Extend adds an Extension to the scaffolder.

An extension can be used to add functions to the template context, to modify the template context, and so on.

func Functions

func Functions(funcs FuncMap) Option

Functions adds functions to use in scaffolding templates.

Directories

Path Synopsis
extensions
javascript Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL