diff options
| author | ruki <[email protected]> | 2026-02-03 12:10:22 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-03 12:10:22 +0800 |
| commit | 1bcce9d85f07cae1f7ac9f3a3099047539d26c84 (patch) | |
| tree | 1516224d2d661413fe5e786a67a83484784b2d76 | |
| parent | 84ecebb4e9beaef3d046617c9c5ae3c1714a4aa8 (diff) | |
| parent | 7a9f668793b387efc3d657ceedbd0ab913dab4e0 (diff) | |
Merge pull request #7284 from luadebug/stdin
Add `--stdin`
| -rw-r--r-- | core/src/xmake/engine.c | 6 | ||||
| -rw-r--r-- | tests/modules/stdin/test.lua | 52 | ||||
| -rw-r--r-- | xmake/plugins/lua/main.lua | 51 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 5 |
4 files changed, 105 insertions, 9 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index df5ae06be..9c02d8487 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -874,7 +874,11 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t *engine, tb_char_t **arg ssize_t size = readlink(XM_PROC_SELF_FILE, path, (size_t)maxn); if (size > 0 && size < maxn) { path[size] = '\0'; - ok = tb_true; + // ignore cosmocc ape binary, we fallback to argv[0], .e.g /usr/bin/ape, /home/ruki/.ape-1.10 + tb_char_t const* filename = tb_strrchr(path, '/'); + if (!tb_strstr(filename ? filename + 1 : path, "ape")) { + ok = tb_true; + } } #elif defined(TB_CONFIG_OS_BSD) && defined(KERN_PROC_PATHNAME) // only for FreeBSD and OpenBSD, https://github.com/xmake-io/xmake/issues/2948 diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua new file mode 100644 index 000000000..323ba6f81 --- /dev/null +++ b/tests/modules/stdin/test.lua @@ -0,0 +1,52 @@ +import("lib.detect.find_tool") + +function _run_sh(t, name, cmd, expect) + if not is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('"%s" l --stdin', xmake) + local outdata = os.iorunv("sh", {"-c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) + end +end + +function _run_cmd(t, name, cmd, expect) + if is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('%s l --stdin', xmake) + local outdata = os.iorunv("cmd", {"/c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) + end +end + +function _run_pwsh(t, name, cmd, expect) + if is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('%s l --stdin', xmake) + local pwsh = find_tool("powershell") + if pwsh then + local outdata = os.iorunv(pwsh.program, {"-c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) + end + end +end + +function test_sh(t) + _run_sh(t, "sh_single", "echo \"print('hello_sh')\"", "hello_sh") + _run_sh(t, "sh_calc", "echo \"local f = 1+1; print(f)\"", "2") + _run_sh(t, "sh_main", "echo \"function main() print('in_sh_main') end\"", "in_sh_main") + _run_sh(t, "sh_multi", "printf \"print('shell_line1')\\nprint('shell_line2')\"", "shell_line1\nshell_line2") +end + +function test_cmd(t) + _run_cmd(t, "cmd_single", "echo print 'hello_cmd'", "hello_cmd") + _run_cmd(t, "cmd_calc", "echo local f = 1+1; print(f)", "2") + _run_cmd(t, "cmd_multi_lines", "(echo print 'line1' && echo print 'line2')", "line1\nline2") + _run_cmd(t, "cmd_multi_semicolon", "echo print('semi1'); print('semi2')", "semi1\nsemi2") +end + +function test_pwsh(t) + _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") + _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") + _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\" \"print('pline2')\"", "pline1\npline2") +end diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 2b0a9efe2..772faa2c6 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -45,6 +45,22 @@ function _list() end end +-- get script from stdin +function _get_script_from_stdin() + local script_content = io.read("*a") + if script_content then + -- remove utf8 bom + script_content = script_content:ltrim(utf8.bom) + if not script_content:find("function main", 1, true) then + script_content = "function main(...)\n" .. script_content .. "\nend" + end + + local script = os.tmpfile() .. ".lua" + io.writefile(script, script_content) + return script + end +end + function main() -- list builtin scripts @@ -54,14 +70,33 @@ 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("stdin") + if script or from_stdin then + + -- run script from stdin? + local script_file_to_remove + if from_stdin then + local script_path = _get_script_from_stdin() + if script_path then + script = script_path + script_file_to_remove = script_path + end + end + + 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")}) + + if script_file_to_remove then + os.tryrm(script_file_to_remove) + end + end else -- enter interactive mode sandbox.interactive() diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 8ef6c3573..dee1707b6 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -45,6 +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", + "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)", |
