diff options
| author | Saikari <[email protected]> | 2026-02-02 09:22:02 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-02-02 09:22:02 +0300 |
| commit | 5e885b512315064825a96fa4865707adae9ab7ed (patch) | |
| tree | bc1cf3ee4bf7d0a4485e09b8d60074118da6c512 /xmake/plugins/lua/main.lua | |
| parent | 84dcbd0fa5ebaa612c851fb1694993a9aa3f3968 (diff) | |
refactor: extract stdin script handling into a separate function for improved readability and maintainability
Diffstat (limited to 'xmake/plugins/lua/main.lua')
| -rw-r--r-- | xmake/plugins/lua/main.lua | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 96ff6e40d..988f2af1f 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -45,6 +45,31 @@ 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 + if script_content:startswith(utf8.bom) then + script_content = script_content:sub(#utf8.bom + 1) + end + 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}) + end + + 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 @@ -61,24 +86,10 @@ function main() -- run script from stdin? local script_file_to_remove if from_stdin then - local script_content = io.read("*a") - if script_content then - -- remove utf8 bom - 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}) - end - - if not script_content:find("function main", 1, true) then - script_content = "function main(...)\n" .. script_content .. "\nend" - end - - script = os.tmpfile() .. ".lua" - io.writefile(script, script_content) - script_file_to_remove = script + local script_path = _get_script_from_stdin() + if script_path then + script = script_path + script_file_to_remove = script_path end end |
