From 4630ca802874f6d45e447a7ab79c1a54b03d866a Mon Sep 17 00:00:00 2001 From: Saikari Date: Sat, 31 Jan 2026 05:42:37 +0300 Subject: add support for running Lua scripts from stdin and enhance error handling --- xmake/plugins/lua/xmake.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 8ef6c3573..c8ebc8ba3 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -45,6 +45,7 @@ task("lua") {'l', "list" , "k" , nil , "List all scripts." } , {'c', "command" , "k" , nil , "Run script as command" } , {'d', "deserialize" , "kv" , nil , "Deserialize arguments starts with given prefix" } + , {nil, "from-stdin" , "k" , nil , "Run script from stdin" } , {nil, "script" , "v" , nil , "Run the given lua script name, file or module and enter interactive mode if no given script.", "e.g.", " - xmake lua (enter interactive mode)", -- cgit v1.3.1 From 5e59fa1d59486438337290e3d528561791843f7c Mon Sep 17 00:00:00 2001 From: Saikari Date: Sat, 31 Jan 2026 16:51:38 +0300 Subject: refactor: improve stdin command handling with detailed usage examples --- xmake/core/base/os.lua | 5 ++++- xmake/plugins/lua/xmake.lua | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 12bc30c1d..9332f2c61 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -1187,7 +1187,10 @@ function os.isexec(filepath) end end elseif os.isfile(filepath) then - return os._access(filepath, "x") + if os._access then + return os._access(filepath, "x") + end + return true end return false end diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index c8ebc8ba3..527ecb19e 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -45,7 +45,21 @@ task("lua") {'l', "list" , "k" , nil , "List all scripts." } , {'c', "command" , "k" , nil , "Run script as command" } , {'d', "deserialize" , "kv" , nil , "Deserialize arguments starts with given prefix" } - , {nil, "from-stdin" , "k" , nil , "Run script from stdin" } + , {nil, "from-stdin" , "k" , nil , "Run script from stdin", + "e.g.", + " - CMD", + " - Single: echo print('hello') | xmake lua --from-stdin", + " - Multiline: (echo print('1') && echo print('2')) | xmake lua --from-stdin", + " - File: type script.lua | xmake lua --from-stdin", + " - PWSH", + " - Single: Write-Output 'print(\"hello\")' | xmake lua --from-stdin", + " - Multiline: Write-Output \"print('1')`nprint('2')\" | xmake lua --from-stdin", + " - File: Get-Content script.lua | xmake lua --from-stdin", + " - SH", + " - Single: echo 'print(\"hello\")' | xmake lua --from-stdin", + " - Multiline: (echo 'print(\"1\")'; echo 'print(\"2\")') | xmake lua --from-stdin", + " - File: cat script.lua | xmake lua --from-stdin" + } , {nil, "script" , "v" , nil , "Run the given lua script name, file or module and enter interactive mode if no given script.", "e.g.", " - xmake lua (enter interactive mode)", -- cgit v1.3.1 From 4aee85b4d3f6537ad1d64472bced22525db6d98f Mon Sep 17 00:00:00 2001 From: Saikari Date: Sat, 31 Jan 2026 16:57:13 +0300 Subject: refactor: update stdin command usage example for consistency --- xmake/plugins/lua/xmake.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 527ecb19e..467cde3f1 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -48,7 +48,7 @@ task("lua") , {nil, "from-stdin" , "k" , nil , "Run script from stdin", "e.g.", " - CMD", - " - Single: echo print('hello') | xmake lua --from-stdin", + " - Single: echo print(\"hello\") | xmake lua --from-stdin", " - Multiline: (echo print('1') && echo print('2')) | xmake lua --from-stdin", " - File: type script.lua | xmake lua --from-stdin", " - PWSH", -- cgit v1.3.1 From 554f7a23f12ba4df73c23de64d52855e82a37088 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 14:06:52 +0300 Subject: refactor: improve stdin handling and shell command execution across platforms --- tests/modules/stdin/test.lua | 75 +++++++ tests/projects/test_stdin/xmake.lua | 226 --------------------- xmake/core/base/os.lua | 50 +---- .../sandbox/modules/import/core/sandbox/module.lua | 55 +---- xmake/core/sandbox/modules/os.lua | 6 - xmake/modules/utils/run_script.lua | 7 +- xmake/plugins/lua/main.lua | 26 ++- xmake/plugins/lua/xmake.lua | 20 +- 8 files changed, 109 insertions(+), 356 deletions(-) create mode 100644 tests/modules/stdin/test.lua delete mode 100644 tests/projects/test_stdin/xmake.lua (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua new file mode 100644 index 000000000..72551ed9c --- /dev/null +++ b/tests/modules/stdin/test.lua @@ -0,0 +1,75 @@ +target("test") + set_kind("phony") + on_run(function (target) + local xmake = path.unix(os.programfile()) + if os.host() == "windows" then + xmake = xmake:gsub("/", "\\") + end + + local function test_shell(name, cmd, expect) + print("testing " .. name .. ": " .. cmd) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format("%s > \"%s\" 2> \"%s\"", cmd, outfile, errfile) + local ret = -1 + try { + function () + if os.host() ~= "windows" then + ret = os.execv("sh", {"-c", full_cmd}) + else + ret = os.exec(full_cmd) + end + end + } + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + if out:find(expect) then + print(" -> passed") + else + print(" -> failed") + raise("[test_stdin]: Test failed! Expect: ", expect) + end + print(" out: " .. (out or "")) + print(" err: " .. (err or "")) + os.tryrm(outfile) + os.tryrm(errfile) + end + + local pwsh = "" + if os.host() == "windows" then + -- Test cmd + test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") + test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") + -- Test powershell (if available) + local pwsh = "powershell" + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + else + -- Linux/MacOS + local pwsh = "" + try { function () os.iorun("pwsh -v"); pwsh = "pwsh" end } + if pwsh == "" then + try { function () os.iorun("powershell -v"); pwsh = "powershell" end } + end + + if pwsh ~= "" then + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + end + + test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") + test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") + end + end) diff --git a/tests/projects/test_stdin/xmake.lua b/tests/projects/test_stdin/xmake.lua deleted file mode 100644 index a603a08e9..000000000 --- a/tests/projects/test_stdin/xmake.lua +++ /dev/null @@ -1,226 +0,0 @@ -target("test") - set_kind("phony") - on_run(function (target) - import("core.base.option") - local xmake = path.unix(os.programfile()) - local xmake_dir = os.getenv("XMAKE_PROGRAM_DIR") - print("XMAKE_PROGRAM_DIR: " .. (xmake_dir or "nil")) - print("xmake binary: " .. xmake) - - if xmake_dir then - xmake_dir = path.unix(xmake_dir) - if os.host() == "windows" then - xmake_dir = xmake_dir:gsub("/", "\\") - end - os.setenv("XMAKE_PROGRAM_DIR", xmake_dir) - end - - -- New Helper: Probe for feature working (handles Windows/pwsh fallback) - local function check_feature() - print("Checking feature: --from-stdin ...") - local shell = os.shell() - - -- 1. Try detected shell if compatible - if shell == "pwsh" or shell == "powershell" then - local pwsh_probe = string.format("Write-Output \"print('probe_ok')\" | & '%s' lua --from-stdin", xmake) - local ok, out, _ = os.iorun_in_shell(shell, pwsh_probe) - if ok and out and out:find("probe_ok") then return true end - elseif shell == "cmd" then - local win_xmake = xmake:gsub("/", "\\") - local cmd_probe = string.format("echo print\"probe_ok\" | \"%s\" lua --from-stdin", win_xmake) - local ok, out, _ = os.iorun_in_shell("cmd", cmd_probe) - if ok and out and out:find("probe_ok") then return true end - end - - -- 2. Try generic sh (preferred if available validation default) - local probe_cmd = string.format("echo 'print(\"probe_ok\")' | %s lua --from-stdin", xmake) - local ok, out, _ = os.iorun_in_shell("sh", probe_cmd) - if ok and out and out:find("probe_ok") then return true end - - -- 3. Fallback: On Windows, try pwsh if sh failed - if os.host() == "windows" and shell ~= "pwsh" and shell ~= "powershell" and shell ~= "cmd" then - local pwsh_probe = string.format("Write-Output \"print('probe_ok')\" | & '%s' lua --from-stdin", xmake) - local ok, out, _ = os.iorun_in_shell("pwsh", pwsh_probe) - if ok and out and out:find("probe_ok") then return true end - end - return false - end - - -- check if feature is present - if check_feature() then - print("Feature presence check: PASS") - else - local ok, out, err = os.iorun_in_shell("sh", string.format("%s lua --help", xmake)) - if out and out:find("--from-stdin", 1, true) then - print("Feature presence check: PASS (via help text)") - else - print("Feature presence check: FAIL") - print("Help output (snippet): " .. (out and out:sub(1,100) or "nil")) - end - end - - -- test 1: pipe a few lines of lua code from echo (multiline) - local pipe_cmd = string.format("(echo 'print(\"hello\")'; echo 'print(\"from pipe\")') | '%s' lua --from-stdin", xmake) - print("running: " .. pipe_cmd) - local ok, out, err = os.iorun_in_shell("sh", pipe_cmd) - print("STDOUT 1:\n" .. (out or "")) - print("STDERR 1:\n" .. (err or "")) - assert(ok, "test 1 failed: command returned error") - if out then - assert(out:find("hello") and out:find("from pipe"), "test 1 failed: output mismatch") - end - - -- test 2: redirect from a .lua file (multiline) - -- FIX: Use cat and merge stderr (2>&1) to ensure we capture output robustly without hanging on Win - local scriptfile = path.join(os.curdir(), "test.lua") - io.writefile(scriptfile, 'print("hello")\nprint("from file")\n') - - local cat_cmd = string.format("cat '%s' | '%s' lua --from-stdin 2>&1", path.unix(scriptfile), xmake) - print("running: " .. cat_cmd) - ok, out, err = os.iorun_in_shell("sh", cat_cmd) - print("STDOUT 2:\n" .. (out or "")) - print("STDERR 2:\n" .. (err or "")) - - assert(ok, "test 2 failed: command returned error") - if out then - assert(out:find("hello") and out:find("from file"), "test 2 failed: output mismatch") - end - os.rm(scriptfile) - - -- test 3: verify traceback on error via pipe (multiline) - local error_pipe_cmd = string.format("(echo 'print(\"ok step\")'; echo 'raise(\"error_pipe\")') | '%s' lua --from-stdin", xmake) - print("running: " .. error_pipe_cmd) - ok, out, err = os.iorun_in_shell("sh", error_pipe_cmd) - print("STDOUT 3:\n" .. (out or "")) - print("STDERR 3:\n" .. (err or "")) - assert(not ok, "test 3 failed: command should have returned error") - if out then assert(out:find("ok step"), "test 3 failed: missing ok step output") end - assert((err and err:find("error_pipe")) or (out and out:find("error_pipe")), "test 3 failed: missing error message") - - -- test 4: verify traceback on error via file (multiline) - local errorfile = path.join(os.curdir(), "error.lua") - io.writefile(errorfile, 'print("ok step")\nraise("error_file")\n') - - -- FIX: Use cat and merge stderr (2>&1) - local error_file_cmd = string.format("cat '%s' | '%s' lua --from-stdin 2>&1", path.unix(errorfile), xmake) - print("running: " .. error_file_cmd) - ok, out, err = os.iorun_in_shell("sh", error_file_cmd) - print("STDOUT 4:\n" .. (out or "")) - print("STDERR 4:\n" .. (err or "")) - - assert(not ok, "test 4 failed: command should have returned error") - -- Check out (merged) or err just in case - if out then assert(out:find("ok step"), "test 4 failed: missing ok step output") end - assert((err and err:find("error_file")) or (out and out:find("error_file")), "test 4 failed: missing error message") - os.rm(errorfile) - - -- pwsh tests - if os.execv("pwsh", {"-v"}) == 0 then - print("pwsh detected, running pwsh tests...") - - -- test 5: pwsh pipe success (multiline) - local pwsh_pipe_cmd = string.format("Write-Output \"print(`\"hello`\")`nprint(`\"from pwsh pipe`\")\" | & '%s' lua --from-stdin", xmake) - print("running pwsh: " .. pwsh_pipe_cmd) - ok, out, err = os.iorun_in_shell("pwsh", pwsh_pipe_cmd) - print("STDOUT 5:\n" .. (out or "")) - print("STDERR 5:\n" .. (err or "")) - assert(ok, "test 5 failed: command returned error") - if out then - assert(out:find("hello") and out:find("from pwsh pipe"), "test 5 failed: output mismatch") - end - - -- test 6: pwsh file redirect success (multiline) - local scriptfile = path.join(os.curdir(), "test_pwsh.lua") - io.writefile(scriptfile, 'print("hello")\nprint("from pwsh file")\n') - local pwsh_redirect_cmd = string.format("Get-Content '%s' | & '%s' lua --from-stdin", scriptfile, xmake) - print("running pwsh: " .. pwsh_redirect_cmd) - ok, out, err = os.iorun_in_shell("pwsh", pwsh_redirect_cmd) - print("STDOUT 6:\n" .. (out or "")) - print("STDERR 6:\n" .. (err or "")) - assert(ok, "test 6 failed: command returned error") - if out then - assert(out:find("hello") and out:find("from pwsh file"), "test 6 failed: output mismatch") - end - os.rm(scriptfile) - - -- test 7: pwsh pipe error (multiline) - local pwsh_error_pipe_cmd = string.format("Write-Output \"print(`\"ok step`\")`nraise(`\"error_pwsh_pipe`\")\" | & '%s' lua --from-stdin", xmake) - print("running pwsh: " .. pwsh_error_pipe_cmd) - ok, out, err = os.iorun_in_shell("pwsh", pwsh_error_pipe_cmd) - print("STDOUT 7:\n" .. (out or "")) - print("STDERR 7:\n" .. (err or "")) - assert(not ok, "test 7 failed: command should have returned error") - if out then assert(out:find("ok step"), "test 7 failed: missing ok step output") end - assert((err and err:find("error_pwsh_pipe")) or (out and out:find("error_pwsh_pipe")), "test 7 failed: missing error message") - - -- test 8: pwsh file redirect error (multiline) - local errorfile = path.join(os.curdir(), "error_pwsh.lua") - io.writefile(errorfile, 'print("ok step")\nraise("error_pwsh_file")\n') - local pwsh_error_file_cmd = string.format("Get-Content '%s' | & '%s' lua --from-stdin", errorfile, xmake) - print("running pwsh: " .. pwsh_error_file_cmd) - ok, out, err = os.iorun_in_shell("pwsh", pwsh_error_file_cmd) - print("STDOUT 8:\n" .. (out or "")) - print("STDERR 8:\n" .. (err or "")) - assert(not ok, "test 8 failed: command should have returned error") - if out then assert(out:find("ok step"), "test 8 failed: missing ok step output") end - assert((err and err:find("error_pwsh_file")) or (out and out:find("error_pwsh_file")), "test 8 failed: missing error message") - os.rm(errorfile) - else - print("pwsh not found, skipping pwsh tests") - end - - -- cmd tests - if os.host() == "windows" then - print("windows detected, running cmd.exe tests...") - local win_xmake = xmake:gsub("/", "\\") - - -- test 9: cmd pipe success (multiline) - local cmd_pipe_cmd = string.format("(echo print\"hello\" && echo print\"from cmd pipe\") | \"%s\" lua --from-stdin", win_xmake) - print("running cmd: " .. cmd_pipe_cmd) - ok, out, err = os.iorun_in_shell("cmd", cmd_pipe_cmd) - print("STDOUT 9:\n" .. (out or "")) - print("STDERR 9:\n" .. (err or "")) - assert(ok, "test 9 failed: command returned error") - if out then assert(out:find("hello") and out:find("from cmd pipe"), "test 9 failed: output mismatch") end - - -- test 10: cmd file pipe success (multiline) - local scriptfile = path.join(os.curdir(), "test_cmd.lua") - local win_scriptfile = scriptfile:gsub("/", "\\") - -- FIX: Add newline for robust 'type' piping - io.writefile(scriptfile, 'print("hello")\nprint("from cmd file")\n') - - local cmd_file_cmd = string.format("type \"%s\" | \"%s\" lua --from-stdin", win_scriptfile, win_xmake) - print("running cmd: " .. cmd_file_cmd) - ok, out, err = os.iorun_in_shell("cmd", cmd_file_cmd) - print("STDOUT 10:\n" .. (out or "")) - print("STDERR 10:\n" .. (err or "")) - assert(ok, "test 10 failed: command returned error") - if out then assert(out:find("hello") and out:find("from cmd file"), "test 10 failed: output mismatch") end - os.rm(scriptfile) - - -- test 11: cmd pipe error (multiline) - local cmd_err_pipe_cmd = string.format("(echo print\"ok step\" && echo raise\"error_cmd_pipe\") | \"%s\" lua --from-stdin", win_xmake) - print("running cmd: " .. cmd_err_pipe_cmd) - ok, out, err = os.iorun_in_shell("cmd", cmd_err_pipe_cmd) - print("STDOUT 11:\n" .. (out or "")) - print("STDERR 11:\n" .. (err or "")) - assert(not ok, "test 11 failed: command should have returned error") - if out then assert(out:find("ok step"), "test 11 failed: missing ok step output") end - assert((err and err:find("error_cmd_pipe")) or (out and out:find("error_cmd_pipe")), "test 11 failed: missing error message") - - -- test 12: cmd file pipe error (multiline) - local errorfile = path.join(os.curdir(), "error_cmd.lua") - local win_errorfile = errorfile:gsub("/", "\\") - io.writefile(errorfile, 'print("ok step")\nraise("error_cmd_file")\n') - - local cmd_err_file_cmd = string.format("type \"%s\" | \"%s\" lua --from-stdin", win_errorfile, win_xmake) - print("running cmd: " .. cmd_err_file_cmd) - ok, out, err = os.iorun_in_shell("cmd", cmd_err_file_cmd) - print("STDOUT 12:\n" .. (out or "")) - print("STDERR 12:\n" .. (err or "")) - assert(not ok, "test 12 failed: command should have returned error") - if out then assert(out:find("ok step"), "test 12 failed: missing ok step output") end - assert((err and err:find("error_cmd_file")) or (out and out:find("error_cmd_file")), "test 12 failed: missing error message") - os.rm(errorfile) - end - end) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 86f04e35d..12bc30c1d 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -1106,51 +1106,6 @@ function os.iorunv(program, argv, opt) return ok == 0, outdata, errdata, errors end --- run command in the given shell and return output and error data --- --- @param shell the shell name (e.g. sh, bash, zsh, cmd, pwsh, powershell) --- @param cmd the command string --- @param opt the options --- --- @return ok, stdout, stderr, errors --- -function os.iorun_in_shell(shell, cmd, opt) - - -- check - if not shell or not cmd then - return false, nil, nil, "invalid arguments" - end - - -- run in pwsh/powershell - if shell == "pwsh" or shell == "powershell" then - local shell_cmd = string.format("& { %s; exit $LASTEXITCODE }", cmd) - return os.iorunv(shell, {"-c", shell_cmd}, opt) - - -- run in cmd - elseif shell == "cmd" then - - -- use batfile to robust pipe handling - local batfile = os.tmpfile() .. ".bat" - local batch_content = "@echo off\n" - - -- append command - batch_content = batch_content .. cmd .. "\n" - - -- append exit code check - batch_content = batch_content .. "if %errorlevel% neq 0 exit /b %errorlevel%\n" - - io.writefile(batfile, batch_content) - - local ok, out, err, errors = os.iorunv(batfile, {}, opt) - os.rm(batfile) - return ok, out, err, errors - - -- run in sh/bash/zsh... - else - return os.iorunv(shell, {"-c", cmd}, opt) - end -end - -- raise an exception and abort the current script -- -- the parent function will capture it if we uses pcall or xpcall @@ -1232,10 +1187,7 @@ function os.isexec(filepath) end end elseif os.isfile(filepath) then - if os._access then - return os._access(filepath, "x") - end - return true + return os._access(filepath, "x") end return false end diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index 34d55415d..dd7dac16e 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -67,41 +67,6 @@ function core_sandbox_module._modulepath(name) return modulepath end --- load module from string -function core_sandbox_module._loadstring(content, instance, name) - assert(content) - - -- load module script - local script, errors = load(content, name) - if not script then - return nil, errors - end - - -- with sandbox? - if instance then - - -- fork a new sandbox for this script - instance, errors = instance:fork(script, instance:rootdir()) - if not instance then - return nil, errors - end - - -- load module - local result, errors = instance:module() - if not result then - return nil, errors - end - return result, instance:script() - end - - -- load module without sandbox - local ok, result = utils.trycall(script) - if not ok then - return nil, result - end - return result, script -end - -- load module from file function core_sandbox_module._loadfile(filepath, instance) assert(filepath) @@ -545,7 +510,7 @@ function core_sandbox_module.import(name, opt) local instance = sandbox.instance() assert(instance) - -- rootdir is optional + -- the root directory for this sandbox script local rootdir = opt.rootdir or instance:rootdir() -- init module directories (disable local packages?) @@ -556,26 +521,10 @@ function core_sandbox_module.import(name, opt) loadopt.instance = instance loadopt.modules = modules loadopt.modules_directories = modules_directories + local found, module, errors = core_sandbox_module._find_and_load(name, loadopt) - -- load module - local module - local errors - local found = false - if opt.content then - found = true - module, errors = core_sandbox_module._loadstring(opt.content, instance, name) - if module then - if not opt.nocache then - modules[name] = {module, nil} - end - end - else - found, module, errors = core_sandbox_module._find_and_load(name, loadopt) - end - -- not found? attempt to load module.interface if not found and not opt.inherit then - -- get module name local found2 = false local errors2 = nil diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index ec5cb0e1e..aebb3cfb0 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -324,12 +324,6 @@ function sandbox_os.iorunv(program, argv, opt) return outdata, errdata end --- run command in shell with io redirection and return (ok, out, err) -function sandbox_os.iorun_in_shell(shell, cmd, ...) - cmd = vformat(cmd, ...) - return os.iorun_in_shell(shell, cmd) -end - -- execute command function sandbox_os.exec(cmd, ...) cmd = vformat(cmd, ...) diff --git a/xmake/modules/utils/run_script.lua b/xmake/modules/utils/run_script.lua index a51f4eef0..e6f8469f6 100644 --- a/xmake/modules/utils/run_script.lua +++ b/xmake/modules/utils/run_script.lua @@ -61,12 +61,7 @@ function _run_script(script, args, opt) local script_type, script_name -- import and run script - if opt.content then - - -- run the given lua script content - script_type, script_name = "given lua script content", script - func = import(script, {anonymous = true, content = opt.content}) - elseif path.extension(script) == ".lua" and os.isfile(script) then + if path.extension(script) == ".lua" and os.isfile(script) then -- run the given lua script file (xmake lua /tmp/script.lua) script_type, script_name = "given lua script file", path.relative(script) diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 0a608b10a..a945a64c1 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -55,15 +55,26 @@ function main() -- run script local script = option.get("script") local arguments = option.get("arguments") - local from_stdin = option.get("from_stdin") or option.get("from-stdin") + local from_stdin = option.get("stdin") if script or from_stdin then -- run script from stdin? - local script_content + local script_file_to_remove if script == "-" or from_stdin then - script_content = io.read("*a") - if not script or script == "-" then - script = "xmake_lua_stdin" + local script_content = io.read("*a") + if script_content then + import("core.base.tty") + local shell = tty.shell() + if shell == "cmd" or shell == "powershell" or shell == "pwsh" or os.host() == "windows" then + script_content = script_content:trim() + if script_content:startswith('"') and script_content:endswith('"') then + script_content = script_content:sub(2, -2) + end + script_content = script_content:replace("\\n", "\n", {plain = true}):replace("\\r", "\r", {plain = true}) + end + script = os.tmpfile() .. ".lua" + io.writefile(script, script_content) + script_file_to_remove = script end end @@ -74,8 +85,11 @@ function main() diagnosis = option.get("diagnosis"), command = option.get("command"), arguments = arguments, - content = script_content, deserialize = option.get("deserialize")}) + + if script_file_to_remove then + os.tryrm(script_file_to_remove) + end end else -- enter interactive mode diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 467cde3f1..8b60c96d3 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -45,20 +45,20 @@ task("lua") {'l', "list" , "k" , nil , "List all scripts." } , {'c', "command" , "k" , nil , "Run script as command" } , {'d', "deserialize" , "kv" , nil , "Deserialize arguments starts with given prefix" } - , {nil, "from-stdin" , "k" , nil , "Run script from stdin", + , {nil, "stdin" , "k" , nil , "Run script from stdin", "e.g.", " - CMD", - " - Single: echo print(\"hello\") | xmake lua --from-stdin", - " - Multiline: (echo print('1') && echo print('2')) | xmake lua --from-stdin", - " - File: type script.lua | xmake lua --from-stdin", + " - Single: echo print(\"hello\") | xmake lua --stdin", + " - Multiline: (echo print('1') && echo print('2')) | xmake lua --stdin", + " - File: type script.lua | xmake lua --stdin", " - PWSH", - " - Single: Write-Output 'print(\"hello\")' | xmake lua --from-stdin", - " - Multiline: Write-Output \"print('1')`nprint('2')\" | xmake lua --from-stdin", - " - File: Get-Content script.lua | xmake lua --from-stdin", + " - Single: Write-Output 'print(\"hello\")' | xmake lua --stdin", + " - Multiline: Write-Output \"print('1')`nprint('2')\" | xmake lua --stdin", + " - File: Get-Content script.lua | xmake lua --stdin", " - SH", - " - Single: echo 'print(\"hello\")' | xmake lua --from-stdin", - " - Multiline: (echo 'print(\"1\")'; echo 'print(\"2\")') | xmake lua --from-stdin", - " - File: cat script.lua | xmake lua --from-stdin" + " - Single: echo 'print(\"hello\")' | xmake lua --stdin", + " - Multiline: (echo 'print(\"1\")'; echo 'print(\"2\")') | xmake lua --stdin", + " - File: cat script.lua | xmake lua --stdin" } , {nil, "script" , "v" , nil , "Run the given lua script name, file or module and enter interactive mode if no given script.", "e.g.", -- cgit v1.3.1 From faca264f8ea0fb695f6bc8b3a9d1411f901f054b Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 16:14:46 +0300 Subject: refactor: simplify stdin handling by removing redundant checks and streamlining usage examples --- xmake/plugins/lua/main.lua | 2 +- xmake/plugins/lua/xmake.lua | 14 ++------------ 2 files changed, 3 insertions(+), 13 deletions(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 1cc13dbc3..247684e24 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -60,7 +60,7 @@ function main() -- run script from stdin? local script_file_to_remove - if script == "-" or from_stdin then + if from_stdin then local script_content = io.read("*a") if script_content then -- remove utf8 bom diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 8b60c96d3..bb9308378 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -47,18 +47,8 @@ task("lua") , {'d', "deserialize" , "kv" , nil , "Deserialize arguments starts with given prefix" } , {nil, "stdin" , "k" , nil , "Run script from stdin", "e.g.", - " - CMD", - " - Single: echo print(\"hello\") | xmake lua --stdin", - " - Multiline: (echo print('1') && echo print('2')) | xmake lua --stdin", - " - File: type script.lua | xmake lua --stdin", - " - PWSH", - " - Single: Write-Output 'print(\"hello\")' | xmake lua --stdin", - " - Multiline: Write-Output \"print('1')`nprint('2')\" | xmake lua --stdin", - " - File: Get-Content script.lua | xmake lua --stdin", - " - SH", - " - Single: echo 'print(\"hello\")' | xmake lua --stdin", - " - Multiline: (echo 'print(\"1\")'; echo 'print(\"2\")') | xmake lua --stdin", - " - File: cat script.lua | xmake lua --stdin" + " - echo 'print(\"hello\")' | xmake lua --stdin", + " - cat script.lua | xmake lua --stdin" } , {nil, "script" , "v" , nil , "Run the given lua script name, file or module and enter interactive mode if no given script.", "e.g.", -- cgit v1.3.1 From f851d28b49caa6d19e4edc1df39b74222bda6946 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 09:29:02 +0300 Subject: try format --- xmake/plugins/lua/xmake.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index bb9308378..dee1707b6 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -45,11 +45,11 @@ task("lua") {'l', "list" , "k" , nil , "List all scripts." } , {'c', "command" , "k" , nil , "Run script as command" } , {'d', "deserialize" , "kv" , nil , "Deserialize arguments starts with given prefix" } - , {nil, "stdin" , "k" , nil , "Run script from stdin", + , {nil, "stdin" , "k" , nil , "Run script from stdin", "e.g.", " - echo 'print(\"hello\")' | xmake lua --stdin", " - cat script.lua | xmake lua --stdin" - } + } , {nil, "script" , "v" , nil , "Run the given lua script name, file or module and enter interactive mode if no given script.", "e.g.", " - xmake lua (enter interactive mode)", -- cgit v1.3.1