Implement loading lua from separate files via @
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -205,11 +207,62 @@ func PrependLuaAssignment(luaExpr string) string {
|
||||
return luaExpr
|
||||
}
|
||||
|
||||
// LoadExternalLuaFile loads Lua code from an external file
|
||||
func LoadExternalLuaFile(luaPath string, sourceDir string) (string, error) {
|
||||
loadLuaLogger := processorLogger.WithPrefix("LoadExternalLuaFile").WithField("luaPath", luaPath).WithField("sourceDir", sourceDir)
|
||||
loadLuaLogger.Debug("Loading external Lua file")
|
||||
|
||||
// Resolve path: if relative, resolve relative to sourceDir; if absolute, use as-is
|
||||
var resolvedPath string
|
||||
if filepath.IsAbs(luaPath) {
|
||||
resolvedPath = luaPath
|
||||
} else {
|
||||
if sourceDir == "" {
|
||||
// No source directory, use current working directory
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
loadLuaLogger.Error("Failed to get current working directory: %v", err)
|
||||
return "", fmt.Errorf("failed to get current working directory: %w", err)
|
||||
}
|
||||
resolvedPath = filepath.Join(cwd, luaPath)
|
||||
} else {
|
||||
resolvedPath = filepath.Join(sourceDir, luaPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize path
|
||||
resolvedPath = filepath.Clean(resolvedPath)
|
||||
loadLuaLogger.Debug("Resolved Lua file path: %q", resolvedPath)
|
||||
|
||||
// Read the file
|
||||
content, err := os.ReadFile(resolvedPath)
|
||||
if err != nil {
|
||||
loadLuaLogger.Error("Failed to read Lua file %q: %v", resolvedPath, err)
|
||||
return "", fmt.Errorf("failed to read Lua file %q: %w", luaPath, err)
|
||||
}
|
||||
|
||||
loadLuaLogger.Debug("Successfully loaded %d bytes from Lua file %q", len(content), resolvedPath)
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// BuildLuaScript prepares a Lua expression from shorthand notation
|
||||
func BuildLuaScript(luaExpr string) string {
|
||||
func BuildLuaScript(luaExpr string, sourceDir string) string {
|
||||
buildLuaScriptLogger := processorLogger.WithPrefix("BuildLuaScript").WithField("inputLuaExpr", luaExpr)
|
||||
buildLuaScriptLogger.Debug("Building full Lua script from expression")
|
||||
|
||||
// Check if this is an external Lua file reference
|
||||
if strings.HasPrefix(luaExpr, "@") {
|
||||
luaPath := strings.TrimPrefix(luaExpr, "@")
|
||||
externalLua, err := LoadExternalLuaFile(luaPath, sourceDir)
|
||||
if err != nil {
|
||||
buildLuaScriptLogger.Error("Failed to load external Lua file: %v", err)
|
||||
// Return error script that will fail at runtime
|
||||
return fmt.Sprintf(`error("Failed to load external Lua file: %v")`, err)
|
||||
}
|
||||
luaExpr = externalLua
|
||||
buildLuaScriptLogger.Debug("Loaded external Lua file, %d characters", len(luaExpr))
|
||||
}
|
||||
|
||||
// Perform $var substitutions from globalVariables
|
||||
luaExpr = replaceVariables(luaExpr)
|
||||
|
||||
@@ -228,10 +281,23 @@ func BuildLuaScript(luaExpr string) string {
|
||||
}
|
||||
|
||||
// BuildJSONLuaScript prepares a Lua expression for JSON mode
|
||||
func BuildJSONLuaScript(luaExpr string) string {
|
||||
func BuildJSONLuaScript(luaExpr string, sourceDir string) string {
|
||||
buildJSONLuaScriptLogger := processorLogger.WithPrefix("BuildJSONLuaScript").WithField("inputLuaExpr", luaExpr)
|
||||
buildJSONLuaScriptLogger.Debug("Building full Lua script for JSON mode from expression")
|
||||
|
||||
// Check if this is an external Lua file reference
|
||||
if strings.HasPrefix(luaExpr, "@") {
|
||||
luaPath := strings.TrimPrefix(luaExpr, "@")
|
||||
externalLua, err := LoadExternalLuaFile(luaPath, sourceDir)
|
||||
if err != nil {
|
||||
buildJSONLuaScriptLogger.Error("Failed to load external Lua file: %v", err)
|
||||
// Return error script that will fail at runtime
|
||||
return fmt.Sprintf(`error("Failed to load external Lua file: %v")`, err)
|
||||
}
|
||||
luaExpr = externalLua
|
||||
buildJSONLuaScriptLogger.Debug("Loaded external Lua file, %d characters", len(luaExpr))
|
||||
}
|
||||
|
||||
// Perform $var substitutions from globalVariables
|
||||
luaExpr = replaceVariables(luaExpr)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user