From 984adfca55d6fb8ae06e1b894ffca87cf3b90ecf Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 3 Sep 2010 14:26:27 -0300 Subject: Add the 'show' command by Steve Donovan, plus some changes. Added an error check and some layout improvements, to make it look a bit like index.html from the repository. The only 'major' change was to present the repository path instead of the rock directory. I thought it would be confusing to present that path there since it's not where the modules are. --- src/bin/luarocks | 1 + src/luarocks/search.lua | 3 +- src/luarocks/show.lua | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ src/luarocks/util.lua | 27 ++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/luarocks/show.lua (limited to 'src') diff --git a/src/bin/luarocks b/src/bin/luarocks index fd0c1d9c..5da4bc25 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -17,5 +17,6 @@ commands.remove = require("luarocks.remove") commands.make = require("luarocks.make") commands.download = require("luarocks.download") commands.path = require("luarocks.path") +commands.show = require("luarocks.show") command_line.run_command(...) diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 6303eb8c..97b86a45 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -283,8 +283,9 @@ end -- @param results table: A table where keys are package names and versions -- are tables matching version strings to an array of rocks servers. -- @param show_repo boolean or nil: Whether to show repository +-- @param long boolean or nil: Whether to show module files -- information or not. Default is true. -function print_results(results, show_repo) +function print_results(results, show_repo, long) assert(type(results) == "table") assert(type(show_repo) == "boolean" or not show_repo) -- Force display of repo location for the time being diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua new file mode 100644 index 00000000..fcdffb9c --- /dev/null +++ b/src/luarocks/show.lua @@ -0,0 +1,136 @@ + +--- Module implementing the LuaRocks "show" command. +-- Shows information about an installed rock. +module("luarocks.show", package.seeall) + +local search = require("luarocks.search") +local cfg = require("luarocks.cfg") +local util = require("luarocks.util") +local path = require("luarocks.path") +local dir = require("luarocks.dir") +local deps = require("luarocks.deps") +local fetch = require("luarocks.fetch") +local manif = require("luarocks.manif") +help_summary = "Shows information about an installed rock." + +help = [[ + is an existing package name. +Without any flags, show all module information. +With these flags, return only the desired information: + +--home home page of project +--modules all modules provided by this package as used by require() +--deps packages this package depends on +--rockspec the full path of the rockspec file +--mversion the package version +--tree local tree where rock is installed +--rock-dir data directory of the installed rock +]] + +local function keys_as_string(t, sep) + return table.concat(util.keys(t), sep or " ") +end + +local function word_wrap(line) + local width = tonumber(os.getenv("COLUMNS")) or 80 + if width > 80 then width = 80 end + if #line > width then + local brk = width + while brk > 0 and line:sub(brk, brk) ~= " " do + brk = brk - 1 + end + if brk > 0 then + return line:sub(1, brk-1) .. "\n" .. word_wrap(line:sub(brk+1)) + end + end + return line +end + +local function format_text(text) + text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2") + local paragraphs = util.split_string(text, "\n\n") + for n, line in ipairs(paragraphs) do + paragraphs[n] = word_wrap(line) + end + return (table.concat(paragraphs, "\n\n"):gsub("%s$", "")) +end + +--- Driver function for "show" command. +-- @param name or nil: an existing package name. +-- @param version string or nil: a version may also be passed. +-- @return boolean: True if succeeded, nil on errors. +function run(...) + local flags, name, version = util.parse_flags(...) + if not name then + return nil, "Argument missing, see help." + end + local results = {} + local query = search.make_query(name, version) + query.exact_name = true + local tree_map = {} + for _, tree in ipairs(cfg.rocks_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.."\nUse 'list' to find installed rocks" + end + + local version,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, repos in util.sortedpairs(versions, deps.compare_versions) do + version = vs + for _, rp in ipairs(repos) do repo_url = rp.repo end + end + + + local repo = tree_map[repo_url] + local directory = path.install_dir(name,version,repo) + 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 + + local descript = rockspec.description + local manifest, err = manif.load_manifest(repo_url) + if not manifest then return nil,err end + local minfo = manifest.repository[name][version][1] + + if flags["tree"] then print(repo) + elseif flags["rock-dir"] then print(directory) + elseif flags["home"] then print(descript.homepage) + elseif flags["modules"] then print(keys_as_string(minfo.modules)) + elseif flags["deps"] then print(keys_as_string(minfo.dependencies)) + elseif flags["rockspec"] then print(rockspec_file) + elseif flags["mversion"] then print(version) + else + print() + print(rockspec.package.." "..rockspec.version.." - "..descript.summary) + print() + print(format_text(descript.detailed)) + print() + if descript.license then + print("License: ", descript.license) + end + if descript.homepage then + print("Homepage: ", descript.homepage) + end + print("Installed in: ", repo) + if next(minfo.modules) then + print() + print("Modules:") + print("\t"..keys_as_string(minfo.modules, "\n\t")) + end + if next(minfo.dependencies) then + print() + print("Depends on:") + print("\t"..keys_as_string(minfo.dependencies, "\n\t")) + end + print() + end + return true +end + diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 164dd260..ed70b2ba 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -243,6 +243,33 @@ function starts_with(s, prefix) return s:sub(1,#prefix) == prefix end +-- from http://lua-users.org/wiki/SplitJoin +-- by PhilippeLhoste +function split_string(str, delim, maxNb) + -- Eliminate bad cases... + if string.find(str, delim) == nil then + return { str } + end + if maxNb == nil or maxNb < 1 then + maxNb = 0 -- No limit + end + local result = {} + local pat = "(.-)" .. delim .. "()" + local nb = 0 + local lastPos + for part, pos in string.gfind(str, pat) do + nb = nb + 1 + result[nb] = part + lastPos = pos + if nb == maxNb then break end + end + -- Handle the last field + if nb ~= maxNb then + result[nb + 1] = string.sub(str, lastPos) + end + return result +end + --[[ Author: Julio Manuel Fernandez-Diaz Date: January 12, 2007 -- cgit v1.2.3-55-g6feb