Documentation
¶
Overview ¶
Package lua provides Lua scripting engine integration
Index ¶
- Constants
- func IsHookTimeout(err error) bool
- func WithTimeout[T any](ctx context.Context, L *glua.LState, d time.Duration, fn func() (T, error)) (T, error)
- type Engine
- func (e *Engine) ClearForTests()
- func (e *Engine) CollectVariableTransformers() []contracts.VariableTransformer
- func (e *Engine) Emit(ctx context.Context, event string, luaCtx any) error
- func (e *Engine) Init() error
- func (e *Engine) LanguagesForPath(path string) []string
- func (e *Engine) LoadLanguages(ctx context.Context, langs []string) error
- func (e *Engine) LoadRunWorkflow(ctx context.Context, path string) (bool, error)
- func (e *Engine) ResetForTests()
- func (e *Engine) RunFile(ctx context.Context, path string) error
- func (e *Engine) RunFileWithCtx(ctx context.Context, path string, luaCtx any) error
- func (e *Engine) RunWorkflowCleanup(ctx context.Context, workflowCtx any) error
- func (e *Engine) RunWorkflowPrepare(ctx context.Context, workflowCtx any) error
- func (e *Engine) RunWorkflowRepo(ctx context.Context, workflowCtx any) error
- func (e *Engine) SetExtensionsMap(m map[string][]string)
- type HookEdit
- type HookHost
- type HookScript
- type Options
- type Transformer
Constants ¶
const ( HookEnable = "enable" HookDisable = "disable" HookUpdate = "update" )
Lifecycles a plugin hook registers a callback for. The names are contract surface: hooks/init.lua calls dk.hooks.<lifecycle>(fn), and the CLI runs the callback of the lifecycle the command it is in belongs to.
const ( HookEditInsertXML = "insert_xml" HookEditTextBlock = "text_block" HookEditCreateFile = "create_file" )
Edit operations a hook requests through ctx.edits. They mirror the operation families of the edit-plan executor, which is what validates, previews, and applies them; the runtime only collects what a callback asked for, so a hook never writes a file itself.
Variables ¶
This section is empty.
Functions ¶
func IsHookTimeout ¶
IsHookTimeout reports whether err is the wall-clock stop of a hook rather than a failure of the script itself, which is what lets a caller say that a hook ran too long instead of that it was wrong.
Types ¶
type Engine ¶
Engine is a Lua scripting engine using gopher-lua (enabled with build tag 'lua')
func (*Engine) ClearForTests ¶
func (e *Engine) ClearForTests()
ClearForTests resets mutable state used by tests (transformers and maps)
func (*Engine) CollectVariableTransformers ¶
func (e *Engine) CollectVariableTransformers() []contracts.VariableTransformer
CollectVariableTransformers returns Go adapters for all registered Lua transformers
func (*Engine) LanguagesForPath ¶
LanguagesForPath resolves the effective languages for a given file path. Always includes "common" first. If a mapping is configured, applies specific globs first then '*' fallback; otherwise uses all loaded packs.
func (*Engine) LoadLanguages ¶
LoadLanguages loads the given language packs in order (in addition to 'common').
func (*Engine) LoadRunWorkflow ¶
LoadRunWorkflow loads a run workflow script and reports whether workflow callbacks were registered.
func (*Engine) ResetForTests ¶
func (e *Engine) ResetForTests()
ResetForTests fully resets the engine for testing (including Lua state)
func (*Engine) RunFileWithCtx ¶
RunFileWithCtx loads a Lua script and, if it returns a function, calls it with dk and a context available under dk.run
func (*Engine) RunWorkflowCleanup ¶
RunWorkflowCleanup executes all registered dk.run.cleanup callbacks.
func (*Engine) RunWorkflowPrepare ¶
RunWorkflowPrepare executes all registered dk.run.prepare callbacks.
func (*Engine) RunWorkflowRepo ¶
RunWorkflowRepo executes the registered dk.run.each callback for one repository.
func (*Engine) SetExtensionsMap ¶
SetExtensionsMap stores extension-to-language mapping used by higher-level file hook context creation.
type HookEdit ¶
HookEdit is one operation a hook collected through ctx.edits. It carries no behaviour: the executor decides what a family means, so the runtime and the declarative front end hand it the same data.
type HookHost ¶
type HookHost interface {
// Fragment reads a file the hook repository ships next to its script.
Fragment(name string) (string, error)
// Glob returns the working-tree files matching a pattern, read-only.
Glob(pattern string) ([]string, error)
// Read returns the content of one working-tree file, read-only.
Read(path string) (string, error)
// Log reports one message of the hook to the maintainer.
Log(message string)
}
HookHost is everything a hook callback may reach outside its own script. Every capability is a method here rather than an ambient library, so what a hook can do is decided by the CLI that lends it the host.
type HookScript ¶
type HookScript struct {
// contains filtered or unexported fields
}
HookScript is a loaded hooks/init.lua with the lifecycle callbacks it registered. Loading runs the top level of the script, which is what performs the registrations, so a caller that must not execute untrusted code has to decide before it loads rather than before it runs a callback.
func LoadHookScript ¶
func LoadHookScript(ctx context.Context, name, source string, timeout time.Duration) (*HookScript, error)
LoadHookScript runs source in a sandboxed Lua state and returns the script with the lifecycle callbacks it registered. name is the path the script was fetched from and becomes the chunk name, so a failure names the file and the line a maintainer can look at.
func (*HookScript) Close ¶
func (s *HookScript) Close()
Close releases the Lua state. A script is loaded per command, so nothing outlives the run that asked for it.
func (*HookScript) Registers ¶
func (s *HookScript) Registers(lifecycle string) bool
Registers reports whether the script carries a callback for one lifecycle, which is what decides whether a hook or the declarative edits of the same repository produce the plan.
type Options ¶
type Options struct {
// Sandbox controls whether process/file-system standard libraries are hidden.
Sandbox bool
// ScriptTimeout limits each run script and each dk.run callback. Zero keeps the default.
ScriptTimeout time.Duration
}
Options configures a Lua engine instance.
type Transformer ¶
Transformer adapts a Lua transform function to Go VariableTransformer
func (*Transformer) GetTokenName ¶
func (lt *Transformer) GetTokenName() string
GetTokenName returns the token name for this transformer
func (*Transformer) Transform ¶
func (lt *Transformer) Transform(className string) string
Transform applies the Lua transformation function to a class name