gen

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package gen is the compiler side of the plugin protocol: which backends exist, how a request is built, and what happens to the files that come back.

The `plugin` package is the surface a backend author sees.

Index

Constants

View Source
const CommandPrefix = "tdl-gen-"

CommandPrefix is prefixed to a target name to find its executable on PATH, the way git and protoc find their subcommands.

View Source
const DefaultTimeout = 2 * time.Minute

DefaultTimeout bounds how long a plugin has to answer, so a hung plugin fails with a diagnosis.

View Source
const MarkerName = ".tdl-output"

MarkerName is the file tdl drops in a directory it writes to, so --clean knows the directory is its own.

View Source
const WatchInterval = time.Second

WatchInterval is how often a watched file is polled.

Variables

View Source
var ErrNotOurs = errors.New("the output directory was not written by tdl")

ErrNotOurs is returned when --clean is asked to empty a directory that has files but no marker.

Functions

func Builtin

func Builtin(name string) (plugin.Backend, bool)

Builtin returns the backend compiled in under name.

func BuiltinNames

func BuiltinNames() []string

BuiltinNames lists the compiled-in backends, sorted.

func CheckDirectives

func CheckDirectives(target string, model *ir.Model, desc plugin.Description) []*plugin.Diagnostic

CheckDirectives compares the directives a target block uses against what its backend says it understands.

A declared directive with the wrong number or kind of arguments is an error, reported before anything is generated. An undeclared directive is a warning and is passed through anyway, since a backend may handle more than it advertises.

func Clean

func Clean(out string) ([]string, error)

Clean empties an output directory tdl owns, leaving the marker. A missing or empty directory is fine; one with contents and no marker is ErrNotOurs.

func Fatal added in v0.1.8

func Fatal(diags []*plugin.Diagnostic) bool

Fatal reports whether any diagnostic is an error rather than a warning.

func Mark

func Mark(out string) error

Mark writes the ownership marker into out.

func Owned

func Owned(out string) bool

Owned reports whether out carries the marker.

func Resolve

func Resolve(name string) (plugin.Backend, error)

Resolve returns the backend serving a target: the compiled-in one if there is one, otherwise tdl-gen-<name> on PATH. A built-in name cannot be shadowed from PATH.

func Watch

func Watch(done <-chan struct{}, path string, onChange func())

Watch calls onChange whenever path's contents change, until done is closed. It compares contents rather than modification time, so a rewrite with no change does not trigger. A poll landing mid-save may see a truncated file; the next poll corrects it.

func Write

func Write(out string, files []*plugin.File) ([]string, error)

Write puts a response's files under out. A path is relative to out; an absolute one or one climbing out with ".." is refused before anything is written.

Types

type Mode

type Mode int

Mode is what a run does with the files a backend returns.

const (
	// ModeWrite writes them.
	ModeWrite Mode = iota
	// ModeVerify compares them against disk and writes nothing.
	ModeVerify
	// ModeClean empties the output directory first, then writes.
	ModeClean
)

type Result

type Result struct {
	Target      string
	Written     []string
	Removed     []string
	Stale       []Stale
	Diagnostics []*plugin.Diagnostic
}

Result is what one target produced.

func Run

func Run(ctx context.Context, backend plugin.Backend, target Target, model *ir.Model, mode Mode) (Result, error)

Run generates one target and does what mode says with the result.

type Session

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

Session is a plugin kept alive across generations. A plugin that declared reuse serves many requests on one connection and must treat each as independent.

func Open

func Open(ctx context.Context, sub *Subprocess) (*Session, error)

Open starts a plugin and holds the connection if it declared reuse; otherwise each generation gets a fresh process. A failed handshake is an error.

func (*Session) Close

func (s *Session) Close()

Close stops a held plugin.

func (*Session) Describe

func (s *Session) Describe() plugin.Description

Describe reports what the plugin said about itself when it was opened.

func (*Session) Generate

func (s *Session) Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error)

Generate serves one request, over the held connection when there is one.

func (*Session) Restarts

func (s *Session) Restarts() int

Restarts counts how many times the held plugin was replaced because its binary changed.

func (*Session) Reused

func (s *Session) Reused() bool

Reused reports whether this session is holding a connection.

type Stale

type Stale struct {
	Path   string
	Reason string
}

Stale describes one way an output directory disagrees with what a backend would write.

func Verify

func Verify(out string, files []*plugin.File) ([]Stale, error)

Verify compares a response against what is on disk without writing. The backend still produces full contents on a dry run.

type Subprocess

type Subprocess struct {
	Name    string
	Path    string
	Timeout time.Duration
}

Subprocess is a backend running as tdl-gen-<name>. It implements plugin.Backend, so callers treat a plugin and a compiled-in backend the same way.

func Find

func Find(name string) (*Subprocess, error)

Find locates the executable for a target name.

func (*Subprocess) Describe

func (s *Subprocess) Describe() plugin.Description

Describe starts the plugin, shakes hands, and stops it again. Generate starts a process of its own.

func (*Subprocess) Generate

func (s *Subprocess) Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error)

Generate runs one request through the plugin.

type Target

type Target struct {
	Name  string
	Out   string
	Block *ir.TargetBlock
}

Target is one target block resolved into what a backend needs.

func Targets

func Targets(model *ir.Model, override string) ([]Target, error)

Targets returns the target blocks in a model, with their output directories read from each block's `out` directive unless override is set.

Jump to

Keyboard shortcuts

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