gen

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: GPL-3.0 Imports: 25 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, listing the files it wrote there. A file it does not list is someone else's, and tdl neither overwrites nor removes it.

View Source
const WatchInterval = time.Second

WatchInterval is how often a watched file is polled.

Variables

This section is empty.

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 removes the files the marker in out lists, and any directory that leaves empty, and empties the list.

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, written []string) error

Mark adds written to the files the marker in out lists.

func Owned

func Owned(out string) (map[string]bool, error)

Owned returns the files the marker in out lists, as paths joined to out. A directory with no marker owns nothing.

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, one climbing out with "..", or a file already there that the marker does not list 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
	Expected []string // what a verify run would write

	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 Orphaned added in v0.3.0

func Orphaned(out string, expected map[string]bool) ([]Stale, error)

Orphaned lists the files the marker in out lists that are still there and that nothing would write. An unlisted file is not tdl's.

func Verify

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

Verify compares a response against what is on disk without writing, and returns the paths it would write. The backend still produces full contents on a dry run. Orphaned finds what nothing would write.

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. A relative directive is relative to the file declaring the block, as an import or an include is.

Jump to

Keyboard shortcuts

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