lua

package
v0.0.0-...-8e695e4 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lua provides Lua scripting engine integration

Index

Constants

View Source
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.

View Source
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

func IsHookTimeout(err error) bool

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.

func WithTimeout

func WithTimeout[T any](ctx context.Context, L *glua.LState, d time.Duration, fn func() (T, error)) (T, error)

WithTimeout executes a function with a timeout

Types

type Engine

type Engine struct {
	L *glua.LState
	// contains filtered or unexported fields
}

Engine is a Lua scripting engine using gopher-lua (enabled with build tag 'lua')

func NewEngine

func NewEngine(options ...Options) *Engine

NewEngine creates a new Lua scripting engine instance

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) Emit

func (e *Engine) Emit(ctx context.Context, event string, luaCtx any) error

Emit triggers event handlers registered via dk.hooks.on()

func (*Engine) Init

func (e *Engine) Init() error

Init initializes the Lua engine and sets up the execution environment

func (*Engine) LanguagesForPath

func (e *Engine) LanguagesForPath(path string) []string

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

func (e *Engine) LoadLanguages(ctx context.Context, langs []string) error

LoadLanguages loads the given language packs in order (in addition to 'common').

func (*Engine) LoadRunWorkflow

func (e *Engine) LoadRunWorkflow(ctx context.Context, path string) (bool, error)

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) RunFile

func (e *Engine) RunFile(ctx context.Context, path string) error

RunFile loads and executes a Lua script file

func (*Engine) RunFileWithCtx

func (e *Engine) RunFileWithCtx(ctx context.Context, path string, luaCtx any) error

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

func (e *Engine) RunWorkflowCleanup(ctx context.Context, workflowCtx any) error

RunWorkflowCleanup executes all registered dk.run.cleanup callbacks.

func (*Engine) RunWorkflowPrepare

func (e *Engine) RunWorkflowPrepare(ctx context.Context, workflowCtx any) error

RunWorkflowPrepare executes all registered dk.run.prepare callbacks.

func (*Engine) RunWorkflowRepo

func (e *Engine) RunWorkflowRepo(ctx context.Context, workflowCtx any) error

RunWorkflowRepo executes the registered dk.run.each callback for one repository.

func (*Engine) SetExtensionsMap

func (e *Engine) SetExtensionsMap(m map[string][]string)

SetExtensionsMap stores extension-to-language mapping used by higher-level file hook context creation.

type HookEdit

type HookEdit struct {
	Kind    string
	Path    string
	Anchor  string
	Content string
}

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.

func (*HookScript) Run

func (s *HookScript) Run(ctx context.Context, lifecycle string, host HookHost) ([]HookEdit, error)

Run calls the callback of one lifecycle and returns the operations it collected. A lifecycle without a callback yields no operation and no error, so a script that only handles enabling stays silent everywhere else.

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

type Transformer struct {
	L *glua.LState
	// contains filtered or unexported fields
}

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

Jump to

Keyboard shortcuts

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