From d33b56cd60d623500c88b8bd2924e6674fc22776 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 13 Apr 2018 12:29:15 -0300 Subject: Display namespaces in `luarocks show` --- spec/show_spec.lua | 5 +++++ src/luarocks/cmd/show.lua | 8 ++++--- src/luarocks/path.lua | 19 ++++++++++++++++ src/luarocks/search.lua | 55 +++++++++++++++++++++-------------------------- src/luarocks/util.lua | 1 + 5 files changed, 54 insertions(+), 34 deletions(-) diff --git a/spec/show_spec.lua b/spec/show_spec.lua index 4880106d..a3f856fd 100644 --- a/spec/show_spec.lua +++ b/spec/show_spec.lua @@ -88,4 +88,9 @@ describe("LuaRocks show #blackbox #b_show", function() assert.match("a_build_dep", run.luarocks("show has_build_dep --build-deps")) end) + it("shows #namespaces via --rock-namespace", function() + assert(run.luarocks_bool("build a_user/a_rock --server=" .. testing_paths.fixtures_dir .. "/a_repo" )) + assert.match("a_user", run.luarocks("show a_rock --rock-namespace")) + end) + end) diff --git a/src/luarocks/cmd/show.lua b/src/luarocks/cmd/show.lua index dfd2f3de..b76ff778 100644 --- a/src/luarocks/cmd/show.lua +++ b/src/luarocks/cmd/show.lua @@ -91,8 +91,9 @@ function show.command(flags, name, version) if not name then return nil, version end - + local tree = path.rocks_tree_to_string(repo) local directory = path.install_dir(name, version, repo) + local namespace = path.read_namespace(name, version, tree) local rockspec_file = path.rockspec_file(name, version, repo) local rockspec, err = fetch.load_local_rockspec(rockspec_file) if not rockspec then return nil,err end @@ -102,7 +103,8 @@ function show.command(flags, name, version) if not manifest then return nil,err end local minfo = manifest.repository[name][version][1] - if flags["rock-tree"] then util.printout(path.rocks_tree_to_string(repo)) + if flags["rock-tree"] then util.printout(tree) + elseif flags["rock-namespace"] then util.printout(namespace) elseif flags["rock-dir"] then util.printout(directory) elseif flags["home"] then util.printout(descript.homepage) elseif flags["issues"] then util.printout(descript.issues_url) @@ -120,7 +122,7 @@ function show.command(flags, name, version) elseif flags["mversion"] then util.printout(version) else util.printout() - util.printout(rockspec.package.." "..rockspec.version.." - "..(descript.summary or "")) + util.printout((namespace and namespace .."/" or "") .. rockspec.package.." "..rockspec.version.." - "..(descript.summary or "")) util.printout() if descript.detailed then util.printout(format_text(descript.detailed)) diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index f6b12727..4babcb30 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -287,4 +287,23 @@ function path.map_trees(deps_mode, fn, ...) return result end +--- Get the namespace of a locally-installed rock, if any. +-- @param name string: The rock name, without a namespace. +-- @param version string: The rock version. +-- @param tree string: The local tree to use. +-- @return string?: The namespace if it exists, or nil. +function path.read_namespace(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + assert(type(tree) == "string") + + local namespace + local fd = io.open(path.rock_namespace_file(name, version, tree), "r") + if fd then + namespace = fd:read("*a") + fd:close() + end + return namespace +end + return path diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 49098642..57f39bdc 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -25,7 +25,8 @@ function search.store_result(result_tree, result) if not result_tree[name][version] then result_tree[name][version] = {} end table.insert(result_tree[name][version], { arch = result.arch, - repo = result.repo + repo = result.repo, + namespace = result.namespace, }) end @@ -47,25 +48,6 @@ local function store_if_match(result_tree, result, query) end end ---- Get the namespace of a locally-installed rock, if any. --- @param name string: The rock name, without a namespace. --- @param version string: The rock version. --- @param tree string: The local tree to use. --- @return string?: The namespace if it exists, or nil. -local function read_namespace(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - assert(type(tree) == "string") - - local namespace - local fd = io.open(path.rock_namespace_file(name, version, tree), "r") - if fd then - namespace = fd:read("*a") - fd:close() - end - return namespace -end - --- Perform search on a local repository. -- @param repo string: The pathname of the local repository. -- @param query table: a query object. @@ -96,7 +78,7 @@ function search.disk_search(repo, query, result_tree) elseif fs.is_dir(pathname) then for version in fs.dir(pathname) do if version:match("-%d+$") then - local namespace = read_namespace(name, version, repo) + local namespace = path.read_namespace(name, version, repo) local result = results.new(name, version, repo, "installed", namespace) store_if_match(result_tree, result, query) end @@ -132,7 +114,7 @@ local function manifest_search(result_tree, repo, query, lua_version, is_local) end for name, versions in pairs(manifest.repository) do for version, items in pairs(versions) do - local namespace = is_local and read_namespace(name, version, repo) or query.namespace + local namespace = is_local and path.read_namespace(name, version, repo) or query.namespace for _, item in ipairs(items) do local result = results.new(name, version, repo, item.arch, namespace) store_if_match(result_tree, result, query) @@ -292,21 +274,32 @@ function search.print_result_tree(result_tree, porcelain) assert(type(result_tree) == "table") assert(type(porcelain) == "boolean" or not porcelain) - for package, versions in util.sortedpairs(result_tree) do - if not porcelain then - util.printout(package) + if porcelain then + for package, versions in util.sortedpairs(result_tree) do + for version, repos in util.sortedpairs(versions, vers.compare_versions) do + for _, repo in ipairs(repos) do + local nrepo = dir.normalize(repo.repo) + util.printout(package, version, repo.arch, nrepo, repo.namespace) + end + end end + return + end + + for package, versions in util.sortedpairs(result_tree) do + local namespaces = {} for version, repos in util.sortedpairs(versions, vers.compare_versions) do for _, repo in ipairs(repos) do + local key = repo.namespace or "" + local list = namespaces[key] or {} + namespaces[key] = list + repo.repo = dir.normalize(repo.repo) - if porcelain then - util.printout(package, version, repo.arch, repo.repo) - else - util.printout(" "..version.." ("..repo.arch..") - "..repo.repo) - end + table.insert(list, " "..version.." ("..repo.arch..") - "..repo.repo) end end - if not porcelain then + for key, list in util.sortedpairs(namespaces) do + util.printout(key == "" and package or key .. "/" .. package) util.printout() end end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index c797fa80..6dd96e0d 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -130,6 +130,7 @@ local supported_flags = { ["porcelain"] = true, ["quick"] = true, ["rock-dir"] = true, + ["rock-namespace"] = true, ["rock-tree"] = true, ["rock-trees"] = true, ["rockspec"] = true, -- cgit v1.2.3-55-g6feb