summaryrefslogtreecommitdiff
path: root/xmake/plugins/lua/main.lua
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-01-31 05:42:37 +0300
committerSaikari <[email protected]>2026-01-31 05:42:37 +0300
commit4630ca802874f6d45e447a7ab79c1a54b03d866a (patch)
treefaaa20ffbf1b81c9079d37ac62dc6440892fb934 /xmake/plugins/lua/main.lua
parent51a68ee33918b95811420beb6a56673a83abe6cc (diff)
add support for running Lua scripts from stdin and enhance error handling
Diffstat (limited to 'xmake/plugins/lua/main.lua')
-rw-r--r--xmake/plugins/lua/main.lua62
1 files changed, 54 insertions, 8 deletions
diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua
index 2b0a9efe2..3da4ee4b8 100644
--- a/xmake/plugins/lua/main.lua
+++ b/xmake/plugins/lua/main.lua
@@ -54,14 +54,60 @@ function main()
-- run script
local script = option.get("script")
- if script then
- run_script(script, {
- curdir = os.workingdir(),
- verbose = option.get("verbose"),
- diagnosis = option.get("diagnosis"),
- command = option.get("command"),
- arguments = option.get("arguments"),
- deserialize = option.get("deserialize")})
+ local arguments = option.get("arguments")
+ local from_stdin = option.get("from_stdin") or option.get("from-stdin")
+ if script or from_stdin then
+
+ -- run script from stdin?
+ local scriptfile_stdin
+ if script == "-" or from_stdin then
+ local script_content = io.stdin:read("*a")
+ if script_content then
+ scriptfile_stdin = os.tmpfile("xmake_lua_stdin") .. ".lua"
+ io.writefile(scriptfile_stdin, script_content)
+ if from_stdin and script and script ~= "-" then
+ arguments = arguments or {}
+ table.insert(arguments, 1, script)
+ end
+ script = scriptfile_stdin
+ end
+ end
+
+ -- enable diagnosis to get the stack traceback
+ local get_old = option.get
+ option.get = function (name)
+ if name == "diagnosis" then
+ return true
+ end
+ return get_old(name)
+ end
+
+ try {
+ function ()
+ if script then
+ run_script(script, {
+ curdir = os.workingdir(),
+ verbose = option.get("verbose"),
+ diagnosis = option.get("diagnosis"),
+ command = option.get("command"),
+ arguments = arguments,
+ deserialize = option.get("deserialize")})
+ end
+ end,
+ catch {
+ function (errors)
+ raise(errors)
+ end
+ },
+ finally {
+ function ()
+ option.get = get_old
+ if scriptfile_stdin then
+ os.rm(scriptfile_stdin)
+ end
+ end
+ }
+ }
else
-- enter interactive mode
sandbox.interactive()