Documentation
¶
Overview ¶
Package core provides headless Risor execution without GUI dependencies. This enables static compilation and smaller binaries for CLI tools and servers.
Index ¶
- Constants
- func GetAppVersion() string
- func SetAppVersion(version string)
- type Context
- func (cb *Context) EnabledModules() map[string]bool
- func (cb *Context) Eval(script string) (any, error)
- func (cb *Context) EvalWithImports(script string, fetchFunc func(path string) (string, error)) (any, error)
- func (cb *Context) ImportScript(source string) error
- func (cb *Context) LoadScript(script string)
- type Option
- type Requirements
- type ScriptRunner
- type VersionInfo
Constants ¶
const Version = "0.5.0"
Version is the current version of the fynerisor library
Variables ¶
This section is empty.
Functions ¶
func GetAppVersion ¶
func GetAppVersion() string
GetAppVersion returns the embedding application's version. Returns empty string if not set.
func SetAppVersion ¶
func SetAppVersion(version string)
SetAppVersion sets the embedding application's version. This version is used when scripts call require(["vX.Y.Z"]) to check compatibility with the embedding application, not fynerisor itself.
Example:
func main() {
fynerisor.SetAppVersion("1.2.3")
w := fynerisor.NewApp("My App")
// Scripts can now use: require(["v1.2"])
w.LoadScript(script)
w.ShowAndRun()
}
Scripts should use semantic versioning format: "v1.2.3" or "v1.2"
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context builds Risor execution contexts for non-GUI applications. It provides access to modules (HTTP, SQL, OS, etc.) and import functionality without requiring Fyne or any GUI dependencies.
This enables static compilation for headless scripts, CLI tools, or server applications that need Risor scripting with modules.
func NewContext ¶
NewContext creates a new Risor context for non-GUI applications.
Parameters:
- opts: Optional configuration using functional options (WithHTTP, WithSQL, etc.)
Returns:
- *Context: Execution context for Risor scripts
Example:
ctx := core.NewContext(
core.WithHTTP(),
core.WithSQL(),
core.WithOS(),
)
script := `
require(["@http", "@sql"])
let data = http.get("https://api.example.com/data").json()
print(data)
`
result, err := ctx.Eval(script)
func (*Context) EnabledModules ¶
EnabledModules returns a map of enabled module names.
func (*Context) Eval ¶
Eval evaluates a Risor script and returns the result.
Parameters:
- script: The Risor script source code
Returns:
- any: The script result (converted to Go types)
- error: Any evaluation error
Example:
// Direct eval
ctx := core.NewContext(core.WithHTTP())
result, err := ctx.Eval(`http.get("https://example.com").status`)
// With imports
ctx.ImportScript("utils.risor")
ctx.LoadScript(`let result = myUtil(42)`)
result, err := ctx.Eval("")
func (*Context) EvalWithImports ¶
func (cb *Context) EvalWithImports(script string, fetchFunc func(path string) (string, error)) (any, error)
EvalWithImports analyzes the script for imports, loads them in order, then evaluates the main script. This method creates a shared Risor context so imported scripts can define functions/variables used by the main script.
Parameters:
- script: The Risor script source code
- fetchFunc: Function to fetch import sources by path/URL
Returns:
- any: The script result (converted to Go types)
- error: Any evaluation error
Example:
ctx := core.NewContext(core.WithHTTP())
fetchFunc := func(path string) (string, error) {
data, err := os.ReadFile(path)
return string(data), err
}
script := `
import("utils.risor")
require("@http")
let result = myUtil(42)
`
result, err := ctx.EvalWithImports(script, fetchFunc)
func (*Context) ImportScript ¶
ImportScript loads a script from a path or URL and adds it to the import list. The script will be executed before the main script when Eval() is called.
Parameters:
- source: Path to a local file or HTTP(S) URL
Returns:
- error: Any error encountered while fetching the script
Example:
ctx := core.NewContext(core.WithHTTP())
ctx.ImportScript("utils.risor")
ctx.ImportScript("https://example.com/helpers.risor")
result, err := ctx.Eval(mainScript)
func (*Context) LoadScript ¶
LoadScript sets the main script to be executed. Call this after all ImportScript() calls.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a Context during creation.
func WithAppName ¶
WithAppName sets the application name exposed to Risor scripts via app.name. This allows scripts to detect which application is running them.
Example:
ctx := core.NewContext(
core.WithAppName("myapp"),
)
Usage in script:
if (app.name == "myapp") {
print("Running in myapp")
}
func WithFilepath ¶
func WithFilepath() Option
WithFilepath enables the filepath module for file path manipulation from Risor scripts.
func WithGlobals ¶
WithGlobals adds custom global objects to the Risor script environment. The globals parameter should be created using risor.WithEnv().
Example:
customGlobals := map[string]any{
"myAPI": myAPIObject,
}
ctx := core.NewContext(
core.WithGlobals(risor.WithEnv(customGlobals)),
)
func WithHTTP ¶
func WithHTTP() Option
WithHTTP enables the HTTP module for making HTTP requests from Risor scripts. The module provides functions: get, post, put, delete, and fetch.
Example:
ctx := core.NewContext(core.WithHTTP())
Usage in script:
let response = http.get("https://api.example.com/data")
print(response.status, response.body)
func WithIO ¶
func WithIO() Option
WithIO enables the IO module for file I/O operations from Risor scripts. The module provides functions: cp and read_all.
Example:
ctx := core.NewContext(core.WithIO())
Usage in script:
io.cp("source.txt", "dest.txt")
let content = io.read_all("file.txt")
func WithOS ¶
func WithOS() Option
WithOS enables the OS module for accessing OS functionality from Risor scripts. The module provides functions: goos, current_user, open_browser, read_file, write_file, and read_dir.
Example:
ctx := core.NewContext(core.WithOS())
Usage in script:
let platform = os.goos()
let user = os.current_user()
os.write_file("test.txt", "Hello!")
func WithSQL ¶
func WithSQL() Option
WithSQL enables the SQL module for database connectivity from Risor scripts. Supports: MySQL, PostgreSQL, SQLite, SQL Server
Example:
ctx := core.NewContext(core.WithSQL())
Usage in script:
let conn = sql.connect("sqlite3::memory:")
conn.exec("CREATE TABLE users (id INT, name TEXT)")
let rows = conn.query("SELECT * FROM users").collect()
func WithStrings ¶
func WithStrings() Option
WithStrings enables the strings module for string manipulation from Risor scripts.
type Requirements ¶
type Requirements struct {
MinVersion string // Minimum version (e.g., "0.2.0")
ExactVersion string // Exact version if ==v syntax used
RequiredModules []string // List of required modules (e.g., ["sql", "http"])
Imports []string // List of import paths/URLs (e.g., ["utils.risor", "http://..."])
Raw []string // Raw requirement strings
}
Requirements represents parsed script requirements
func AnalyzeRequirements ¶
func AnalyzeRequirements(script string) (*Requirements, error)
AnalyzeRequirements parses a Risor script and extracts all require() calls. This function is useful for external applications that need to understand script dependencies before execution.
It compiles the script and inspects require() calls without executing the full script logic.
Example:
script := `
require(["v0.2", "@sql", "@http"])
let btn = widget.NewButton("Hello", () => {})
`
reqs, err := fynerisor.AnalyzeRequirements(script)
if err != nil {
log.Fatal(err)
}
fmt.Println("Requires version:", reqs.MinVersion)
fmt.Println("Requires modules:", reqs.RequiredModules)
func (*Requirements) RequiresGUI ¶
func (r *Requirements) RequiresGUI() bool
RequiresGUI returns true if the script requires a GUI window (@gui)
func (*Requirements) String ¶
func (r *Requirements) String() string
String returns a human-readable representation of requirements
type ScriptRunner ¶
type ScriptRunner struct {
// contains filtered or unexported fields
}
ScriptRunner manages script execution with import support. Shared by both Window and ContextBuilder.
func NewScriptRunner ¶
func NewScriptRunner(globals []risor.Option) *ScriptRunner
NewScriptRunner creates a new script runner with the given globals.
func (*ScriptRunner) Clear ¶
func (sr *ScriptRunner) Clear()
Clear clears all loaded scripts (both imports and main script).
func (*ScriptRunner) Eval ¶
func (sr *ScriptRunner) Eval() (any, error)
Eval executes all imported scripts followed by the main script. Returns the result of the final script.
Returns:
- any: The result of script execution
- error: Any execution error
func (*ScriptRunner) ImportScript ¶
func (sr *ScriptRunner) ImportScript(source string) error
ImportScript loads a script from a path or URL and adds it to the import list. The script will be executed before the main script when Eval() is called.
Parameters:
- source: Path to a local file or HTTP(S) URL
Returns:
- error: Any error encountered while fetching the script
Example:
runner.ImportScript("utils.risor")
runner.ImportScript("https://example.com/helpers.risor")
func (*ScriptRunner) LoadScript ¶
func (sr *ScriptRunner) LoadScript(script string)
LoadScript adds the main script to be executed. This should be called after all ImportScript() calls.
type VersionInfo ¶
type VersionInfo struct {
Version string // Fynerisor library version
AppVersion string // Embedding application version (if set)
RisorCompat string // Compatible Risor version
FyneCompat string // Compatible Fyne version
}
VersionInfo provides detailed version information
func GetVersion ¶
func GetVersion() VersionInfo
GetVersion returns version information for this fynerisor release