From 58c8e709cc4677ec6fb5fdfcec1958fab6743709 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Tue, 30 Jun 2020 11:48:32 +0800 Subject: fix auto complete --- xmake/actions/build/xmake.lua | 2 +- xmake/actions/clean/xmake.lua | 2 +- xmake/actions/config/xmake.lua | 6 +--- xmake/actions/install/xmake.lua | 2 +- xmake/actions/package/xmake.lua | 2 +- xmake/actions/run/xmake.lua | 17 ++------- xmake/actions/uninstall/xmake.lua | 2 +- xmake/core/project/target.lua | 34 ++++++++++-------- xmake/modules/private/utils/complete_helper.lua | 46 +++++++++++++++++++++++++ xmake/plugins/show/main.lua | 2 +- xmake/plugins/show/xmake.lua | 3 +- 11 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 xmake/modules/private/utils/complete_helper.lua diff --git a/xmake/actions/build/xmake.lua b/xmake/actions/build/xmake.lua index 8d3b28291..700bb8996 100644 --- a/xmake/actions/build/xmake.lua +++ b/xmake/actions/build/xmake.lua @@ -59,7 +59,7 @@ task("build") , {} , {nil, "target", "v", nil , "The target name. It will build all default targets if this parameter is not specified." - , values = function () return try{ function () return table.keys(import("core.project.project").targets()) end } end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/actions/clean/xmake.lua b/xmake/actions/clean/xmake.lua index 0d9b265c1..2c0213d37 100644 --- a/xmake/actions/clean/xmake.lua +++ b/xmake/actions/clean/xmake.lua @@ -45,7 +45,7 @@ task("clean") , {} , {nil, "target", "v", nil , "The target name. It will clean all default targets if this parameter is not specified." - , values = function () return try{ function () return table.keys(import("core.project.project").targets()) end } end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/actions/config/xmake.lua b/xmake/actions/config/xmake.lua index c53844e5c..2c8bb97c1 100644 --- a/xmake/actions/config/xmake.lua +++ b/xmake/actions/config/xmake.lua @@ -206,11 +206,7 @@ task("config") , {} , {nil, "target", "v" , nil , "Configure for the given target." - , values = function () - return try { function () - return table.keys(import("core.project.project").targets()) - end } - end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/actions/install/xmake.lua b/xmake/actions/install/xmake.lua index 09ecc883f..3b859238a 100644 --- a/xmake/actions/install/xmake.lua +++ b/xmake/actions/install/xmake.lua @@ -50,7 +50,7 @@ task("install") , { } , {nil, "target", "v", nil , "The target name. It will install all default targets if this parameter is not specified." - , values = function () return try{ function () return table.keys(import("core.project.project").targets()) end } end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/actions/package/xmake.lua b/xmake/actions/package/xmake.lua index 13c3f4b28..d15c3eec1 100644 --- a/xmake/actions/package/xmake.lua +++ b/xmake/actions/package/xmake.lua @@ -45,7 +45,7 @@ task("package") , {'a', "all", "k", nil , "Package all targets." } , {} , {nil, "target", "v", nil , "The target name. It will package all default targets if this parameter is not specified." - , values = function () return try{ function () return table.keys(import("core.project.project").targets()) end } end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/actions/run/xmake.lua b/xmake/actions/run/xmake.lua index bfdce3af1..a44eb1158 100644 --- a/xmake/actions/run/xmake.lua +++ b/xmake/actions/run/xmake.lua @@ -49,21 +49,8 @@ task("run") " --workdir=`pwd`" } , {} , {nil, "target", "v", nil , "The target name. It will run all default targets if this parameter is not specified." - , values = function () - return try{ - function () - import("core.project.project") - local targets = project.targets() - local runable = {} - for k, v in pairs(targets) do - if v:script("run") or v:get("kind") == "binary" then - table.insert(runable, k) - end - end - return runable - end - } - end } + , values = function (complete, opt) return import("private.utils.complete_helper.runable_targets")(complete, opt) end } + , {nil, "arguments", "vs", nil , "The target arguments" } } } diff --git a/xmake/actions/uninstall/xmake.lua b/xmake/actions/uninstall/xmake.lua index 4f70922c2..882499668 100644 --- a/xmake/actions/uninstall/xmake.lua +++ b/xmake/actions/uninstall/xmake.lua @@ -52,7 +52,7 @@ task("uninstall") "or $ PREFIX=local xmake uninstall" } , { } , {nil, "target", "v", nil , "The target name. It will uninstall all default targets if this parameter is not specified." - , values = function () return try{ function () return table.keys(import("core.project.project").targets()) end } end } + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 652ae4758..d7f44c4af 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -881,24 +881,28 @@ function _instance:targetdir() -- get build directory targetdir = config.buildir() + end - -- append plat sub-directory - local plat = self:plat() - if plat then - targetdir = path.join(targetdir, plat) - end + if not targetdir then + return nil + end - -- append arch sub-directory - local arch = self:arch() - if arch then - targetdir = path.join(targetdir, arch) - end + -- append plat sub-directory + local plat = self:plat() + if plat then + targetdir = path.join(targetdir, plat) + end - -- append mode sub-directory - local mode = config.get("mode") - if mode then - targetdir = path.join(targetdir, mode) - end + -- append arch sub-directory + local arch = self:arch() + if arch then + targetdir = path.join(targetdir, arch) + end + + -- append mode sub-directory + local mode = config.get("mode") + if mode then + targetdir = path.join(targetdir, mode) end -- ok? diff --git a/xmake/modules/private/utils/complete_helper.lua b/xmake/modules/private/utils/complete_helper.lua new file mode 100644 index 000000000..a8a547dd7 --- /dev/null +++ b/xmake/modules/private/utils/complete_helper.lua @@ -0,0 +1,46 @@ +--!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 OpportunityLiu +-- @file complete_helper.lua +-- + +function targets() + return try + { + function () + import("core.project.project") + return table.keys(project.targets()) + end + } +end + +function runable_targets() + return try + { + function () + import("core.project.project") + local targets = project.targets() + local runable = {} + for k, v in pairs(targets) do + if v:script("run") or v:get("kind") == "binary" then + table.insert(runable, k) + end + end + return runable + end + } +end \ No newline at end of file diff --git a/xmake/plugins/show/main.lua b/xmake/plugins/show/main.lua index b1c7b3183..9503b1a75 100644 --- a/xmake/plugins/show/main.lua +++ b/xmake/plugins/show/main.lua @@ -23,7 +23,7 @@ import("core.base.option") -- show list function _show_list(name) - assert(import("lists." .. name, {try = true, anonymous = true}), "unknown list name(%s)", name)() + assert(#name > 0 and import("lists." .. name, {try = true, anonymous = true}), "unknown list name(%s)", name)() end -- main entry diff --git a/xmake/plugins/show/xmake.lua b/xmake/plugins/show/xmake.lua index 729d2eece..159eec1a6 100644 --- a/xmake/plugins/show/xmake.lua +++ b/xmake/plugins/show/xmake.lua @@ -42,7 +42,8 @@ task("show") , values = function (complete, opt) return import("list").lists() end}, - {'t', "target" , "kv" , nil , "Show the information of the given target."} + {'t', "target" , "kv" , nil , "Show the information of the given target." + , values = function (complete, opt) return import("private.utils.complete_helper.targets")(complete, opt) end } } } -- cgit v1.3.1 From 6dd3577da0219a388ea2b3331cb9956c1e915dcd Mon Sep 17 00:00:00 2001 From: Opportunity Date: Tue, 30 Jun 2020 12:08:46 +0800 Subject: set defaults for buildir --- xmake/core/project/config.lua | 37 ++++++++++++++++++------------------- xmake/core/project/target.lua | 19 +++++-------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index 837e45faf..90e4656a8 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -122,28 +122,27 @@ function config.options() return configs end --- get the buildir +-- get the buildir function config.buildir() - - -- get it - local buildir = config.get("buildir") - if buildir then - - -- get the absolute path first - if not path.is_absolute(buildir) then - local rootdir - if os.isdir(path.join(os.workingdir(), ".xmake")) then - -- we switch to independent working directory @see https://github.com/xmake-io/xmake/issues/820 - rootdir = os.workingdir() - else - rootdir = os.projectdir() - end - buildir = path.absolute(buildir, rootdir) - end - -- adjust path for the current directory - buildir = path.relative(buildir, os.curdir()) + -- get it + local buildir = config.get("buildir") or "build" + + -- get the absolute path first + if not path.is_absolute(buildir) then + local rootdir + if os.isdir(path.join(os.workingdir(), ".xmake")) then + -- we switch to independent working directory @see https://github.com/xmake-io/xmake/issues/820 + rootdir = os.workingdir() + else + rootdir = os.projectdir() + end + buildir = path.absolute(buildir, rootdir) end + + -- adjust path for the current directory + buildir = path.relative(buildir, os.curdir()) + return buildir end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index d7f44c4af..fc73acd25 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -875,17 +875,8 @@ end -- get the target directory function _instance:targetdir() - -- the target directory - local targetdir = self:get("targetdir") - if not targetdir then - - -- get build directory - targetdir = config.buildir() - end - - if not targetdir then - return nil - end + -- the target directory or build directory + local targetdir = self:get("targetdir") or config.buildir() -- append plat sub-directory local plat = self:plat() @@ -909,7 +900,7 @@ function _instance:targetdir() return targetdir end --- get the target file +-- get the target file function _instance:targetfile() -- the target directory @@ -920,7 +911,7 @@ function _instance:targetfile() -- only compile objects? no target file if targetkind == "object" then - return + return end -- make the target file name and attempt to use the format of linker first @@ -935,7 +926,7 @@ end function _instance:symbolfile() -- the target directory - local targetdir = self:targetdir() or config.buildir() + local targetdir = self:targetdir() assert(targetdir and type(targetdir) == "string") -- the symbol file name -- cgit v1.3.1 From 4c54385b422e3c191b3a2a13d849774132a601a8 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Tue, 30 Jun 2020 12:15:33 +0800 Subject: load config b4 complete --- xmake/modules/private/utils/complete_helper.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xmake/modules/private/utils/complete_helper.lua b/xmake/modules/private/utils/complete_helper.lua index a8a547dd7..4e8c38e8f 100644 --- a/xmake/modules/private/utils/complete_helper.lua +++ b/xmake/modules/private/utils/complete_helper.lua @@ -23,6 +23,11 @@ function targets() { function () import("core.project.project") + import("core.project.config") + + -- load config + config.load() + return table.keys(project.targets()) end } @@ -33,6 +38,11 @@ function runable_targets() { function () import("core.project.project") + import("core.project.config") + + -- load config + config.load() + local targets = project.targets() local runable = {} for k, v in pairs(targets) do -- cgit v1.3.1