comparison share/lua/5.2/luarocks/show.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Module implementing the LuaRocks "show" command.
3 -- Shows information about an installed rock.
4 module("luarocks.show", package.seeall)
5
6 local search = require("luarocks.search")
7 local cfg = require("luarocks.cfg")
8 local util = require("luarocks.util")
9 local path = require("luarocks.path")
10 local dir = require("luarocks.dir")
11 local deps = require("luarocks.deps")
12 local fetch = require("luarocks.fetch")
13 local manif = require("luarocks.manif")
14 help_summary = "Shows information about an installed rock."
15
16 help = [[
17 <argument> is an existing package name.
18 Without any flags, show all module information.
19 With 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 --rock-tree local tree where rock is installed
27 --rock-dir data directory of the installed rock
28 ]]
29
30 local function keys_as_string(t, sep)
31 return table.concat(util.keys(t), sep or " ")
32 end
33
34 local 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
47 end
48
49 local 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$", ""))
56 end
57
58 local function module_name(mod, filename, name, version, repo, manifest)
59 local base_dir
60 if filename:match("%.lua$") then
61 base_dir = path.deploy_lua_dir(repo)
62 else
63 base_dir = path.deploy_lib_dir(repo)
64 end
65
66 return dir.path(base_dir, filename)
67 end
68
69 --- Driver function for "show" command.
70 -- @param name or nil: an existing package name.
71 -- @param version string or nil: a version may also be passed.
72 -- @return boolean: True if succeeded, nil on errors.
73 function run(...)
74 local flags, name, version = util.parse_flags(...)
75 if not name then
76 return nil, "Argument missing, see help."
77 end
78 local results = {}
79 local query = search.make_query(name, version)
80 query.exact_name = true
81 local tree_map = {}
82 local trees = cfg.rocks_trees
83 if flags["tree"] then
84 trees = { flags["tree"] }
85 end
86 for _, tree in ipairs(trees) do
87 local rocks_dir = path.rocks_dir(tree)
88 tree_map[rocks_dir] = tree
89 search.manifest_search(results, rocks_dir, query)
90 end
91
92 if not next(results) then --
93 return nil,"cannot find package "..name.." "..(version or "").."\nUse 'list' to find installed rocks."
94 end
95
96 version = nil
97 local repo_url
98 local package, versions = util.sortedpairs(results)()
99 --question: what do we do about multiple versions? This should
100 --give us the latest version on the last repo (which is usually the global one)
101 for vs, repositories in util.sortedpairs(versions, deps.compare_versions) do
102 if not version then version = vs end
103 for _, rp in ipairs(repositories) do repo_url = rp.repo end
104 end
105
106
107 local repo = tree_map[repo_url]
108 local directory = path.install_dir(name,version,repo)
109 local rockspec_file = path.rockspec_file(name, version, repo)
110 local rockspec, err = fetch.load_local_rockspec(rockspec_file)
111 if not rockspec then return nil,err end
112
113 local descript = rockspec.description or {}
114 local manifest, err = manif.load_manifest(repo_url)
115 if not manifest then return nil,err end
116 local minfo = manifest.repository[name][version][1]
117
118 if flags["rock-tree"] then util.printout(repo)
119 elseif flags["rock-dir"] then util.printout(directory)
120 elseif flags["home"] then util.printout(descript.homepage)
121 elseif flags["modules"] then util.printout(keys_as_string(minfo.modules))
122 elseif flags["deps"] then util.printout(keys_as_string(minfo.dependencies))
123 elseif flags["rockspec"] then util.printout(rockspec_file)
124 elseif flags["mversion"] then util.printout(version)
125 else
126 util.printout()
127 util.printout(rockspec.package.." "..rockspec.version.." - "..(descript.summary or ""))
128 util.printout()
129 if descript.detailed then
130 util.printout(format_text(descript.detailed))
131 util.printout()
132 end
133 if descript.license then
134 util.printout("License: ", descript.license)
135 end
136 if descript.homepage then
137 util.printout("Homepage: ", descript.homepage)
138 end
139 util.printout("Installed in: ", repo)
140 if next(minfo.modules) then
141 util.printout()
142 util.printout("Modules:")
143 for mod, filename in util.sortedpairs(minfo.modules) do
144 util.printout("\t"..mod.." ("..path.which(mod, filename, name, version, repo, manifest)..")")
145 end
146 end
147 if next(minfo.dependencies) then
148 util.printout()
149 util.printout("Depends on:")
150 util.printout("\t"..keys_as_string(minfo.dependencies, "\n\t"))
151 end
152 util.printout()
153 end
154 return true
155 end
156