summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-12-28 19:55:02 +0800
committerGitHub <[email protected]>2022-12-28 19:55:02 +0800
commitc6af394c4aa2fac804db111610e49e8adb13ad8c (patch)
tree459c2c03a5829f73cd0574e7705e6b3e102fe5b4
parent0dc8cd5b958f4f4a6fcfd33e2595e58047768bd5 (diff)
parente86e21923838c70920c529dac36409814902bb68 (diff)
Merge pull request #3214 from glcraft/completer
Completion on xrepo install packages
-rw-r--r--xmake/modules/private/utils/completer.lua60
-rw-r--r--xmake/modules/private/xrepo/action/install.lua3
-rw-r--r--xmake/modules/private/xrepo/quick_search/cache.lua89
-rw-r--r--xmake/modules/private/xrepo/quick_search/completion.lua62
-rw-r--r--xmake/plugins/repo/main.lua20
5 files changed, 214 insertions, 20 deletions
diff --git a/xmake/modules/private/utils/completer.lua b/xmake/modules/private/utils/completer.lua
index 2d882c9e3..e4427bc87 100644
--- a/xmake/modules/private/utils/completer.lua
+++ b/xmake/modules/private/utils/completer.lua
@@ -201,31 +201,53 @@ function completer:_complete_option_v(options, current, completing)
optvs = v
end
end
- local found_candidates = {}
- if opt then
- -- show candidates of values
- local values = opt.values
- if type(values) == "function" then
- values = values(completing, current)
+ -- transform values array to candidates array
+ local function _transform_values(values)
+ local candidates = {}
+ if #values > 0 and type(values[1]) == "string" then
+ for _, v in ipairs(values) do
+ table.insert(candidates, { value = v, is_complete = true })
+ end
+ else
+ for _, v in ipairs(values) do
+ v.is_complete=true
+ table.insert(candidates, v)
+ end
end
- for _, v in ipairs(values) do
- if tostring(v):startswith(completing) then
- table.insert(found_candidates, { value = v, is_complete = true })
+ return candidates
+ end
+ -- filter candidates with completing
+ local function _filter_candidates(candidates)
+ local found_candidates = {}
+ for _, v in ipairs(candidates) do
+ if v.value:find(completing,1,true) then
+ v.is_complete=true
+ table.insert(found_candidates, v)
end
end
+ return found_candidates
+ end
+ -- get completion candidates from values option
+ local function _values_into_candidates(values)
+ if values == nil then
+ return {}
+ elseif type(values) == "function" then
+ -- no need to filter result of values() as we consider values() already filter candidates
+ return _transform_values(values(completing, current))
+ else
+ return _filter_candidates(_transform_values(values))
+ end
+ end
+
+ local found_candidates = {}
+ if opt then
+ -- get candidates from values option
+ found_candidates = _values_into_candidates(opt.values)
end
if optvs and #found_candidates == 0 then
- -- show candidates of values
- local values = optvs.values
- if type(values) == "function" then
- values = values(completing)
- end
- for _, v in ipairs(values) do
- if tostring(v):startswith(completing) then
- table.insert(found_candidates, { value = v, is_complete = true })
- end
- end
+ -- get candidates from values option
+ found_candidates = _values_into_candidates(optvs.values)
end
self:_print_candidates(found_candidates)
return #found_candidates > 0
diff --git a/xmake/modules/private/xrepo/action/install.lua b/xmake/modules/private/xrepo/action/install.lua
index f650e5ff1..6eddea12e 100644
--- a/xmake/modules/private/xrepo/action/install.lua
+++ b/xmake/modules/private/xrepo/action/install.lua
@@ -79,7 +79,8 @@ function menu_options()
" - xrepo install -p iphoneos -a arm64 \"zlib >=1.2.0\"",
" - xrepo install -p android [--ndk=/xxx] -m debug \"pcre2 10.x\"",
" - xrepo install -p mingw [--mingw=/xxx] -k shared zlib",
- " - xrepo install conan::zlib/1.2.11 vcpkg::zlib"}
+ " - xrepo install conan::zlib/1.2.11 vcpkg::zlib",
+ values = function (complete, opt) return import("private.xrepo.quick_search.completion")(complete, opt) end}
}
-- show menu options
diff --git a/xmake/modules/private/xrepo/quick_search/cache.lua b/xmake/modules/private/xrepo/quick_search/cache.lua
new file mode 100644
index 000000000..6f145d6ca
--- /dev/null
+++ b/xmake/modules/private/xrepo/quick_search/cache.lua
@@ -0,0 +1,89 @@
+--!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-present, TBOOX Open Source Group.
+--
+-- @author glcraft
+-- @file cache.lua
+--
+
+import("core.base.json")
+import("core.cache.globalcache")
+import("core.package.package", {alias = "core_package"})
+import("private.action.require.impl.repository")
+
+local cache = globalcache.cache("quick_search")
+
+-- search package directories from repositories
+function _list_package_dirs()
+ -- find the package directories from all repositories
+ local unique = {}
+ local packageinfos = {}
+ for _, repo in ipairs(repository.repositories()) do
+ for _, file in ipairs(os.files(path.join(repo:directory(), "packages", "*", "*", "xmake.lua"))) do
+ local dir = path.directory(file)
+ local subdirname = path.basename(path.directory(dir))
+ if #subdirname == 1 then -- ignore l/luajit/port/xmake.lua
+ local packagename = path.filename(dir)
+ if not unique[packagename] then
+ table.insert(packageinfos, {name = packagename, repo = repo, packagedir = path.directory(file)})
+ unique[packagename] = true
+ end
+ end
+ end
+ end
+ return packageinfos
+end
+
+-- check cache content exists
+function _init()
+ if table.empty(cache:data()) then
+ update()
+ end
+end
+
+-- update the cache file
+function update()
+ for _, packageinfo in ipairs(_list_package_dirs()) do
+ local package = core_package.load_from_repository(packageinfo.name, packageinfo.repo, packageinfo.packagedir)
+ cache:set(packageinfo.name, {
+ description = package:description(),
+ versions = package:versions(),
+ })
+ end
+ cache:save()
+end
+
+-- remove the cache file
+function clear()
+ cache:clear()
+ cache:save()
+end
+
+-- get the cache data
+function get()
+ _init()
+ return cache:data()
+end
+
+function find(name)
+ _init()
+ local list_result = {}
+ for packagename, packagedata in pairs(cache:data()) do
+ if packagename:find(name, 1, true) then
+ table.insert(list_result, {name = packagename, data = packagedata})
+ end
+ end
+ return list_result
+end \ No newline at end of file
diff --git a/xmake/modules/private/xrepo/quick_search/completion.lua b/xmake/modules/private/xrepo/quick_search/completion.lua
new file mode 100644
index 000000000..8b8b802bb
--- /dev/null
+++ b/xmake/modules/private/xrepo/quick_search/completion.lua
@@ -0,0 +1,62 @@
+--!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-present, TBOOX Open Source Group.
+--
+-- @author glcraft
+-- @file complete.lua
+--
+
+import("private.xrepo.quick_search.cache")
+
+-- complete xrepo packages
+function _xmake_package_complete(complete, opt)
+ local candidates = {}
+ local found = cache.find(complete)
+ for _, candidate in ipairs(found) do
+ table.insert(candidates, {value = candidate.name, description = candidate.data.description})
+ end
+ return candidates
+end
+
+function main(complete, opt)
+ local prefix = ""
+
+ -- if help menu, do nothing
+ if opt.helpmenu then
+ return {}
+ end
+
+ -- check prefix if present
+ if complete:find("::", 1, true) then
+ prefix = complete:sub(1, complete:find("::", 1, true) - 1)
+ complete = complete:sub(complete:find("::", 1, true) + 2)
+ end
+
+ local packages
+
+ -- complete xmake packages
+ if prefix == "" or prefix == "xmake" then
+ packages = _xmake_package_complete(complete, opt)
+ end
+
+ -- to prevent shell completion misunderstandings,
+ -- we put back the prefix
+ if packages and prefix ~= "" then
+ for _, package in ipairs(packages) do
+ package.value = prefix .. "::" .. package.value
+ end
+ end
+ return packages or {}
+end \ No newline at end of file
diff --git a/xmake/plugins/repo/main.lua b/xmake/plugins/repo/main.lua
index 98262f3e9..9a1418128 100644
--- a/xmake/plugins/repo/main.lua
+++ b/xmake/plugins/repo/main.lua
@@ -29,6 +29,13 @@ import("net.proxy")
import("private.async.runjobs")
import("private.action.require.impl.environment")
+function _clear_quick_search_cache(is_global)
+ if is_global then
+ import("private.xrepo.quick_search.cache")
+ cache.clear()
+ end
+end
+
-- add repository url
function _add(name, url, branch, is_global)
@@ -55,6 +62,9 @@ function _add(name, url, branch, is_global)
-- leave environment
environment.leave()
+
+ -- clear quick search cache
+ _clear_quick_search_cache(is_global)
end
-- remove repository url
@@ -69,8 +79,12 @@ function _remove(name, is_global)
os.rmdir(repodir)
end
+ -- clear quick search cache
+ _clear_quick_search_cache(is_global)
+
-- trace
cprint("${bright}remove %s repository(%s): ok!", (is_global and "global" or "local"), name)
+
end
-- update repositories
@@ -112,6 +126,9 @@ function _update()
pulled[repodir] = true
end
end
+
+ -- clear quick search cache
+ _clear_quick_search_cache(true)
end
-- pull repositories
@@ -140,6 +157,9 @@ function _clear(is_global)
os.rmdir(repodir)
end
+ -- clear quick search cache
+ _clear_quick_search_cache(is_global)
+
-- trace
cprint("${color.success}clear %s repositories: ok!", (is_global and "global" or "local"))
end