diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2010-09-03 14:26:27 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2010-09-03 14:26:27 -0300 |
commit | 385a1ac6a593c1ca100f12fcbb30d345c3c35373 (patch) | |
tree | 6afa568349acd65ae1cf0923168ddab067a94bdd | |
parent | 05e12e918bbd3e2a58a42bf7e5e340ea90667f2e (diff) | |
download | luarocks-385a1ac6a593c1ca100f12fcbb30d345c3c35373.tar.gz luarocks-385a1ac6a593c1ca100f12fcbb30d345c3c35373.tar.bz2 luarocks-385a1ac6a593c1ca100f12fcbb30d345c3c35373.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.
-rw-r--r-- | src/luarocks/show.lua | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua new file mode 100644 index 00000000..60672fc5 --- /dev/null +++ b/src/luarocks/show.lua | |||
@@ -0,0 +1,133 @@ | |||
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 | ]] | ||
27 | |||
28 | local function keys_as_string(t, sep) | ||
29 | return table.concat(util.keys(t), sep or " ") | ||
30 | end | ||
31 | |||
32 | local function word_wrap(line) | ||
33 | local width = tonumber(os.getenv("COLUMNS")) or 80 | ||
34 | if width > 80 then width = 80 end | ||
35 | if #line > width then | ||
36 | local brk = width | ||
37 | while brk > 0 and line:sub(brk, brk) ~= " " do | ||
38 | brk = brk - 1 | ||
39 | end | ||
40 | if brk > 0 then | ||
41 | return line:sub(1, brk-1) .. "\n" .. word_wrap(line:sub(brk+1)) | ||
42 | end | ||
43 | end | ||
44 | return line | ||
45 | end | ||
46 | |||
47 | local function format_text(text) | ||
48 | text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2") | ||
49 | local paragraphs = util.split_string(text, "\n\n") | ||
50 | for n, line in ipairs(paragraphs) do | ||
51 | paragraphs[n] = word_wrap(line) | ||
52 | end | ||
53 | return (table.concat(paragraphs, "\n\n"):gsub("%s$", "")) | ||
54 | end | ||
55 | |||
56 | --- Driver function for "show" command. | ||
57 | -- @param name or nil: an existing package name. | ||
58 | -- @param version string or nil: a version may also be passed. | ||
59 | -- @return boolean: True if succeeded, nil on errors. | ||
60 | function run(...) | ||
61 | local flags, name, version = util.parse_flags(...) | ||
62 | if not name then | ||
63 | return nil, "Argument missing, see help." | ||
64 | end | ||
65 | local results = {} | ||
66 | local query = search.make_query(name, version) | ||
67 | query.exact_name = true | ||
68 | local tree_map = {} | ||
69 | for _, tree in ipairs(cfg.rocks_trees) do | ||
70 | local rocks_dir = path.rocks_dir(tree) | ||
71 | tree_map[rocks_dir] = tree | ||
72 | search.manifest_search(results, rocks_dir, query) | ||
73 | end | ||
74 | |||
75 | if not next(results) then -- | ||
76 | return nil,"cannot find package "..name.."\nUse 'list' to find installed rocks" | ||
77 | end | ||
78 | |||
79 | local version,repo_url | ||
80 | local package, versions = util.sortedpairs(results)() | ||
81 | --question: what do we do about multiple versions? This should | ||
82 | --give us the latest version on the last repo (which is usually the global one) | ||
83 | for vs, repos in util.sortedpairs(versions, deps.compare_versions) do | ||
84 | version = vs | ||
85 | for _, rp in ipairs(repos) do repo_url = rp.repo end | ||
86 | end | ||
87 | |||
88 | |||
89 | local repo = tree_map[repo_url] | ||
90 | local directory = path.install_dir(name,version,repo) | ||
91 | local rockspec_file = path.rockspec_file(name, version, repo) | ||
92 | local rockspec, err = fetch.load_local_rockspec(rockspec_file) | ||
93 | if not rockspec then return nil,err end | ||
94 | |||
95 | local descript = rockspec.description | ||
96 | local manifest, err = manif.load_manifest(repo_url) | ||
97 | if not manifest then return nil,err end | ||
98 | local minfo = manifest.repository[name][version][1] | ||
99 | |||
100 | if flags["dir"] then print(directory) | ||
101 | elseif flags["home"] then print(descript.homepage) | ||
102 | elseif flags["modules"] then print(keys_as_string(minfo.modules)) | ||
103 | elseif flags["deps"] then print(keys_as_string(minfo.dependencies)) | ||
104 | elseif flags["rockspec"] then print(rockspec_file) | ||
105 | elseif flags["mversion"] then print(version) | ||
106 | else | ||
107 | print() | ||
108 | print(rockspec.package.." "..rockspec.version.." - "..descript.summary) | ||
109 | print() | ||
110 | print(format_text(descript.detailed)) | ||
111 | print() | ||
112 | if descript.license then | ||
113 | print("License: ", descript.license) | ||
114 | end | ||
115 | if descript.homepage then | ||
116 | print("Homepage: ", descript.homepage) | ||
117 | end | ||
118 | print("Installed in: ", repo) | ||
119 | if next(minfo.modules) then | ||
120 | print() | ||
121 | print("Modules:") | ||
122 | print("\t"..keys_as_string(minfo.modules, "\n\t")) | ||
123 | end | ||
124 | if next(minfo.dependencies) then | ||
125 | print() | ||
126 | print("Depends on:") | ||
127 | print("\t"..keys_as_string(minfo.dependencies, "\n\t")) | ||
128 | end | ||
129 | print() | ||
130 | end | ||
131 | return true | ||
132 | end | ||
133 | |||