diff --git a/xmake/modules/package/manager/xmake/search_package.lua b/xmake/modules/package/manager/xmake/search_package.lua index 6c12a4dcc36..175af81c166 100644 --- a/xmake/modules/package/manager/xmake/search_package.lua +++ b/xmake/modules/package/manager/xmake/search_package.lua @@ -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 @@ -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 diff --git a/xmake/modules/private/action/require/impl/repository.lua b/xmake/modules/private/action/require/impl/repository.lua index 74b97efda9a..aab908e5b7d 100644 --- a/xmake/modules/private/action/require/impl/repository.lua +++ b/xmake/modules/private/action/require/impl/repository.lua @@ -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 - diff --git a/xmake/modules/private/xrepo/quick_search/cache.lua b/xmake/modules/private/xrepo/quick_search/cache.lua index e14074d24ba..9ba14207604 100644 --- a/xmake/modules/private/xrepo/quick_search/cache.lua +++ b/xmake/modules/private/xrepo/quick_search/cache.lua @@ -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 @@ -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(), }) @@ -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 + diff --git a/xmake/modules/private/xrepo/quick_search/completion.lua b/xmake/modules/private/xrepo/quick_search/completion.lua index 8b8b802bbe4..b1a26e11728 100644 --- a/xmake/modules/private/xrepo/quick_search/completion.lua +++ b/xmake/modules/private/xrepo/quick_search/completion.lua @@ -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 @@ -59,4 +59,4 @@ function main(complete, opt) end end return packages or {} -end \ No newline at end of file +end