From c53a3c84000cfeaa5749abe3a5917b55f74c21de Mon Sep 17 00:00:00 2001 From: Hisham Date: Thu, 8 Sep 2016 11:28:52 -0300 Subject: Make `pack` use the same logic as `show` for finding a rock. --- src/luarocks/doc.lua | 4 ++-- src/luarocks/pack.lua | 36 +++++++++--------------------------- src/luarocks/search.lua | 33 +++++++++++++++++++++++++++++++++ src/luarocks/show.lua | 37 ++----------------------------------- 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/src/luarocks/doc.lua b/src/luarocks/doc.lua index ec2b1110..15460b31 100644 --- a/src/luarocks/doc.lua +++ b/src/luarocks/doc.lua @@ -5,7 +5,7 @@ local doc = {} package.loaded["luarocks.doc"] = doc local util = require("luarocks.util") -local show = require("luarocks.show") +local search = require("luarocks.search") local path = require("luarocks.path") local dir = require("luarocks.dir") local fetch = require("luarocks.fetch") @@ -63,7 +63,7 @@ function doc.command(flags, name, version) return nil, "Argument missing. "..util.see_help("doc") end - local iname, iversion, repo = show.pick_installed_rock(name, version, flags["tree"]) + local iname, iversion, repo = search.pick_installed_rock(name, version, flags["tree"]) if not iname then util.printout(name..(version and " "..version or "").." is not installed. Looking for it in the rocks servers...") return try_to_open_homepage(name, version) diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index 277cf246..fb40a576 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua @@ -85,38 +85,20 @@ end -- @param name string: Name of package to pack. -- @param version string or nil: A version number may also be passed. +-- @param tree string or nil: An optional tree to pick the package from. -- @return string or (nil, string): The filename of the resulting -- .src.rock file; or nil and an error message. -local function do_pack_binary_rock(name, version) +local function do_pack_binary_rock(name, version, tree) assert(type(name) == "string") assert(type(version) == "string" or not version) - local query = search.make_query(name, version) - query.exact_name = true - local results = {} - - search.manifest_search(results, cfg.rocks_dir, query) - - if not next(results) then - return nil, "'"..name.."' does not seem to be an installed rock." - end - - local versions = results[name] - - if not version then - local first = next(versions) - if next(versions, first) then - return nil, "Please specify which version of '"..name.."' to pack." - end - version = first - end - if not version:match("[^-]+%-%d+") then - return nil, "Expected version "..version.." in version-revision format." + local repo, repo_url + name, version, repo, repo_url = search.pick_installed_rock(name, version, tree) + if not name then + return nil, version end - - local info = versions[version][1] - - local root = path.root_dir(info.repo) + + local root = path.root_dir(repo_url) local prefix = path.install_dir(name, version, root) if not fs.exists(prefix) then return nil, "'"..name.." "..version.."' does not seem to be an installed rock." @@ -202,7 +184,7 @@ function pack.command(flags, arg, version) if arg:match(".*%.rockspec") then file, err = pack.pack_source_rock(arg) else - file, err = do_pack_binary_rock(arg, version) + file, err = do_pack_binary_rock(arg, version, flags["tree"]) end if err then return nil, err diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index eaa321d5..d22c2a18 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -416,6 +416,39 @@ function search.act_on_src_or_rockspec(action, name, version, ...) return action(url, ...) end +function search.pick_installed_rock(name, version, given_tree) + local results = {} + local query = search.make_query(name, version) + query.exact_name = true + local tree_map = {} + local trees = cfg.rocks_trees + if given_tree then + trees = { given_tree } + end + for _, tree in ipairs(trees) do + local rocks_dir = path.rocks_dir(tree) + tree_map[rocks_dir] = tree + search.manifest_search(results, rocks_dir, query) + end + + if not next(results) then -- + return nil,"cannot find package "..name.." "..(version or "").."\nUse 'list' to find installed rocks." + end + + version = nil + local repo_url + local package, versions = util.sortedpairs(results)() + --question: what do we do about multiple versions? This should + --give us the latest version on the last repo (which is usually the global one) + for vs, repositories in util.sortedpairs(versions, deps.compare_versions) do + if not version then version = vs end + for _, rp in ipairs(repositories) do repo_url = rp.repo end + end + + local repo = tree_map[repo_url] + return name, version, repo, repo_url +end + --- Driver function for "search" command. -- @param name string: A substring of a rock name to search. -- @param version string or nil: a version may also be passed. diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua index 01860e78..88f9512d 100644 --- a/src/luarocks/show.lua +++ b/src/luarocks/show.lua @@ -58,45 +58,12 @@ local function format_text(text) return (table.concat(paragraphs, "\n\n"):gsub("%s$", "")) end -function show.pick_installed_rock(name, version, tree) - local results = {} - local query = search.make_query(name, version) - query.exact_name = true - local tree_map = {} - local trees = cfg.rocks_trees - if tree then - trees = { tree } - end - for _, tree in ipairs(trees) do - local rocks_dir = path.rocks_dir(tree) - tree_map[rocks_dir] = tree - search.manifest_search(results, rocks_dir, query) - end - - if not next(results) then -- - return nil,"cannot find package "..name.." "..(version or "").."\nUse 'list' to find installed rocks." - end - - version = nil - local repo_url - local package, versions = util.sortedpairs(results)() - --question: what do we do about multiple versions? This should - --give us the latest version on the last repo (which is usually the global one) - for vs, repositories in util.sortedpairs(versions, deps.compare_versions) do - if not version then version = vs end - for _, rp in ipairs(repositories) do repo_url = rp.repo end - end - - local repo = tree_map[repo_url] - return name, version, repo, repo_url -end - local function installed_rock_label(name, tree) local installed, version if cfg.rocks_provided[name] then installed, version = true, cfg.rocks_provided[name] else - installed, version = show.pick_installed_rock(name, nil, tree) + installed, version = search.pick_installed_rock(name, nil, tree) end return installed and "(using "..version..")" or "(missing)" end @@ -111,7 +78,7 @@ function show.command(flags, name, version) end local repo, repo_url - name, version, repo, repo_url = show.pick_installed_rock(name, version, flags["tree"]) + name, version, repo, repo_url = search.pick_installed_rock(name, version, flags["tree"]) if not name then return nil, version end -- cgit v1.2.3-55-g6feb