From 3364333877dc8a579f08b54099fda653948e07ab Mon Sep 17 00:00:00 2001 From: Opportunity Date: Mon, 20 Jan 2020 20:30:31 +0800 Subject: improve tab complete --- xmake/plugins/lua/main.lua | 134 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 xmake/plugins/lua/main.lua (limited to 'xmake/plugins/lua/main.lua') diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua new file mode 100644 index 000000000..7586ab65a --- /dev/null +++ b/xmake/plugins/lua/main.lua @@ -0,0 +1,134 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file main.lua +-- + +-- imports +import("core.base.option") +import("core.sandbox.module") +import("core.sandbox.sandbox") + +-- get all lua scripts +function scripts() + local names = {} + local files = os.match(path.join(os.scriptdir(), "scripts/*.lua")) + for _, file in ipairs(files) do + table.insert(names, path.basename(file)) + end + return names +end + +-- list all lua scripts +function _list() + print("scripts:") + for _, file in ipairs(scripts()) do + print(" " .. file) + end +end + +function _run_commanad(command, args) + local tmpfile = os.tmpfile() .. ".lua" + io.writefile(tmpfile, "function main(...)\n" .. command .. "\nend") + return _run_script(tmpfile, args) +end + +function _run_script(script, args) + + local func + local printresult = false + + -- 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)) + 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)) + else + + -- attempt to find the builtin module + local object = nil + for _, name in ipairs(script:split("%.")) do + object = object and object[name] or module.get(name) + if not object then + break + end + end + if object then + -- run builtin modules (xmake lua core.xxx.xxx) + vprint("running builtin module: %s", script) + func = object + else + -- run imported modules (xmake lua core.xxx.xxx) + vprint("running imported module: %s", script) + func = import(script, {anonymous = true}) + end + printresult = true + end + + 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)) + end +end + +function _get_args() + + local args = option.get("arguments") or {} + args.n = #args + for i, value in ipairs(args) do + if value:startswith('@') then + -- deserialize @ prefixed arg + 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 + end + end + end + + return args +end + +function main() + + if option.get("list") then + return _list() + end + + -- run command? + local script = option.get("script") + if option.get("command") and script then + return _run_commanad(script, _get_args()) + end + + -- get script + if script then + return _run_script(script, _get_args()) + end + + -- enter interactive mode + sandbox.interactive() +end -- cgit v1.3.1 From 08d00484b84526bf8b20f8ff9016ad2c0fc851ae Mon Sep 17 00:00:00 2001 From: Opportunity Date: Mon, 20 Jan 2020 22:18:58 +0800 Subject: fix code style --- xmake/actions/config/xmake.lua | 59 ++++++++++++++++++++++----------------- xmake/actions/update/xmake.lua | 26 ++++++++--------- xmake/core/base/task.lua | 8 ++---- xmake/core/theme/theme.lua | 10 +++---- xmake/platforms/windows/xmake.lua | 25 +++++++++-------- xmake/plugins/lua/main.lua | 2 +- xmake/plugins/lua/xmake.lua | 4 ++- 7 files changed, 71 insertions(+), 63 deletions(-) (limited to 'xmake/plugins/lua/main.lua') diff --git a/xmake/actions/config/xmake.lua b/xmake/actions/config/xmake.lua index 3dd486c96..91c5147f4 100644 --- a/xmake/actions/config/xmake.lua +++ b/xmake/actions/config/xmake.lua @@ -11,7 +11,7 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. --- +-- -- Copyright (C) 2015-2020, TBOOX Open Source Group. -- -- @author ruki @@ -39,16 +39,21 @@ task("config") , shortname = 'f' -- options - , options = + , options = { {'c', "clean", "k", nil , "Clean the cached configure and configure all again." } , {nil, "menu", "k", nil , "Configure project with a menu-driven user interface." } - , {nil, "require", "kv", nil , "Require all dependent packages?", - " - y: force to enable", - " - n: disable" } + , {nil, "require", "kv", nil , "Require all dependent packages?" + , values = function (complete) + if complete then + return {"yes", "no"} + else + return {"y: force to enable", "n: disable" } + end + end } , {category = "."} - , {'p', "plat", "kv", "$(host)" , "Compile for the given platform." + , {'p', "plat", "kv", "$(host)" , "Compile for the given platform." , values = function () return import("core.platform.platform").plats() end } @@ -56,7 +61,7 @@ task("config") -- show the description of all architectures function () - -- import platform + -- import platform import("core.platform.platform") -- make description @@ -74,8 +79,8 @@ task("config") -- get it return description end - , values = function (completing, opt) - if not completing then return end + , values = function (complete, opt) + if not complete then return end -- import import("core.platform.platform") @@ -105,44 +110,48 @@ task("config") -- get it return arches:to_array() end } - , {'m', "mode", "kv", "release" , "Compile for the given mode.", - " - debug", - " - release", - " - ... (custom)" - , values = function (completing) - if not completing then return end - return {"debug", "release", "profile", "check", "coverage"} + , {'m', "mode", "kv", "release" , "Compile for the given mode." + , values = function (complete) + + local modes = try { + function() return import("core.project.project").modes() end + } or {"debug", "release"} + table.sort(modes) + if not complete then + table.insert(modes, "... (custom)") + end + return modes end } , {'k', "kind", "kv", "static" , "Compile for the given target kind." , values = {"static", "shared", "binary"} } , {nil, "host", "kv", "$(host)" , "The Current Host Environment." } -- show project menu options - , function () + , function () - -- import project menu + -- import project menu import("core.project.menu") - -- get project menu options - return menu.options() + -- get project menu options + return menu.options() end , {category = "Cross Complation Configuration"} - , {nil, "cross", "kv", nil, "The Cross Toolchains Prefix" + , {nil, "cross", "kv", nil, "The Cross Toolchains Prefix" , "e.g." , " - i386-mingw32-" , " - arm-linux-androideabi-" } - , {nil, "bin", "kv", nil, "The Cross Toolchains Bin Directory" + , {nil, "bin", "kv", nil, "The Cross Toolchains Bin Directory" , "e.g." , " - sdk/bin (/arm-linux-gcc ..)" } - , {nil, "sdk", "kv", nil, "The Cross SDK Directory" + , {nil, "sdk", "kv", nil, "The Cross SDK Directory" , "e.g." , " - sdk/bin" , " - sdk/lib" , " - sdk/include" } -- show language menu options - , function () + , function () -- import language menu import("core.language.menu") @@ -152,7 +161,7 @@ task("config") end -- show platform menu options - , function () + , function () -- import platform menu import("core.platform.menu") diff --git a/xmake/actions/update/xmake.lua b/xmake/actions/update/xmake.lua index c4b281638..74dcd0ba9 100644 --- a/xmake/actions/update/xmake.lua +++ b/xmake/actions/update/xmake.lua @@ -49,14 +49,14 @@ task("update") " https://github.com/xmake-io/xmake", " github:xmake-io/xmake#~2.2.3", " git://github.com/xmake-io/xmake.git#master", - values = function (completing) - if not completing then + values = function (complete) + if not complete then return end return try{ function () import("private.action.update.fetch_version") - local seg = completing:split('#', { plain = true, limit = 2, strict = true }) + local seg = complete:split('#', { plain = true, limit = 2, strict = true }) if #seg == 1 then if seg[1]:find(':', 1, true) then -- incomplete custom source @@ -67,19 +67,19 @@ task("update") end local versions = fetch_version(seg[1]) - if versions.is_official then - for i,v in ipairs(versions.tags) do - if v:startswith("v") and #v > 5 then - versions.tags[i] = v:sub(2) - end + for i,v in ipairs(versions.tags) do + if v:startswith("v") and #v > 5 then + versions.tags[i] = v:sub(2) end - return table.join(versions.branches, table.reverse(versions.tags)) + end + local candidates = table.join("latest", versions.branches, table.reverse(versions.tags)) + if versions.is_official then + return candidates else - local values = table.join(versions.branches, table.reverse(versions.tags)) - for i, v in ipairs(values) do - values[i] = seg[1] .. "#" .. v + for i, v in ipairs(candidates) do + candidates[i] = seg[1] .. "#" .. v end - return values + return candidates end end } end} diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 39a3711da..7ffcdfcc4 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -37,12 +37,8 @@ function task.common_options() { {'q', "quiet", "k", nil, "Quiet operation." } , {'y', "yes", "k", nil, "Input yes by default if need user confirm." } - , {nil, "confirm", "kv", nil, "Input the given result if need user confirm.", - " - y|yes", - " - n|no", - " - d|def" - , values = function (complete) - if not complete then return end + , {nil, "confirm", "kv", nil, "Input the given result if need user confirm." + , values = function () return {"yes", "no", "def"} end } , {'v', "verbose", "k", nil, "Print lots of verbose information for users." } diff --git a/xmake/core/theme/theme.lua b/xmake/core/theme/theme.lua index adf5024fb..f8070c44b 100644 --- a/xmake/core/theme/theme.lua +++ b/xmake/core/theme/theme.lua @@ -11,7 +11,7 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. --- +-- -- Copyright (C) 2015-2020, TBOOX Open Source Group. -- -- @author ruki @@ -59,7 +59,7 @@ function theme._interpreter() -- init interpreter local interp = interpreter.new() assert(interp) - + -- define apis interp:api_define(theme._apis()) @@ -72,7 +72,7 @@ end -- get theme apis function theme._apis() - return + return { keyvalues = { @@ -90,7 +90,7 @@ function theme.directories() local dirs = theme._DIRS or { path.join(global.directory(), "themes") , path.join(os.programdir(), "themes") } - + -- save directories to cache theme._DIRS = dirs return dirs @@ -101,7 +101,7 @@ function theme.names() local paths = {} for _, dir in ipairs(theme.directories()) do - table.join2(paths, (os.match(path.join(dir, "*", "xmake.lua"), "f"))) + table.join2(paths, (os.files(path.join(dir, "*", "xmake.lua")))) end for i, v in ipairs(paths) do local value = path.split(v) diff --git a/xmake/platforms/windows/xmake.lua b/xmake/platforms/windows/xmake.lua index 0dc7b91f8..43c7acccb 100644 --- a/xmake/platforms/windows/xmake.lua +++ b/xmake/platforms/windows/xmake.lua @@ -11,7 +11,7 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. --- +-- -- Copyright (C) 2015-2020, TBOOX Open Source Group. -- -- @author ruki @@ -50,10 +50,10 @@ platform("windows") -- set menu set_menu { - config = - { + config = + { {category = "Visual Studio SDK Configuration" } - , {nil, "vs", "kv", "auto", "The Microsoft Visual Studio" + , {nil, "vs", "kv", "auto", "The Microsoft Visual Studio" , " e.g. --vs=2017" } , {nil, "vs_toolset", "kv", nil, "The Microsoft Visual Studio Toolset Version" , " e.g. --vs_toolset=14.0" } @@ -67,19 +67,20 @@ platform("windows") , {category = "WDK Configuration" } , {nil, "wdk", "kv", "auto", "The WDK Directory" } , {nil, "wdk_sdkver", "kv", "auto", "The WDK Version" } - , {nil, "wdk_winver", "kv", "auto", "The WDK Windows Version", - " - win10_rs3", - " - win10", - " - win81", - " - win8", - " - win7", - " - win7_[sp1|sp2|sp3]" } + , {nil, "wdk_winver", "kv", "auto", "The WDK Windows Version" + , values = function (complete) + if complete then + return {"win10_rs3", "win10", "win81", "win8", "win7_sp3", "win7_sp2", "win7_sp1", "win7"} + else + return {"win10[|_rs3]", "win81", "win8", "win7[|_sp1|_sp2|_sp3]"} + end + end } , {category = "Vcpkg Configuration" } , {nil, "vcpkg", "kv", "auto", "The Vcpkg Directory" } } , global = - { + { {category = "Visual Studio SDK Configuration" } , {nil, "vs", "kv", "auto", "The Microsoft Visual Studio" } , {category = "Cuda SDK Configuration" } diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 7586ab65a..c0ed6e3d0 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -26,7 +26,7 @@ import("core.sandbox.sandbox") -- get all lua scripts function scripts() local names = {} - local files = os.match(path.join(os.scriptdir(), "scripts/*.lua")) + local files = os.files(path.join(os.scriptdir(), "scripts/*.lua")) for _, file in ipairs(files) do table.insert(names, path.basename(file)) end diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 7b7bb8dd3..23d00b145 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -52,7 +52,9 @@ task("lua") " - xmake lua core.xxx.xxx", " - xmake lua -c 'print(...)' hello xmake!" , values = function (complete, opt) - if not complete or opt.command or opt.list then return end + if not complete or opt.command or opt.list then + return + end return import("main.scripts")() end } -- cgit v1.3.1