scaffolder

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: Apache-2.0 Imports: 10 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 desScaffolder evaluates a template of directories and files into a destination using JSON context 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 will be 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.
  • The following functions are available as Go template functions: "snake", "screamingSnake", "camel", "lowerCamel", "kebab", "screamingKebab", "upper", "lower", "title",

For example, given the following files and directories as the template:

template/
  {{ range .modules }}{{ push .name  . }}{{ end }}/
    file.txt

And the context "context.json":

{
  "modules": [
    {"name": "module1", "path": "path1"},
    {"name": "module2", "path": "path2"}
  ]
}

Running scaffolder with:

scaffolder --json context.json --template template --dest dest

The output "dest" directory will contain the following files and directories:

module1/
  file.txt
module2/
  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 About added in v1.5.0

func About() string

About returns text describing how scaffolder works.

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

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

type AfterEachExtensionFunc func(path string) error

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

func (AfterEachExtensionFunc) AfterEach

func (f AfterEachExtensionFunc) AfterEach(path string) error

func (AfterEachExtensionFunc) Extend

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

type Config

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

Config for the scaffolding.

func (*Config) Source

func (c *Config) Source() string

func (*Config) Target

func (c *Config) Target() string

type Extension

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

Extension's allow the scaffolder to be extended.

type ExtensionFunc

type ExtensionFunc func(mutableConfig *Config) error

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

func (ExtensionFunc) AfterEach

func (f ExtensionFunc) AfterEach(path string) error

func (ExtensionFunc) Extend

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

type FuncMap

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

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

func Exclude(paths ...string) Option

Exclude the given regex paths from scaffolding.

Matching occurs before template evaluation and .tmpl suffix removal.

func Extend

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
cmd
scaffolder command
extensions

Jump to

Keyboard shortcuts

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