From 9862d1b95fa5978eaf24d65f1ca5f151d410a14b Mon Sep 17 00:00:00 2001 From: Opportunity Date: Wed, 15 Jan 2020 11:24:36 +0800 Subject: Improve options parsing --- xmake/plugins/lua/xmake.lua | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 1fc96c724..57b2bc5f4 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -53,18 +53,30 @@ task("lua") -- get script if script then + local args = option.get("arguments") or {} + for i, value in ipairs(args) do + if value:startswith('@') then + local v, err = string.deserialize(value:sub(2)) + if err then + utils.warning(err) + else + args[i] = v + end + end + end + -- 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)) - import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(unpack(option.get("arguments") or {})) + import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(unpack(args)) 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) - import("scripts." .. script, {anonymous = true})(unpack(option.get("arguments") or {})) + import("scripts." .. script, {anonymous = true})(unpack(args)) else -- attempt to find the builtin module @@ -79,13 +91,13 @@ task("lua") if object then -- run builtin modules (xmake lua core.xxx.xxx) vprint("running builtin module: %s", script) - result = object(unpack(option.get("arguments") or {})) + result = table.pack(object(unpack(args))) else -- run imported modules (xmake lua core.xxx.xxx) vprint("running imported module: %s", script) - result = import(script, {anonymous = true})(unpack(option.get("arguments") or {})) + result = table.pack(import(script, {anonymous = true})(unpack(args))) end - if result ~= nil then utils.dump(result) end + if result and result.n ~= 0 then utils.dump(unpack(result, 1, result.n)) end end else -- enter interactive mode @@ -115,9 +127,11 @@ task("lua") " - xmake lua (enter interactive mode)", " - xmake lua /tmp/script.lua", " - xmake lua echo 'hello xmake'", - " - xmake lua core.xxx.xxx", + " - xmake lua core.xxx.xxx", " - xmake lua -c 'print(...)' hello xmake!" } - , {nil, "arguments", "vs", nil, "The script arguments." } + , {nil, "arguments", "vs", nil, "The script arguments, use '@' to enable deserializing.", + "e.g.", + " - xmake lua lib.detect.find_tool tar @{version=true}" } } } -- cgit v1.3.1 From f4e30b5e7879d4dbfbe4424f5f9156cb6cfdaf50 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Wed, 15 Jan 2020 12:06:01 +0800 Subject: Improve dump --- xmake/core/base/dump.lua | 14 ++++++++++++-- xmake/core/base/utils.lua | 21 ++++++--------------- xmake/plugins/lua/xmake.lua | 10 ++++++---- 3 files changed, 24 insertions(+), 21 deletions(-) (limited to 'xmake/plugins/lua/xmake.lua') diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua index 3adb54133..5e7ed9b8f 100644 --- a/xmake/core/base/dump.lua +++ b/xmake/core/base/dump.lua @@ -88,17 +88,27 @@ function dump._print_function(func, as_key) end -- print value with default format -function dump._print_default(value) +function dump._print_default_scalar(value) io.write(dump._translate("${reset}${color.dump.default}"), dump._format("text.dump.default_format", "%s", value), dump._translate("${reset}")) end -- print udata value with scalar format function dump._print_udata_scalar(value) + local metatable = debug.getmetatable(value) + local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring")) + if tostringmethod then + value = tostringmethod(value) + end io.write(dump._translate("${reset}${color.dump.udata}"), dump._format("text.dump.udata_format", "%s", value), dump._translate("${reset}")) end -- print table value with scalar format function dump._print_table_scalar(value) + local metatable = debug.getmetatable(value) + local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring")) + if tostringmethod then + value = tostringmethod(value) + end io.write(dump._translate("${reset}${color.dump.table}"), dump._format("text.dump.table_format", "%s", value), dump._translate("${reset}")) end @@ -117,7 +127,7 @@ function dump._print_scalar(value, as_key) elseif type(value) == "table" then dump._print_table_scalar(value) else - dump._print_default(value) + dump._print_default_scalar(value) end end diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index ca13ab097..fa42d7e25 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -29,7 +29,7 @@ local log = require("base/log") local io = require("base/io") local dump = require("base/dump") --- dump value +-- dump values function utils.dump(...) if option.get("quiet") then return ... @@ -49,25 +49,16 @@ function utils.dump(...) if values.n == 0 then return end - local indent = nil - local values_count = values.n - values.n = nil - -- use last input as indent if it is a string - if values_count > 1 and type(values[values_count]) == "string" then - indent = values[values_count] - values[values_count] = nil - values_count = values_count - 1 - end - if values_count == 1 then - dump(values[1], indent or "", diagnosis) + if values.n == 1 then + dump(values[1], "", diagnosis) else - for i = 1, values_count do - dump(values[i], indent or string.format("%2d: ", i), diagnosis) + for i = 1, values.n do + dump(values[i], string.format("%2d: ", i), diagnosis) end end - return table.unpack(values, 1, values_count) + return table.unpack(values, 1, values.n) end -- print string with newline diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 57b2bc5f4..e264ba14d 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -54,10 +54,12 @@ task("lua") if script then local args = option.get("arguments") or {} + args.n = #args for i, value in ipairs(args) do if value:startswith('@') then local v, err = string.deserialize(value:sub(2)) if err then + -- for strings that failed to deserialize, regaed it as a normal string, just show a warning message utils.warning(err) else args[i] = v @@ -70,13 +72,13 @@ task("lua") -- run the given lua script file (xmake lua /tmp/script.lua) vprint("running given lua script file: %s", path.relative(script)) - import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(unpack(args)) + import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(table.unpack(args, 1, args.n)) 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) - import("scripts." .. script, {anonymous = true})(unpack(args)) + import("scripts." .. script, {anonymous = true})(table.unpack(args, 1, args.n)) else -- attempt to find the builtin module @@ -91,11 +93,11 @@ task("lua") if object then -- run builtin modules (xmake lua core.xxx.xxx) vprint("running builtin module: %s", script) - result = table.pack(object(unpack(args))) + result = table.pack(object(table.unpack(args, 1, args.n))) else -- run imported modules (xmake lua core.xxx.xxx) vprint("running imported module: %s", script) - result = table.pack(import(script, {anonymous = true})(unpack(args))) + result = table.pack(import(script, {anonymous = true})(table.unpack(args, 1, args.n))) end if result and result.n ~= 0 then utils.dump(unpack(result, 1, result.n)) end end -- cgit v1.3.1