summaryrefslogtreecommitdiff
path: root/xmake/plugins/lua
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2020-01-22 18:18:26 +0800
committerOpportunityLiu <[email protected]>2020-01-22 18:18:26 +0800
commit4689b1ab542c8dfbe255ac636e523818a3db9dde (patch)
tree5da2a6c2d3682d2d805135c4fa486c7c3faf3a19 /xmake/plugins/lua
parent2fdf5dafd9c7e99ee9493f1c39c39f19b3e50d19 (diff)
improve lua
Diffstat (limited to 'xmake/plugins/lua')
-rw-r--r--xmake/plugins/lua/main.lua47
-rw-r--r--xmake/plugins/lua/xmake.lua5
2 files changed, 39 insertions, 13 deletions
diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua
index c0ed6e3d0..aa6cab807 100644
--- a/xmake/plugins/lua/main.lua
+++ b/xmake/plugins/lua/main.lua
@@ -41,6 +41,23 @@ function _list()
end
end
+function _print_vlog(script_type, script_name, args)
+ if not option.get("verbose") then return end
+ cprintf("running %s ${underline}%s${reset}", script_type, script_name)
+ if args.n > 0 then
+ print(" with args:")
+ if not option.get("diagnosis") then
+ for i = 1, args.n do
+ print(" - " .. todisplay(args[i]))
+ end
+ else
+ utils.dump(table.unpack(args, 1, args.n))
+ end
+ else
+ print(".")
+ end
+end
+
function _run_commanad(command, args)
local tmpfile = os.tmpfile() .. ".lua"
io.writefile(tmpfile, "function main(...)\n" .. command .. "\nend")
@@ -51,19 +68,19 @@ function _run_script(script, args)
local func
local printresult = false
+ local script_type, script_name
-- import and run script
if path.extension(script) == ".lua" and os.isfile(script) then
-- run the given lua script file (xmake lua /tmp/script.lua)
- vprint("running given lua script file: %s", path.relative(script))
+ script_type, script_name = "given lua script file", path.relative(script)
func = import(path.basename(script), {rootdir = path.directory(script), anonymous = true})
-
elseif os.isfile(path.join(os.scriptdir(), "scripts", script .. ".lua")) then
-- run builtin lua script (xmake lua echo "hello xmake")
- vprint("running builtin lua script: %s", script)
- func = import("scripts." .. script, {anonymous = true})(table.unpack(args, 1, args.n))
+ script_type, script_name = "builtin lua script", script
+ func = import("scripts." .. script, {anonymous = true})
else
-- attempt to find the builtin module
@@ -76,16 +93,17 @@ function _run_script(script, args)
end
if object then
-- run builtin modules (xmake lua core.xxx.xxx)
- vprint("running builtin module: %s", script)
+ script_type, script_name = "builtin module", script
func = object
else
-- run imported modules (xmake lua core.xxx.xxx)
- vprint("running imported module: %s", script)
+ script_type, script_name = "imported module", script
func = import(script, {anonymous = true})
end
printresult = true
end
+ _print_vlog(script_type or "script", script_name or "", args)
local result = table.pack(func(table.unpack(args, 1, args.n)))
if printresult and result and result.n ~= 0 then
utils.dump(unpack(result, 1, result.n))
@@ -96,13 +114,20 @@ function _get_args()
local args = option.get("arguments") or {}
args.n = #args
+
+ local deserialize = option.get("deserialize")
+ if not deserialize then
+ return args
+ end
+
+ deserialize = tostring(deserialize)
+
for i, value in ipairs(args) do
- if value:startswith('@') then
- -- deserialize @ prefixed arg
- local v, err = string.deserialize(value:sub(2))
+ if value:startswith(deserialize) then
+ -- deserialize prefixed arg
+ local v, err = string.deserialize(value:sub(#deserialize + 1))
if err then
- -- for strings that failed to deserialize, regaed it as a normal string, just show a warning message
- utils.warning(err)
+ raise(err)
else
args[i] = v
end
diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua
index 23d00b145..a466075b4 100644
--- a/xmake/plugins/lua/xmake.lua
+++ b/xmake/plugins/lua/xmake.lua
@@ -44,6 +44,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, "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)",
@@ -58,9 +59,9 @@ task("lua")
return import("main.scripts")()
end }
- , {nil, "arguments" , "vs" , nil , "The script arguments, use '@' to enable deserializing.",
+ , {nil, "arguments" , "vs" , nil , "The script arguments, use '--deserialize' option to enable deserializing.",
"e.g.",
- " - xmake lua lib.detect.find_tool tar @{version=true}" }
+ " - xmake lua -d@ lib.detect.find_tool tar @{version=true}" }
}
}