diff options
| author | ruki <[email protected]> | 2020-01-25 09:37:53 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-01-25 09:37:53 +0800 |
| commit | 3aed90724e29a7773f3c2874926e861ae92fe120 (patch) | |
| tree | 4aa5ac0d9d94b4d0b86c4e5b78693a6ba501f9f0 | |
| parent | 07b241522ae9712167f1f218e08e651717b7fd55 (diff) | |
| parent | 5d7a6efa13021da59a370bb011f4caf494b35da3 (diff) | |
Merge pull request #674 from OpportunityLiu/dev
Improve task create, plugin lua & todisplay
| -rw-r--r-- | xmake/actions/create/main.lua | 7 | ||||
| -rw-r--r-- | xmake/core/base/dump.lua | 92 | ||||
| -rw-r--r-- | xmake/core/base/todisplay.lua | 162 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/todisplay.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/lua/main.lua | 47 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 5 |
6 files changed, 212 insertions, 103 deletions
diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua index 55eca425f..162ee2b88 100644 --- a/xmake/actions/create/main.lua +++ b/xmake/actions/create/main.lua @@ -71,9 +71,12 @@ function _create_project(language, templateid, targetname) -- get project directory local projectdir = path.absolute(option.get("project") or path.join(os.curdir(), targetname)) - -- ensure the project directory - if not os.isdir(projectdir) then + if not os.isdir(projectdir) then + -- make the project directory if not exists os.mkdir(projectdir) + elseif not os.emptydir(projectdir) then + -- otherwise, check whether it is empty + raise("project directory (${underline}%s${reset}) is not empty!", path.relative(projectdir, os.workingdir())) end -- enter the project directory diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua index dbfdc2e3f..4e3494662 100644 --- a/xmake/core/base/dump.lua +++ b/xmake/core/base/dump.lua @@ -22,86 +22,52 @@ local dump = dump or {} -- load modules -local colors = require("base/colors") local todisplay = require("base/todisplay") --- format string with theme colors -function dump._format(fmtkey, fmtdefault, ...) - local theme = colors.theme() - if theme then - return colors.translate(string.format(theme:get(fmtkey), ...), { patch_reset = false, ignore_unknown = true }) - else - return string.format(fmtdefault, ...) - end -end - --- translate string with theme formats -function dump._translate(str) - local theme = colors.theme() - if theme then - return colors.translate(str, { patch_reset = false, ignore_unknown = true }) - else - return colors.ignore(str) - end -end - -- print string function dump._print_string(str, as_key) - local quote = (not as_key) or (not str:match("^[a-zA-Z_][a-zA-Z0-9_]*$")) - if quote then - io.write(dump._translate([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}]]), str, dump._translate([[${reset}${color.dump.string_quote}"${reset}]])) - else - io.write(dump._translate("${reset}${color.dump.string}"), str, dump._translate("${reset}")) - end + io.write(todisplay._print_string(str, as_key)) end -- print keyword function dump._print_keyword(keyword) - io.write(dump._translate("${reset}${color.dump.keyword}"), tostring(keyword), dump._translate("${reset}")) -end - --- print number -function dump._print_number(num) - io.write(dump._translate(todisplay(num))) + io.write(todisplay._print_keyword(keyword)) end -- print function function dump._print_function(func, as_key) if as_key then - io.write(dump._translate("${reset}${color.dump.function}"), dump._format("text.dump.default_format", "%s", func), dump._translate("${reset}")) + io.write(todisplay._translate("${reset}${color.dump.function}"), + todisplay._format("text.dump.default_format", "%s", func), + todisplay._translate("${reset}")) else - io.write(dump._translate(todisplay(func))) + io.write(todisplay._print_function(func)) end end --- print value with default format -function dump._print_default_scalar(value) - io.write(dump._translate(todisplay(value))) -end - -- print scalar value function dump._print_scalar(value, as_key) - if type(value) == "nil" or type(value) == "boolean" then - dump._print_keyword(value) - elseif type(value) == "number" then - dump._print_number(value) - elseif type(value) == "string" then + if type(value) == "string" then dump._print_string(value, as_key) elseif type(value) == "function" then dump._print_function(value, as_key) else - dump._print_default_scalar(value) + io.write(todisplay._print_scalar(value)) end end -- print anchor function dump._print_anchor(printed_set_value) - io.write(dump._translate("${color.dump.anchor}"), dump._format("text.dump.anchor", "&%s", printed_set_value.id), dump._translate("${reset}")) + io.write(todisplay._translate("${color.dump.anchor}"), + todisplay._format("text.dump.anchor", "&%s", printed_set_value.id), + todisplay._translate("${reset}")) end -- print reference function dump._print_reference(printed_set_value) - io.write(dump._translate("${color.dump.reference}"), dump._format("text.dump.reference", "*%s", printed_set_value.id), dump._translate("${reset}")) + io.write(todisplay._translate("${color.dump.reference}"), + todisplay._format("text.dump.reference", "*%s", printed_set_value.id), + todisplay._translate("${reset}")) end -- print anchor and store to printed_set @@ -135,12 +101,16 @@ function dump._print_metatable(value, metatable, inner_indent, printed_set, prin io.write("\n", inner_indent) local funcname = k:sub(3) dump._print_keyword(funcname) - io.write(dump._translate("${reset} ${dim}=${reset} ")) - if funcname == "tostring" or funcname == "len" then + io.write(todisplay._translate("${reset} ${dim}=${reset} ")) + if funcname == "tostring" or funcname == "len" or funcname == "todisplay" then local ok, result = pcall(v, value, value) if ok then - dump._print_scalar(result) - io.write(" (evaluated)") + if funcname == "todisplay" and type(result) == "string" then + io.write(todisplay._translate(result)) + else + dump._print_scalar(result) + end + io.write(todisplay._translate("${dim} (evaluated)${reset}")) else dump._print_scalar(v) end @@ -172,7 +142,7 @@ function dump._print_metatable(value, metatable, inner_indent, printed_set, prin dump._print_keyword("(") dump._print_scalar(k, true) dump._print_keyword(")") - io.write(dump._translate("${reset} ${dim}=${reset} ")) + io.write(todisplay._translate("${reset} ${dim}=${reset} ")) if v and printed_set.refs[v] then dump._print_reference(printed_set.refs[v]) else @@ -231,16 +201,16 @@ function dump._print_udata(value, first_indent, remain_indent, printed_set) local inner_indent = remain_indent .. " " -- print open brackets - io.write(dump._translate("${reset}${color.dump.udata}[${reset}")) + io.write(todisplay._translate("${reset}${color.dump.udata}[${reset}")) -- print metatable local no_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, false) -- print close brackets if no_value then - io.write(dump._translate(" ${color.dump.udata}]${reset}")) + io.write(todisplay._translate(" ${color.dump.udata}]${reset}")) else - io.write("\b \n", remain_indent, dump._translate("${reset}${color.dump.udata}]${reset}")) + io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.udata}]${reset}")) end end @@ -253,14 +223,14 @@ function dump._print_table(value, first_indent, remain_indent, printed_set) local metatable = debug.getmetatable(value) local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring")) if not first_level and tostringmethod then - return dump._print_default_scalar(value) + return dump._print_scalar(value) end local inner_indent = remain_indent .. " " local first_value = true -- print open brackets - io.write(dump._translate("${reset}${color.dump.table}{${reset}")) + io.write(todisplay._translate("${reset}${color.dump.table}{${reset}")) local function print_newline() if first_value then @@ -299,7 +269,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set) if not is_arr or type(k) ~= "number" then print_newline() dump._print_scalar(k, true) - io.write(dump._translate("${reset} ${dim}=${reset} ")) + io.write(todisplay._translate("${reset} ${dim}=${reset} ")) if type(v) == "table" then if printed_set.refs[v] then dump._print_reference(printed_set.refs[v]) @@ -315,9 +285,9 @@ function dump._print_table(value, first_indent, remain_indent, printed_set) -- print close brackets if first_value then - io.write(dump._translate(" ${color.dump.table}}${reset}")) + io.write(todisplay._translate(" ${color.dump.table}}${reset}")) else - io.write("\b \n", remain_indent, dump._translate("${reset}${color.dump.table}}${reset}")) + io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.table}}${reset}")) end end diff --git a/xmake/core/base/todisplay.lua b/xmake/core/base/todisplay.lua index ed821e2f2..92af86975 100644 --- a/xmake/core/base/todisplay.lua +++ b/xmake/core/base/todisplay.lua @@ -23,26 +23,58 @@ local todisplay = todisplay or {} -- load modules local colors = require("base/colors") +local math = require("base/math") + + +function todisplay._reset() + local reset = todisplay._RESET + if not reset then + reset = colors.translate("${reset}") + todisplay._RESET = reset + end + return reset +end -- format string with theme colors function todisplay._format(fmtkey, fmtdefault, ...) local theme = colors.theme() - return string.format((theme and theme:get(fmtkey)) or fmtdefault, ...) + if theme then + return colors.translate(string.format(theme:get(fmtkey), ...), { patch_reset = false, ignore_unknown = true }) + else + return string.format(fmtdefault, ...) + end +end + +-- translate string with theme formats +function todisplay._translate(str) + local theme = colors.theme() + if theme then + return colors.translate(str, { patch_reset = false, ignore_unknown = true }) + else + return colors.ignore(str) + end end -- print keyword function todisplay._print_keyword(keyword) - return string.format("${reset}${color.dump.keyword}%s${reset}", keyword) + return todisplay._translate("${reset}${color.dump.keyword}") .. tostring(keyword) .. todisplay._reset() end -- print string -function todisplay._print_string(str) - return string.format([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}%s${reset}${color.dump.string_quote}"${reset}]], str) +function todisplay._print_string(str, as_key) + local quote = (not as_key) or (not str:match("^[a-zA-Z_][a-zA-Z0-9_]*$")) + if quote then + return todisplay._translate([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}]]) + .. str + .. todisplay._translate([[${reset}${color.dump.string_quote}"${reset}]]) + else + return todisplay._translate("${reset}${color.dump.string}") .. str .. todisplay._reset() + end end -- print number function todisplay._print_number(num) - return string.format("${reset}${color.dump.number}%s${reset}", num) + return todisplay._translate("${reset}${color.dump.number}") .. tostring(num) .. todisplay._reset() end -- print function @@ -53,33 +85,43 @@ function todisplay._print_function(func) srcinfo = srcinfo .. ":" .. funcinfo.linedefined end local funcname = funcinfo.name and (funcinfo.name .. " ") or "" - return string.format("${reset}${color.dump.function}function ${bright}%s${reset}${dim}%s${reset}", funcname, srcinfo) + return todisplay._translate("${reset}${color.dump.function}function ${bright}") + .. funcname + .. todisplay._translate("${reset}${dim}") + .. srcinfo .. todisplay._reset() end --- print value with default format -function todisplay._print_default_scalar(value, style, formatkey) +function todisplay._get_tostr_method(value) local metatable = debug.getmetatable(value) if metatable then local __todisplay = rawget(metatable, "__todisplay") local __tostring = rawget(metatable, "__tostring") - if __todisplay then - local ok, str = pcall(__todisplay, value) - if ok then - value = str - -- disable format - formatkey = nil - end - elseif __tostring then - local ok, str = pcall(__tostring, value) - if ok then - value = str - end + return __todisplay, __tostring + end + return nil, nil +end + +-- print value with default format +function todisplay._print_default_scalar(value, style, formatkey) + local __todisplay, __tostring = todisplay._get_tostr_method(value) + if __todisplay then + local ok, str = pcall(__todisplay, value) + if ok then + value = todisplay._translate(str) + -- disable format + formatkey = nil + end + elseif __tostring then + local ok, str = pcall(__tostring, value) + if ok then + value = str end end if formatkey then value = todisplay._format(formatkey, "%s", value) end - return string.format("${reset}%s%s${reset}", style, value) + local reset = todisplay._reset() + return reset .. todisplay._translate(style) .. value .. reset end -- print udata value with scalar format @@ -88,12 +130,70 @@ function todisplay._print_udata_scalar(value) end -- print table value with scalar format -function todisplay._print_table_scalar(value) - return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format") +function todisplay._print_table_scalar(value, expand) + if debug.getmetatable(value) or not expand then + return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format") + end + local pvalues = {} + local has_string_key = false + local as, ae = math.huge, -math.huge + + local k, v + for i = 1, 10 do + k, v = next(value, k) + if k == nil then break end + + if type(k) == "string" then + pvalues[k] = todisplay._print_scalar(v, false) + has_string_key = true + elseif type(k) == "number" and math.isint(k) then + pvalues[k] = todisplay._print_scalar(v, false) + as = math.min(k, as) + ae = math.max(k, ae) + else + -- no common table or array + return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format") + end + end + if k ~= nil then + -- to large + return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format") + end + + local results = {} + local is_arr = as >= 1 and ae <= 20 + if is_arr then + local nilstr + for i = 1, ae do + local pv = pvalues[i] + if pv == nil then + if not nilstr then + nilstr = todisplay._print_keyword("nil") + end + pv = nilstr + end + results[i] = pv + end + end + if has_string_key then + for pk, pv in pairs(pvalues) do + if type(pk) == "string" then + local pkstr = todisplay._print_string(pk, true) + table.insert(results, pkstr .. " = " .. pv) + elseif not is_arr then + local pkstr = todisplay._print_number(pk) + table.insert(results, pkstr .. " = " .. pv) + end + end + end + if #results == 0 then + return todisplay._translate("${reset}${color.dump.table}") .. "{ }" .. todisplay._reset() + end + return todisplay._translate("${reset}${color.dump.table}") .. "{ " .. table.concat(results, ", ") .." }" .. todisplay._reset() end -- print scalar value -function todisplay._print_scalar(value) +function todisplay._print_scalar(value, expand) if type(value) == "nil" or type(value) == "boolean" then return todisplay._print_keyword(value) elseif type(value) == "number" then @@ -105,10 +205,20 @@ function todisplay._print_scalar(value) elseif type(value) == "userdata" then return todisplay._print_udata_scalar(value) elseif type(value) == "table" then - return todisplay._print_table_scalar(value) + return todisplay._print_table_scalar(value, expand) else return todisplay._print_default_scalar(value, "${color.dump.default}", "text.dump.default_format") end end -return todisplay._print_scalar +function todisplay.print(value) + return todisplay._print_scalar(value, true) +end + +setmetatable(todisplay, { + __call = function (_, ...) + return todisplay.print(...) + end +}) + +return todisplay diff --git a/xmake/core/sandbox/modules/todisplay.lua b/xmake/core/sandbox/modules/todisplay.lua index 97824c9bc..e0ecf30cf 100644 --- a/xmake/core/sandbox/modules/todisplay.lua +++ b/xmake/core/sandbox/modules/todisplay.lua @@ -19,5 +19,5 @@ -- -- load module -return require("base/todisplay") +return require("base/todisplay").print 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}" } } } |
