aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2010-09-03 14:26:27 -0300
committerHisham Muhammad <hisham@gobolinux.org>2010-09-03 14:34:56 -0300
commit984adfca55d6fb8ae06e1b894ffca87cf3b90ecf (patch)
tree8adc525b5dba4b5dd41ab881cf0e131ad922483c
parent05e12e918bbd3e2a58a42bf7e5e340ea90667f2e (diff)
downloadluarocks-984adfca55d6fb8ae06e1b894ffca87cf3b90ecf.tar.gz
luarocks-984adfca55d6fb8ae06e1b894ffca87cf3b90ecf.tar.bz2
luarocks-984adfca55d6fb8ae06e1b894ffca87cf3b90ecf.zip
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.
-rwxr-xr-xsrc/bin/luarocks1
-rw-r--r--src/luarocks/search.lua3
-rw-r--r--src/luarocks/show.lua136
-rw-r--r--src/luarocks/util.lua27
4 files changed, 166 insertions, 1 deletions
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")
17commands.make = require("luarocks.make") 17commands.make = require("luarocks.make")
18commands.download = require("luarocks.download") 18commands.download = require("luarocks.download")
19commands.path = require("luarocks.path") 19commands.path = require("luarocks.path")
20commands.show = require("luarocks.show")
20 21
21command_line.run_command(...) 22command_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
283-- @param results table: A table where keys are package names and versions 283-- @param results table: A table where keys are package names and versions
284-- are tables matching version strings to an array of rocks servers. 284-- are tables matching version strings to an array of rocks servers.
285-- @param show_repo boolean or nil: Whether to show repository 285-- @param show_repo boolean or nil: Whether to show repository
286-- @param long boolean or nil: Whether to show module files
286-- information or not. Default is true. 287-- information or not. Default is true.
287function print_results(results, show_repo) 288function print_results(results, show_repo, long)
288 assert(type(results) == "table") 289 assert(type(results) == "table")
289 assert(type(show_repo) == "boolean" or not show_repo) 290 assert(type(show_repo) == "boolean" or not show_repo)
290 -- Force display of repo location for the time being 291 -- 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 @@
1
2--- Module implementing the LuaRocks "show" command.
3-- Shows information about an installed rock.
4module("luarocks.show", package.seeall)
5
6local search = require("luarocks.search")
7local cfg = require("luarocks.cfg")
8local util = require("luarocks.util")
9local path = require("luarocks.path")
10local dir = require("luarocks.dir")
11local deps = require("luarocks.deps")
12local fetch = require("luarocks.fetch")
13local manif = require("luarocks.manif")
14help_summary = "Shows information about an installed rock."
15
16help = [[
17<argument> is an existing package name.
18Without any flags, show all module information.
19With these flags, return only the desired information:
20
21--home home page of project
22--modules all modules provided by this package as used by require()
23--deps packages this package depends on
24--rockspec the full path of the rockspec file
25--mversion the package version
26--tree local tree where rock is installed
27--rock-dir data directory of the installed rock
28]]
29
30local function keys_as_string(t, sep)
31 return table.concat(util.keys(t), sep or " ")
32end
33
34local function word_wrap(line)
35 local width = tonumber(os.getenv("COLUMNS")) or 80
36 if width > 80 then width = 80 end
37 if #line > width then
38 local brk = width
39 while brk > 0 and line:sub(brk, brk) ~= " " do
40 brk = brk - 1
41 end
42 if brk > 0 then
43 return line:sub(1, brk-1) .. "\n" .. word_wrap(line:sub(brk+1))
44 end
45 end
46 return line
47end
48
49local function format_text(text)
50 text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2")
51 local paragraphs = util.split_string(text, "\n\n")
52 for n, line in ipairs(paragraphs) do
53 paragraphs[n] = word_wrap(line)
54 end
55 return (table.concat(paragraphs, "\n\n"):gsub("%s$", ""))
56end
57
58--- Driver function for "show" command.
59-- @param name or nil: an existing package name.
60-- @param version string or nil: a version may also be passed.
61-- @return boolean: True if succeeded, nil on errors.
62function run(...)
63 local flags, name, version = util.parse_flags(...)
64 if not name then
65 return nil, "Argument missing, see help."
66 end
67 local results = {}
68 local query = search.make_query(name, version)
69 query.exact_name = true
70 local tree_map = {}
71 for _, tree in ipairs(cfg.rocks_trees) do
72 local rocks_dir = path.rocks_dir(tree)
73 tree_map[rocks_dir] = tree
74 search.manifest_search(results, rocks_dir, query)
75 end
76
77 if not next(results) then --
78 return nil,"cannot find package "..name.."\nUse 'list' to find installed rocks"
79 end
80
81 local version,repo_url
82 local package, versions = util.sortedpairs(results)()
83 --question: what do we do about multiple versions? This should
84 --give us the latest version on the last repo (which is usually the global one)
85 for vs, repos in util.sortedpairs(versions, deps.compare_versions) do
86 version = vs
87 for _, rp in ipairs(repos) do repo_url = rp.repo end
88 end
89
90
91 local repo = tree_map[repo_url]
92 local directory = path.install_dir(name,version,repo)
93 local rockspec_file = path.rockspec_file(name, version, repo)
94 local rockspec, err = fetch.load_local_rockspec(rockspec_file)
95 if not rockspec then return nil,err end
96
97 local descript = rockspec.description
98 local manifest, err = manif.load_manifest(repo_url)
99 if not manifest then return nil,err end
100 local minfo = manifest.repository[name][version][1]
101
102 if flags["tree"] then print(repo)
103 elseif flags["rock-dir"] then print(directory)
104 elseif flags["home"] then print(descript.homepage)
105 elseif flags["modules"] then print(keys_as_string(minfo.modules))
106 elseif flags["deps"] then print(keys_as_string(minfo.dependencies))
107 elseif flags["rockspec"] then print(rockspec_file)
108 elseif flags["mversion"] then print(version)
109 else
110 print()
111 print(rockspec.package.." "..rockspec.version.." - "..descript.summary)
112 print()
113 print(format_text(descript.detailed))
114 print()
115 if descript.license then
116 print("License: ", descript.license)
117 end
118 if descript.homepage then
119 print("Homepage: ", descript.homepage)
120 end
121 print("Installed in: ", repo)
122 if next(minfo.modules) then
123 print()
124 print("Modules:")
125 print("\t"..keys_as_string(minfo.modules, "\n\t"))
126 end
127 if next(minfo.dependencies) then
128 print()
129 print("Depends on:")
130 print("\t"..keys_as_string(minfo.dependencies, "\n\t"))
131 end
132 print()
133 end
134 return true
135end
136
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)
243 return s:sub(1,#prefix) == prefix 243 return s:sub(1,#prefix) == prefix
244end 244end
245 245
246-- from http://lua-users.org/wiki/SplitJoin
247-- by PhilippeLhoste
248function split_string(str, delim, maxNb)
249 -- Eliminate bad cases...
250 if string.find(str, delim) == nil then
251 return { str }
252 end
253 if maxNb == nil or maxNb < 1 then
254 maxNb = 0 -- No limit
255 end
256 local result = {}
257 local pat = "(.-)" .. delim .. "()"
258 local nb = 0
259 local lastPos
260 for part, pos in string.gfind(str, pat) do
261 nb = nb + 1
262 result[nb] = part
263 lastPos = pos
264 if nb == maxNb then break end
265 end
266 -- Handle the last field
267 if nb ~= maxNb then
268 result[nb + 1] = string.sub(str, lastPos)
269 end
270 return result
271end
272
246--[[ 273--[[
247Author: Julio Manuel Fernandez-Diaz 274Author: Julio Manuel Fernandez-Diaz
248Date: January 12, 2007 275Date: January 12, 2007