summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-09-29 22:58:45 +0800
committerruki <[email protected]>2017-09-29 11:52:32 +0800
commitc1881132bf73e9914a244896506e9bc3954de301 (patch)
tree07eef86b31ed18d8e6de1fa195fae1a9c95fe506
parent80ae3ad93ee0de27eb3ba9eb42e5d711079e5ad5 (diff)
impl require search
-rw-r--r--xmake/actions/require/info.lua6
-rw-r--r--xmake/actions/require/package.lua50
-rw-r--r--xmake/actions/require/repository.lua25
-rw-r--r--xmake/actions/require/search.lua14
-rw-r--r--xmake/core/package/package.lua16
5 files changed, 106 insertions, 5 deletions
diff --git a/xmake/actions/require/info.lua b/xmake/actions/require/info.lua
index 85ceab31b..2232e55bc 100644
--- a/xmake/actions/require/info.lua
+++ b/xmake/actions/require/info.lua
@@ -49,7 +49,11 @@ function main(requires)
print("The package infos:")
-- list all packages
- for _, instance in ipairs(package.load_packages(requires)) do
+ local packages = package.load_packages(requires)
+ if packages and #packages > 0 then
+
+ -- get tis package
+ local instance = packages[#packages]
-- show package name
local requireinfo = instance:requireinfo() or {}
diff --git a/xmake/actions/require/package.lua b/xmake/actions/require/package.lua
index 39cad8dfb..216a4c88e 100644
--- a/xmake/actions/require/package.lua
+++ b/xmake/actions/require/package.lua
@@ -208,6 +208,42 @@ function _load_package(packagename, requireinfo)
return package
end
+-- search package package from project
+function _search_package_from_project(name)
+-- return core_package.search_from_project(name)
+end
+
+-- search package package from repositories
+function _search_package_from_repository(name)
+
+ -- search package directories from the given package name
+ local packages = {}
+ for packagename, packageinfo in pairs(repository.searchdirs(name)) do
+ local package = core_package.load_from_repository(packagename, packageinfo.is_global, packageinfo.packagedir)
+ if package then
+ table.insert(packages, package)
+ end
+ end
+
+ -- ok?
+ return packages
+end
+
+-- search package from the project and repositories
+function _search_package(name)
+
+ -- search package from project first
+ local packages = _search_package_from_project(name)
+
+ -- search package from repositories
+ if not packages or #packages == 0 then
+ packages = _search_package_from_repository(name)
+ end
+
+ -- ok?
+ return packages
+end
+
-- sort package deps
function _sort_packagedeps(package)
local orderdeps = {}
@@ -508,3 +544,17 @@ function install_packages(requires, requires_extra)
-- ok
return packages
end
+
+-- search packages
+function search_packages(names)
+
+ -- search all names
+ local results = {}
+ for _, name in ipairs(names) do
+ local packages = _search_package(name)
+ if packages then
+ results[name] = packages
+ end
+ end
+ return results
+end
diff --git a/xmake/actions/require/repository.lua b/xmake/actions/require/repository.lua
index 63a52ed61..cf7bc38c7 100644
--- a/xmake/actions/require/repository.lua
+++ b/xmake/actions/require/repository.lua
@@ -114,3 +114,28 @@ function packagedir(packagename, reponame)
end
end
+-- search package directories from repositories
+function searchdirs(name)
+
+ -- split name by '.'
+ local subdirs = "**" .. table.concat(name:split('%.'), "*" .. path.seperator() .. "*") .. "*"
+
+ -- find the package directories from all repositories
+ local packageinfos = {}
+ for _, repo in ipairs(repositories()) do
+
+ -- the package directory pattern
+ for _, file in ipairs(os.files(path.join(repository.directory(repo.global), repo.name, "packages", subdirs, "xmake.lua"))) do
+ packageinfos[path.basename(path.directory(file))] = {is_global = repo.global, packagedir = path.directory(file)}
+ end
+ end
+
+ -- search the package directories from the builtin packages directory
+ for _, file in ipairs(os.files(path.join(os.programdir(), "packages", subdirs, "xmake.lua"))) do
+ packageinfos[path.basename(path.directory(file))] = {is_global = repo.global, packagedir = path.directory(file)}
+ end
+
+ -- ok?
+ return packageinfos
+end
+
diff --git a/xmake/actions/require/search.lua b/xmake/actions/require/search.lua
index b9ea3b1c3..1eb4fdccc 100644
--- a/xmake/actions/require/search.lua
+++ b/xmake/actions/require/search.lua
@@ -48,6 +48,20 @@ function main(names)
-- show title
print("The package names:")
+ -- search packages
+ for name, packages in pairs(package.search_packages(names)) do
+ if #packages > 0 then
+
+ -- show name
+ print(" %s: ", name)
+
+ -- show packages
+ for _, instance in ipairs(packages) do
+ cprint(" -> ${magenta}%s${clear}: %s", instance:fullname(), instance:get("description") or "")
+ end
+ end
+ end
+
-- leave environment
environment.leave()
end
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index c74c7e474..070f5338e 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -547,13 +547,21 @@ function package.load_from_repository(packagename, is_global, packagedir, packag
return nil, errors
end
- -- check the package name
- if not results[packagename] then
- return nil, string.format("the package %s not found!", name)
+ -- get the package info
+ local packageinfo = nil
+ for name, info in pairs(results) do
+ packagename = name -- use the real package name in package() definition
+ packageinfo = info
+ break
+ end
+
+ -- check this package
+ if not packageinfo then
+ return nil, string.format("%s: the package %s not found!", scriptpath, packagename)
end
-- new an instance
- local instance, errors = _instance.new(packagename, results[packagename], package._interpreter():rootdir())
+ local instance, errors = _instance.new(packagename, packageinfo, package._interpreter():rootdir())
if not instance then
return nil, errors
end