Skip to content

Commit

Permalink
improve and cache searchdirs #5744
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Oct 22, 2024
1 parent 9a7d7d8 commit ddbef7a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 88 deletions.
88 changes: 27 additions & 61 deletions xmake/modules/package/manager/xmake/search_package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,38 @@

-- imports
import("core.base.semver")
import("core.package.package", {alias = "core_package"})
import("private.action.require.impl.repository")
import("private.xrepo.quick_search.cache")

-- search package from name
function _search_package_from_name(packages, name, opt)
for _, packageinfo in ipairs(repository.searchdirs(name)) do
local package = core_package.load_from_repository(packageinfo.name, packageinfo.packagedir, {repo = packageinfo.repo})
if package then
local repo = package:repo()
local version
local versions = package:versions()
if versions then
versions = table.copy(versions)
table.sort(versions, function (a, b) return semver.compare(a, b) > 0 end)
if opt.require_version then
for _, ver in ipairs(versions) do
if semver.satisfies(ver, opt.require_version) then
version = ver
end
function _search_package(packages, name, opt)
for _, packageinfo in ipairs(cache.find(name, {description = opt.description ~= false})) do
local packagename = packageinfo.name
local packagedata = packageinfo.data

local version
local versions = packageinfo.versions
if versions then
versions = table.copy(versions)
table.sort(versions, function (a, b) return semver.compare(a, b) > 0 end)
if opt.require_version then
for _, ver in ipairs(versions) do
if semver.satisfies(ver, opt.require_version) then
version = ver
end
else
version = versions[1]
end
end
if not opt.require_version or version then
packages[package:name()] = {name = package:name(), version = version, description = package:get("description"), reponame = repo and repo:name()}
else
version = versions[1]
end
end
end
end

-- search package from description
function _search_package_from_description(packages, name, opt)
for _, packageinfo in ipairs(repository.searchdirs("*")) do
if not packages[packageinfo.name] then
local package = core_package.load_from_repository(packageinfo.name, packageinfo.packagedir, {repo = packageinfo.repo})
if package then
local description = package:description()
if description and description:find(string.ipattern(name)) then
local repo = package:repo()
local version
local versions = package:versions()
if versions then
versions = table.copy(versions)
table.sort(versions, function (a, b) return semver.compare(a, b) > 0 end)
if opt.require_version then
for _, ver in ipairs(versions) do
if semver.satisfies(ver, opt.require_version) then
version = ver
end
end
else
version = versions[1]
end
end
description = description:gsub(string.ipattern(name), function (w)
return "${bright}" .. w .. "${clear}"
end)
if not opt.require_version or version then
packages[package:name()] = {name = package:name(), version = version, description = description, reponame = repo and repo:name()}
end
end
end
local description = packagedata.description
if description then
description = description:gsub(string.ipattern(name), function (w)
return "${bright}" .. w .. "${clear}"
end)
end

if not opt.require_version or version then
packages[packagename] = {name = packagename, version = version, description = description, reponame = packagedata.reponame}
end
end
end
Expand All @@ -95,10 +64,7 @@ end
function main(name, opt)
opt = opt or {}
local packages = {}
_search_package_from_name(packages, name, opt)
if opt.description ~= false then
_search_package_from_description(packages, name, opt)
end
_search_package(packages, name, opt)

local results = {}
for name, info in table.orderpairs(packages) do
Expand Down
22 changes: 0 additions & 22 deletions xmake/modules/private/action/require/impl/repository.lua
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,3 @@ function artifacts_manifest(packagename, version)
end
end

-- search package directories from repositories
function searchdirs(name)

-- find the package directories from all repositories
local unique = {}
local packageinfos = {}
for _, repo in ipairs(repositories()) do
for _, file in ipairs(os.files(path.join(repo:directory(), "packages", "*", string.ipattern("*" .. name .. "*"), "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

19 changes: 16 additions & 3 deletions xmake/modules/private/xrepo/quick_search/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function _list_package_dirs()
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)})
table.insert(packageinfos, {name = packagename, repo = repo, packagedir = dir})
unique[packagename] = true
end
end
Expand All @@ -58,6 +58,7 @@ function update()
for _, packageinfo in ipairs(_list_package_dirs()) do
local package = core_package.load_from_repository(packageinfo.name, packageinfo.packagedir, {repo = packageinfo.repo})
cache:set(packageinfo.name, {
reponame = package:repo() and package:repo():name(),
description = package:description(),
versions = package:versions(),
})
Expand All @@ -77,13 +78,25 @@ function get()
return cache:data()
end

function find(name)
-- find package
function find(name, opt)
_init()
opt = opt or {}
local list_result = {}
for packagename, packagedata in pairs(cache:data()) do
if packagename:startswith(name) then
local found = false
if opt.prefix then
found = packagename:startswith(name)
else
found = packagename:find(name)
end
if not found and opt.description and packagedata.description and packagedata.description:find(name) then
found = true
end
if found then
table.insert(list_result, {name = packagename, data = packagedata})
end
end
return list_result
end

4 changes: 2 additions & 2 deletions xmake/modules/private/xrepo/quick_search/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import("private.xrepo.quick_search.cache")
-- complete xrepo packages
function _xmake_package_complete(complete, opt)
local candidates = {}
local found = cache.find(complete)
local found = cache.find(complete, {prefix = true})
for _, candidate in ipairs(found) do
table.insert(candidates, {value = candidate.name, description = candidate.data.description})
end
Expand Down Expand Up @@ -59,4 +59,4 @@ function main(complete, opt)
end
end
return packages or {}
end
end

0 comments on commit ddbef7a

Please sign in to comment.