engine

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadTemplateFiles

func LoadTemplateFiles(dirPath string) (map[string]string, error)

LoadTemplateFiles loads all files from a directory as templates.

Types

type Bundle

type Bundle struct {
	Name          string         `json:"name" yaml:"name"`
	Description   string         `json:"description,omitempty" yaml:"description,omitempty"`
	Vars          map[string]any `json:"vars,omitempty" yaml:"vars,omitempty"`
	SelfContained bool           `json:"self_contained,omitempty" yaml:"self_contained,omitempty"`
	Requires      []string       `json:"requires,omitempty" yaml:"requires,omitempty"`
	Generators    []GeneratorRef `json:"generators" yaml:"generators"`
}

type ConflictError

type ConflictError struct {
	Files []string
}

ConflictError is returned by Generate when OnExistingFail (the default) is in effect and one or more target files already exist.

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Core

type Core struct {
	// contains filtered or unexported fields
}

func NewCore

func NewCore(parser TemplateParser, fwr writer.FileWriter, out io.Writer) Core

NewCore creates a Core engine with injected dependencies. This is the preferred constructor, enabling dependency injection for testing.

func (*Core) Generate

func (c *Core) Generate(data Data) (GenerateResult, error)

Generate generates code from templates using the Gonja-based engine.

type Data

type Data struct {
	Name         string
	RawMeta      []string
	ScaffoldVars map[string]any   // Variables from scaffold-time .tagconfig.json
	OnExisting   OnExistingPolicy // behaviour when a create action targets an existing file
}

type FileOpDetail

type FileOpDetail struct {
	Path string // destination path
	Op   string // "created", "skipped", "overwritten", "modified"
}

FileOpDetail records the outcome for a single file.

type GenerateResult

type GenerateResult struct {
	Created     int            // files newly created
	Skipped     int            // existing files skipped (--on-existing=skip)
	Overwritten int            // existing files overwritten (--on-existing=overwrite)
	Modified    int            // files appended to or injected into
	Details     []FileOpDetail // per-file breakdown, always populated
}

GenerateResult summarises the outcome of a generation run.

func (*GenerateResult) Add

func (r *GenerateResult) Add(other GenerateResult)

Add accumulates the counters and file-operation details from other into r.

type Generator

type Generator interface {
	Generate(data Data) (GenerateResult, error)
}

Generator defines the interface for the code generation engine. It allows commands to work with any implementation that can generate code from template data.

func NewGenerator

func NewGenerator(dryRun bool, dirPath, sharedPath string, out io.Writer) (Generator, error)

NewGenerator creates a Generator with the standard pipeline. It creates a new template engine, loads templates, and wires up the parser and writer.

func NewGeneratorWithEngine

func NewGeneratorWithEngine(tmplEngine *template.Engine, dryRun bool, dirPath, sharedPath string, out io.Writer) (Generator, error)

NewGeneratorWithEngine creates a Generator using an existing template engine. This allows sharing a template engine (and its cache) across multiple generators, such as when running a bundle of generators.

func NewGeneratorWithRecorder

func NewGeneratorWithRecorder(tmplEngine *template.Engine, dryRun bool, dirPath, sharedPath string, rec *history.Recorder, out io.Writer) (Generator, error)

NewGeneratorWithRecorder creates a Generator that records file operations into rec. It is identical to NewGeneratorWithEngine but wraps the FileWriter with a history.RecordingFileWriter.

type GeneratorRef

type GeneratorRef struct {
	Name string `json:"name" yaml:"name"`
}

GeneratorRef is a reference to a generator by name within a bundle configuration.

type InputData

type InputData struct {
	Name         string            // Primary name value
	Meta         map[string]string // Key-value metadata from --meta flags
	ScaffoldVars map[string]any    // Variables from scaffold-time .tagconfig.json
}

InputData represents the input provided by the engine for template parsing.

type OnExistingPolicy

type OnExistingPolicy string

OnExistingPolicy controls behavior when a create action targets a file that already exists.

const (
	// OnExistingDefault is the zero-value, treated the same as OnExistingFail.
	OnExistingDefault OnExistingPolicy = ""
	// OnExistingFail causes the generation to fail with an error listing conflicts.
	OnExistingFail OnExistingPolicy = "fail"
	// OnExistingSkip silently skips writing files that already exist.
	OnExistingSkip OnExistingPolicy = "skip"
	// OnExistingOverwrite replaces existing files (with backup via history if available).
	OnExistingOverwrite OnExistingPolicy = "overwrite"
)

func (OnExistingPolicy) IsValid

func (p OnExistingPolicy) IsValid() bool

IsValid returns true if the policy is one of the recognised values.

type ParseData

type ParseData struct {
	Action        template.Action    // File operation: Create, Append, Inject, or OpenAPI
	InjectClause  types.InjectClause // Before or After (for inject action)
	InjectMatcher string
	Meta          map[string]string // User-provided metadata from --meta flags
	Notes         string
	Validate      bool // Run OpenAPI validation after merge (openapi action only)
}

ParseData holds the parsing configuration and user input.

type TemplateData

type TemplateData struct {
	Name   string // Template name/path
	To     string // Output file path
	Output []byte // Rendered template content
	ParseData
}

TemplateData represents the parsed and rendered result for a single template.

type TemplateParser

type TemplateParser struct {
	// contains filtered or unexported fields
}

TemplateParser wraps the Gonja template engine for parsing TAG templates.

func NewParserWithExecutor

func NewParserWithExecutor(executor template.TemplateExecutor, templates, sharedTemplates map[string]string) TemplateParser

NewParserWithExecutor creates a TemplateParser using the provided TemplateExecutor. The caller is responsible for configuring the executor (e.g., setting loaders for shared template resolution) before passing it in.

func (*TemplateParser) Parse

func (te *TemplateParser) Parse(input InputData) ([]TemplateData, error)

Parse processes all loaded templates with the given input data. It returns a slice of TemplateData sorted by action (Create, Inject, Append).

Jump to

Keyboard shortcuts

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