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