aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/cmd/show.tl (renamed from src/luarocks/cmd/show.lua)95
1 files changed, 61 insertions, 34 deletions
diff --git a/src/luarocks/cmd/show.lua b/src/luarocks/cmd/show.tl
index 88cbbada..08dd9895 100644
--- a/src/luarocks/cmd/show.lua
+++ b/src/luarocks/cmd/show.tl
@@ -1,6 +1,12 @@
1--- Module implementing the LuaRocks "show" command. 1--- Module implementing the LuaRocks "show" command.
2-- Shows information about an installed rock. 2-- Shows information about an installed rock.
3local show = {} 3local record show
4 record Return
5 name: string
6 file: string
7 label: string
8 end
9end
4 10
5local queries = require("luarocks.queries") 11local queries = require("luarocks.queries")
6local search = require("luarocks.search") 12local search = require("luarocks.search")
@@ -13,7 +19,23 @@ local fetch = require("luarocks.fetch")
13local manif = require("luarocks.manif") 19local manif = require("luarocks.manif")
14local repos = require("luarocks.repos") 20local repos = require("luarocks.repos")
15 21
16function show.add_to_parser(parser) 22local type Return = show.Return
23local type RenderData = {string: string | {string}}
24
25local type Parser = require("luarocks.vendor.argparse").Parser
26
27local type Args = require("luarocks.core.types.args").Args
28
29local type Tree = require("luarocks.core.types.tree").Tree
30
31local type Query = require("luarocks.core.types.query").Query
32
33local type Rockspec = require("luarocks.core.types.rockspec").Rockspec
34local type Dependencies = require("luarocks.core.types.rockspec").Dependencies
35
36local type Entry = require("luarocks.core.types.manifest").Manifest.Entry
37
38function show.add_to_parser(parser: Parser)
17 local cmd = parser:command("show", [[ 39 local cmd = parser:command("show", [[
18Show information about an installed rock. 40Show information about an installed rock.
19 41
@@ -94,14 +116,14 @@ local porcelain_template = [[
94*ideps :indirect_dependency\t${name}\t${label} 116*ideps :indirect_dependency\t${name}\t${label}
95]] 117]]
96 118
97local function keys_as_string(t, sep) 119local function keys_as_string(t: {string: any}, sep: string): string
98 local keys = util.keys(t) 120 local keys = util.keys(t)
99 table.sort(keys) 121 table.sort(keys)
100 return table.concat(keys, sep or " ") 122 return table.concat(keys, sep or " ")
101end 123end
102 124
103local function word_wrap(line) 125local function word_wrap(line: string): string
104 local width = tonumber(os.getenv("COLUMNS")) or 80 126 local width = math.tointeger(os.getenv("COLUMNS")) or 80
105 if width > 80 then width = 80 end 127 if width > 80 then width = 80 end
106 if #line > width then 128 if #line > width then
107 local brk = width 129 local brk = width
@@ -115,7 +137,7 @@ local function word_wrap(line)
115 return line 137 return line
116end 138end
117 139
118local function format_text(text) 140local function format_text(text: string): string
119 text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2") 141 text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2")
120 local paragraphs = util.split_string(text, "\n\n") 142 local paragraphs = util.split_string(text, "\n\n")
121 for n, line in ipairs(paragraphs) do 143 for n, line in ipairs(paragraphs) do
@@ -124,18 +146,20 @@ local function format_text(text)
124 return (table.concat(paragraphs, "\n\n"):gsub("%s$", "")) 146 return (table.concat(paragraphs, "\n\n"):gsub("%s$", ""))
125end 147end
126 148
127local function installed_rock_label(dep, tree) 149local function installed_rock_label(dep: Query, tree: string | Tree): string
128 local installed, version 150 local installed, version: boolean, string
129 local rocks_provided = util.get_rocks_provided() 151 local rocks_provided = util.get_rocks_provided()
130 if rocks_provided[dep.name] then 152 if rocks_provided[dep.name] then
131 installed, version = true, rocks_provided[dep.name] 153 installed, version = true, rocks_provided[dep.name]
132 else 154 else
133 installed, version = search.pick_installed_rock(dep, tree) 155 local name: string
156 name, version = search.pick_installed_rock(dep, tree)
157 installed = name ~= nil
134 end 158 end
135 return installed and "using "..version or "missing" 159 return installed and "using "..version or "missing"
136end 160end
137 161
138local function render(template, data) 162local function render(template: string, data: RenderData): string
139 local out = {} 163 local out = {}
140 for cmd, var, line in template:gmatch("(.)([a-z]*)%s*:([^\n]*)\n") do 164 for cmd, var, line in template:gmatch("(.)([a-z]*)%s*:([^\n]*)\n") do
141 line = line:gsub("\\t", "\t") 165 line = line:gsub("\\t", "\t")
@@ -144,15 +168,18 @@ local function render(template, data)
144 table.insert(out, line) 168 table.insert(out, line)
145 elseif cmd == "?" or cmd == "*" or cmd == "!" then 169 elseif cmd == "?" or cmd == "*" or cmd == "!" then
146 if (cmd == "!" and d == nil) 170 if (cmd == "!" and d == nil)
147 or (cmd ~= "!" and (type(d) == "string" 171 or (cmd ~= "!" and (d is string
148 or (type(d) == "table" and next(d)))) then 172 or (d is {string} and next(d)))) then
149 local n = cmd == "*" and #d or 1 173 local n = d is {string} and #d or 1
174 if cmd ~= "*" then
175 n = 1
176 end
150 for i = 1, n do 177 for i = 1, n do
151 local tbl = cmd == "*" and d[i] or data 178 local tbl: string | {any: any} = cmd == "*" and d is {string} and d[i] or data
152 if type(tbl) == "string" then 179 if tbl is string then
153 tbl = tbl:gsub("%%", "%%%%") 180 tbl = tbl:gsub("%%", "%%%%")
154 end 181 end
155 table.insert(out, (line:gsub("${([a-z]+)}", tbl))) 182 table.insert(out, (line:gsub("${([a-z]+)}", tbl as string))) -- code works under the assumption that template is well behaved
156 end 183 end
157 end 184 end
158 end 185 end
@@ -160,7 +187,7 @@ local function render(template, data)
160 return table.concat(out, "\n") 187 return table.concat(out, "\n")
161end 188end
162 189
163local function adjust_path(name, version, basedir, pathname, suffix) 190local function adjust_path(name: string, version: string, basedir: string, pathname: string, suffix?: string): string
164 pathname = dir.path(basedir, pathname) 191 pathname = dir.path(basedir, pathname)
165 local vpathname = path.versioned_name(pathname, basedir, name, version) 192 local vpathname = path.versioned_name(pathname, basedir, name, version)
166 return (fs.exists(vpathname) 193 return (fs.exists(vpathname)
@@ -168,25 +195,25 @@ local function adjust_path(name, version, basedir, pathname, suffix)
168 or pathname) .. (suffix or "") 195 or pathname) .. (suffix or "")
169end 196end
170 197
171local function modules_to_list(name, version, repo) 198local function modules_to_list(name: string, version: string, repo: string | Tree): {Return}
172 local ret = {} 199 local ret: {Return} = {}
173 local rock_manifest = manif.load_rock_manifest(name, version, repo) 200 local rock_manifest = manif.load_rock_manifest(name, version, repo)
174 201
175 local lua_dir = path.deploy_lua_dir(repo) 202 local lua_dir = path.deploy_lua_dir(repo)
176 local lib_dir = path.deploy_lib_dir(repo) 203 local lib_dir = path.deploy_lib_dir(repo)
177 repos.recurse_rock_manifest_entry(rock_manifest.lua, function(pathname) 204 repos.recurse_rock_manifest_entry(rock_manifest.lua, function(pathname: string): boolean, string
178 table.insert(ret, { 205 table.insert(ret, {
179 name = path.path_to_module(pathname), 206 name = path.path_to_module(pathname),
180 file = adjust_path(name, version, lua_dir, pathname), 207 file = adjust_path(name, version, lua_dir, pathname),
181 }) 208 })
182 end) 209 end)
183 repos.recurse_rock_manifest_entry(rock_manifest.lib, function(pathname) 210 repos.recurse_rock_manifest_entry(rock_manifest.lib, function(pathname: string): boolean, string
184 table.insert(ret, { 211 table.insert(ret, {
185 name = path.path_to_module(pathname), 212 name = path.path_to_module(pathname),
186 file = adjust_path(name, version, lib_dir, pathname), 213 file = adjust_path(name, version, lib_dir, pathname),
187 }) 214 })
188 end) 215 end)
189 table.sort(ret, function(a, b) 216 table.sort(ret, function(a: Return, b: Return): boolean
190 if a.name == b.name then 217 if a.name == b.name then
191 return a.file < b.file 218 return a.file < b.file
192 end 219 end
@@ -195,18 +222,18 @@ local function modules_to_list(name, version, repo)
195 return ret 222 return ret
196end 223end
197 224
198local function commands_to_list(name, version, repo) 225local function commands_to_list(name: string, version: string, repo: string | Tree): {Return}
199 local ret = {} 226 local ret: {Return} = {}
200 local rock_manifest = manif.load_rock_manifest(name, version, repo) 227 local rock_manifest = manif.load_rock_manifest(name, version, repo)
201 228
202 local bin_dir = path.deploy_bin_dir(repo) 229 local bin_dir = path.deploy_bin_dir(repo)
203 repos.recurse_rock_manifest_entry(rock_manifest.bin, function(pathname) 230 repos.recurse_rock_manifest_entry(rock_manifest.bin, function(pathname: string): boolean, string
204 table.insert(ret, { 231 table.insert(ret, {
205 name = name, 232 name = name,
206 file = adjust_path(name, version, bin_dir, pathname, cfg.wrapper_suffix), 233 file = adjust_path(name, version, bin_dir, pathname, cfg.wrapper_suffix),
207 }) 234 })
208 end) 235 end)
209 table.sort(ret, function(a, b) 236 table.sort(ret, function(a: Return, b: Return): boolean
210 if a.name == b.name then 237 if a.name == b.name then
211 return a.file < b.file 238 return a.file < b.file
212 end 239 end
@@ -215,16 +242,16 @@ local function commands_to_list(name, version, repo)
215 return ret 242 return ret
216end 243end
217 244
218local function deps_to_list(dependencies, tree) 245local function deps_to_list(dependencies: Dependencies, tree: string | Tree): {Return}
219 local ret = {} 246 local ret: {Return} = {}
220 for _, dep in ipairs(dependencies or {}) do 247 for _, dep in ipairs(dependencies.queries or {}) do
221 table.insert(ret, { name = tostring(dep), label = installed_rock_label(dep, tree) }) 248 table.insert(ret, { name = tostring(dep), label = installed_rock_label(dep, tree) })
222 end 249 end
223 return ret 250 return ret
224end 251end
225 252
226local function indirect_deps(mdeps, rdeps, tree) 253local function indirect_deps(mdeps: {string: string}, rdeps: Dependencies, tree: string | Tree): {Return}
227 local ret = {} 254 local ret: {Return} = {}
228 local direct_deps = {} 255 local direct_deps = {}
229 for _, dep in ipairs(rdeps) do 256 for _, dep in ipairs(rdeps) do
230 direct_deps[dep] = true 257 direct_deps[dep] = true
@@ -237,7 +264,7 @@ local function indirect_deps(mdeps, rdeps, tree)
237 return ret 264 return ret
238end 265end
239 266
240local function show_rock(template, namespace, name, version, rockspec, repo, minfo, tree) 267local function show_rock(template: string, namespace: string, name: string, version: string, rockspec: Rockspec, repo: string | Tree, minfo: Entry, tree: string | Tree)
241 local desc = rockspec.description or {} 268 local desc = rockspec.description or {}
242 local data = { 269 local data = {
243 namespace = namespace, 270 namespace = namespace,
@@ -257,12 +284,12 @@ local function show_rock(template, namespace, name, version, rockspec, repo, min
257 deps = deps_to_list(rockspec.dependencies, tree), 284 deps = deps_to_list(rockspec.dependencies, tree),
258 ideps = indirect_deps(minfo.dependencies, rockspec.dependencies, tree), 285 ideps = indirect_deps(minfo.dependencies, rockspec.dependencies, tree),
259 } 286 }
260 util.printout(render(template, data)) 287 util.printout(render(template, data as RenderData))
261end 288end
262 289
263--- Driver function for "show" command. 290--- Driver function for "show" command.
264-- @return boolean: True if succeeded, nil on errors. 291-- @return boolean: True if succeeded, nil on errors.
265function show.command(args) 292function show.command(args: Args): boolean, string
266 local query = queries.new(args.rock, args.namespace, args.version, true) 293 local query = queries.new(args.rock, args.namespace, args.version, true)
267 294
268 local name, version, repo, repo_url = search.pick_installed_rock(query, args.tree) 295 local name, version, repo, repo_url = search.pick_installed_rock(query, args.tree)