From d1653c5b32f4efa7fec6ab40cb93eb14b0014c38 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 10:28:45 +0300 Subject: refactor: enhance stdin script handling by improving string trimming logic --- tests/modules/stdin/test.lua | 7 ++++++- xmake/plugins/lua/main.lua | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 6b1b4026b..b0c908c3c 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -112,9 +112,14 @@ function main(t) string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2" ) + test_shell( + "pwsh_main", + string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), + "in_pwsh_main" + ) test_shell( "pwsh_multi", - string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake), + string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" ) else diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 261dffaa0..b2a104949 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -53,9 +53,14 @@ function _get_script_from_stdin() script_content = script_content:ltrim(utf8.bom) local shell = os.shell() if shell == "cmd" or shell == "powershell" or shell == "pwsh" or is_host("windows") then - script_content = script_content:trim() - script_content = script_content:trim('\"') - script_content = script_content:replace("\\n", "\n", {plain = true}):replace("\\r", "\r", {plain = true}) + local trimmed = script_content:trim() + if trimmed:startswith('"') and trimmed:endswith('"') then + script_content = trimmed:trim('"') + script_content = script_content:replace("\\n", "\n", {plain = true}) + :replace("\\r", "\r", {plain = true}) + else + script_content = trimmed + end end if not script_content:find("function main", 1, true) then -- cgit v1.3.1