aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-22 17:49:03 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-10-21 13:30:51 -0300
commite835049ce5eb087462da8ffc540efa5ea481ee34 (patch)
treef626b93a600fa25d46cf543e7c521d5eb8db29ed
parent2fc32e9c0183b74875d2341b3c38ea8789cec4a5 (diff)
downloadluarocks-e835049ce5eb087462da8ffc540efa5ea481ee34.tar.gz
luarocks-e835049ce5eb087462da8ffc540efa5ea481ee34.tar.bz2
luarocks-e835049ce5eb087462da8ffc540efa5ea481ee34.zip
Teal: convert luarocks.cmd.config
-rw-r--r--src/luarocks/cmd/config.tl (renamed from src/luarocks/cmd/config.lua)100
1 files changed, 55 insertions, 45 deletions
diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.tl
index d67711a0..53e3be5e 100644
--- a/src/luarocks/cmd/config.lua
+++ b/src/luarocks/cmd/config.tl
@@ -1,6 +1,7 @@
1--- Module implementing the LuaRocks "config" command. 1--- Module implementing the LuaRocks "config" command.
2-- Queries information about the LuaRocks configuration. 2-- Queries information about the LuaRocks configuration.
3local config_cmd = {} 3local record config_cmd
4end
4 5
5local persist = require("luarocks.persist") 6local persist = require("luarocks.persist")
6local config = require("luarocks.config") 7local config = require("luarocks.config")
@@ -11,7 +12,13 @@ local dir = require("luarocks.dir")
11local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
12local json = require("luarocks.vendor.dkjson") 13local json = require("luarocks.vendor.dkjson")
13 14
14function config_cmd.add_to_parser(parser) 15local type Parser = require("luarocks.vendor.argparse").Parser
16
17local type Args = require("luarocks.core.types.args").Args
18
19local type PersistableTable = require("luarocks.core.types.persist").PersistableTable
20
21function config_cmd.add_to_parser(parser: Parser)
15 local cmd = parser:command("config", [[ 22 local cmd = parser:command("config", [[
16Query information about the LuaRocks configuration. 23Query information about the LuaRocks configuration.
17 24
@@ -74,7 +81,7 @@ Query information about the LuaRocks configuration.
74 cmd:flag("--rock-trees"):hidden(true) 81 cmd:flag("--rock-trees"):hidden(true)
75end 82end
76 83
77local function config_file(conf) 84local function config_file(conf: cfg.conf): boolean, string
78 print(dir.normalize(conf.file)) 85 print(dir.normalize(conf.file))
79 if conf.found then 86 if conf.found then
80 return true 87 return true
@@ -83,8 +90,10 @@ local function config_file(conf)
83 end 90 end
84end 91end
85 92
86local function traverse_varstring(var, tbl, fn, missing_parent) 93local function traverse_varstring(var: string, tbl: PersistableTable, fn: function(PersistableTable, string | number): (boolean, string), missing_parent?: function(PersistableTable, string | number)): boolean, string
87 local k, r = var:match("^%[([0-9]+)%]%.(.*)$") 94 local k: string | number
95 local r: string
96 k, r = var:match("^%[([0-9]+)%]%.(.*)$")
88 if k then 97 if k then
89 k = tonumber(k) 98 k = tonumber(k)
90 else 99 else
@@ -100,27 +109,27 @@ local function traverse_varstring(var, tbl, fn, missing_parent)
100 end 109 end
101 110
102 if tbl[k] then 111 if tbl[k] then
103 return traverse_varstring(r, tbl[k], fn, missing_parent) 112 return traverse_varstring(r, tbl[k] as PersistableTable, fn, missing_parent)
104 else 113 else
105 return nil, "Unknown entry " .. k 114 return nil, "Unknown entry " .. tostring(k)
106 end 115 end
107 end 116 end
108 117
109 local i = var:match("^%[([0-9]+)%]$") 118 local i = var:match("^%[([0-9]+)%]$")
110 if i then 119 if i then
111 var = tonumber(i) 120 return fn(tbl, tonumber(i))
112 end 121 end
113 122
114 return fn(tbl, var) 123 return fn(tbl, var)
115end 124end
116 125
117local function print_json(value) 126local function print_json(value: {string : any}): boolean
118 print(json.encode(value)) 127 print(json.encode(value))
119 return true 128 return true
120end 129end
121 130
122local function print_entry(var, tbl, is_json) 131local function print_entry(var: string, tbl: PersistableTable, is_json: boolean): boolean, string
123 return traverse_varstring(var, tbl, function(t, k) 132 return traverse_varstring(var, tbl, function(t: PersistableTable, k: string): boolean, string
124 if not t[k] then 133 if not t[k] then
125 return nil, "Unknown entry " .. k 134 return nil, "Unknown entry " .. k
126 end 135 end
@@ -128,33 +137,34 @@ local function print_entry(var, tbl, is_json)
128 137
129 if not config.should_skip(var, val) then 138 if not config.should_skip(var, val) then
130 if is_json then 139 if is_json then
131 return print_json(val) 140 return print_json(val as {string : any})
132 elseif type(val) == "string" then 141 elseif type(val) == "string" then
133 print(val) 142 print(val)
134 else 143 else
135 persist.write_value(io.stdout, val) 144 persist.write_value(io.stdout as persist.Writer, val)
136 end 145 end
137 end 146 end
138 return true 147 return true
139 end) 148 end)
140end 149end
141 150
142local function infer_type(var) 151local function infer_type(var: string): string
143 local typ 152 local typ: string
144 traverse_varstring(var, cfg, function(t, k) 153 traverse_varstring(var, cfg as PersistableTable, function(t: PersistableTable, k: string): boolean, string --!
145 if t[k] ~= nil then 154 if t[k] then
146 typ = type(t[k]) 155 typ = type(t[k])
147 end 156 end
148 end) 157 end)
149 return typ 158 return typ
150end 159end
151 160
152local function write_entries(keys, scope, do_unset) 161local function write_entries(keys: {string: string}, scope: string, do_unset: boolean): boolean, string
162 local wrote: PersistableTable = {}
153 if scope == "project" and not cfg.config_files.project then 163 if scope == "project" and not cfg.config_files.project then
154 return nil, "Current directory is not part of a project. You may want to run `luarocks init`." 164 return nil, "Current directory is not part of a project. You may want to run `luarocks init`."
155 end 165 end
156 166
157 local file_name = cfg.config_files[scope].file 167 local file_name = (cfg.config_files as {string: {string: string}})[scope].file
158 168
159 local tbl, err = persist.load_config_file_if_basic(file_name, cfg) 169 local tbl, err = persist.load_config_file_if_basic(file_name, cfg)
160 if not tbl then 170 if not tbl then
@@ -162,12 +172,13 @@ local function write_entries(keys, scope, do_unset)
162 end 172 end
163 173
164 for var, val in util.sortedpairs(keys) do 174 for var, val in util.sortedpairs(keys) do
165 traverse_varstring(var, tbl, function(t, k) 175 traverse_varstring(var, tbl, function(t: PersistableTable, k: string | number): boolean, string
166 if do_unset then 176 if do_unset then
167 t[k] = nil 177 t[k] = nil
178 wrote[var] = ""
168 else 179 else
169 local typ = infer_type(var) 180 local typ = infer_type(var)
170 local v 181 local v: string | number | boolean
171 if typ == "number" and tonumber(val) then 182 if typ == "number" and tonumber(val) then
172 v = tonumber(val) 183 v = tonumber(val)
173 elseif typ == "boolean" and val == "true" then 184 elseif typ == "boolean" and val == "true" then
@@ -178,10 +189,10 @@ local function write_entries(keys, scope, do_unset)
178 v = val 189 v = val
179 end 190 end
180 t[k] = v 191 t[k] = v
181 keys[var] = v 192 wrote[var] = v
182 end 193 end
183 return true 194 return true
184 end, function(p, k) 195 end, function(p: PersistableTable, k: string | number)
185 p[k] = {} 196 p[k] = {}
186 end) 197 end)
187 end 198 end
@@ -194,11 +205,11 @@ local function write_entries(keys, scope, do_unset)
194 ok, err = persist.save_from_table(file_name, tbl) 205 ok, err = persist.save_from_table(file_name, tbl)
195 if ok then 206 if ok then
196 print(do_unset and "Removed" or "Wrote") 207 print(do_unset and "Removed" or "Wrote")
197 for var, val in util.sortedpairs(keys) do 208 for var, val in util.sortedpairs(wrote) do
198 if do_unset then 209 if do_unset then
199 print(("\t%s"):format(var)) 210 print(("\t%s"):format(var))
200 else 211 else
201 if type(val) == "string" then 212 if val is string then
202 print(("\t%s = %q"):format(var, val)) 213 print(("\t%s = %q"):format(var, val))
203 else 214 else
204 print(("\t%s = %s"):format(var, tostring(val))) 215 print(("\t%s = %s"):format(var, tostring(val)))
@@ -213,7 +224,7 @@ local function write_entries(keys, scope, do_unset)
213 end 224 end
214end 225end
215 226
216local function get_scope(args) 227local function get_scope(args: Args): string
217 return args.scope 228 return args.scope
218 or (args["local"] and "user") 229 or (args["local"] and "user")
219 or (args.project_tree and "project") 230 or (args.project_tree and "project")
@@ -222,7 +233,7 @@ local function get_scope(args)
222 or "user" 233 or "user"
223end 234end
224 235
225local function report_on_lua_incdir_config(value, lua_version) 236local function report_on_lua_incdir_config(value: string): boolean
226 local variables = { 237 local variables = {
227 ["LUA_DIR"] = cfg.variables.LUA_DIR, 238 ["LUA_DIR"] = cfg.variables.LUA_DIR,
228 ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, 239 ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR,
@@ -231,7 +242,7 @@ local function report_on_lua_incdir_config(value, lua_version)
231 ["LUA"] = cfg.variables.LUA, 242 ["LUA"] = cfg.variables.LUA,
232 } 243 }
233 244
234 local ok, err = deps.check_lua_incdir(variables, lua_version) 245 local ok, err = deps.check_lua_incdir(variables)
235 if not ok then 246 if not ok then
236 util.printerr() 247 util.printerr()
237 util.warning((err:gsub(" You can use.*", ""))) 248 util.warning((err:gsub(" You can use.*", "")))
@@ -239,7 +250,7 @@ local function report_on_lua_incdir_config(value, lua_version)
239 return ok 250 return ok
240end 251end
241 252
242local function report_on_lua_libdir_config(value, lua_version) 253local function report_on_lua_libdir_config(value: string): boolean
243 local variables = { 254 local variables = {
244 ["LUA_DIR"] = cfg.variables.LUA_DIR, 255 ["LUA_DIR"] = cfg.variables.LUA_DIR,
245 ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, 256 ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR,
@@ -248,7 +259,7 @@ local function report_on_lua_libdir_config(value, lua_version)
248 ["LUA"] = cfg.variables.LUA, 259 ["LUA"] = cfg.variables.LUA,
249 } 260 }
250 261
251 local ok, err, _, err_files = deps.check_lua_libdir(variables, lua_version) 262 local ok, err, _, err_files = deps.check_lua_libdir(variables)
252 if not ok then 263 if not ok then
253 util.printerr() 264 util.printerr()
254 util.warning((err:gsub(" You can use.*", ""))) 265 util.warning((err:gsub(" You can use.*", "")))
@@ -270,11 +281,10 @@ end
270 281
271--- Driver function for "config" command. 282--- Driver function for "config" command.
272-- @return boolean: True if succeeded, nil on errors. 283-- @return boolean: True if succeeded, nil on errors.
273function config_cmd.command(args) 284function config_cmd.command(args: Args): boolean, string
274 local lua_version = args.lua_version or cfg.lua_version
275 285
276 deps.check_lua_incdir(cfg.variables, lua_version) 286 deps.check_lua_incdir(cfg.variables)
277 deps.check_lua_libdir(cfg.variables, lua_version) 287 deps.check_lua_libdir(cfg.variables)
278 288
279 -- deprecated flags 289 -- deprecated flags
280 if args.lua_incdir then 290 if args.lua_incdir then
@@ -297,7 +307,7 @@ function config_cmd.command(args)
297 end 307 end
298 if args.rock_trees then 308 if args.rock_trees then
299 for _, tree in ipairs(cfg.rocks_trees) do 309 for _, tree in ipairs(cfg.rocks_trees) do
300 if type(tree) == "string" then 310 if tree is string then
301 util.printout(dir.normalize(tree)) 311 util.printout(dir.normalize(tree))
302 else 312 else
303 local name = tree.name and "\t"..tree.name or "" 313 local name = tree.name and "\t"..tree.name or ""
@@ -313,7 +323,7 @@ function config_cmd.command(args)
313 return nil, "Current directory is not part of a project. You may want to run `luarocks init`." 323 return nil, "Current directory is not part of a project. You may want to run `luarocks init`."
314 end 324 end
315 325
316 local location = cfg.config_files[scope] 326 local location = (cfg.config_files as {string: {string: string}})[scope]
317 if (not location) or (not location.file) then 327 if (not location) or (not location.file) then
318 return nil, "could not get config file location for " .. tostring(scope) .. " scope" 328 return nil, "could not get config file location for " .. tostring(scope) .. " scope"
319 end 329 end
@@ -336,13 +346,13 @@ function config_cmd.command(args)
336 ["variables.LUA"] = cfg.variables.LUA, 346 ["variables.LUA"] = cfg.variables.LUA,
337 } 347 }
338 if args.lua_version then 348 if args.lua_version then
339 local prefix = dir.dir_name(cfg.config_files[scope].file) 349 local prefix = dir.dir_name((cfg.config_files as {string: {string: string}})[scope].file)
340 persist.save_default_lua_version(prefix, args.lua_version) 350 persist.save_default_lua_version(prefix, args.lua_version)
341 end 351 end
342 local ok, err = write_entries(keys, scope, args.unset) 352 local ok, err = write_entries(keys, scope, args.unset)
343 if ok then 353 if ok then
344 local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR, lua_version) 354 local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR)
345 local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR, lua_version) 355 local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR)
346 if not (inc_ok and lib_ok) then 356 if not (inc_ok and lib_ok) then
347 warn_bad_c_config() 357 warn_bad_c_config()
348 end 358 end
@@ -359,16 +369,16 @@ function config_cmd.command(args)
359 if args.value or args.unset then 369 if args.value or args.unset then
360 local scope = get_scope(args) 370 local scope = get_scope(args)
361 371
362 local ok, err = write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) 372 local ok, err = write_entries({ [args.key] = args.value or "" }, scope, args.unset)
363 373
364 if ok then 374 if ok then
365 if args.key == "variables.LUA_INCDIR" then 375 if args.key == "variables.LUA_INCDIR" then
366 local ok = report_on_lua_incdir_config(args.value, lua_version) 376 local ok = report_on_lua_incdir_config(args.value)
367 if not ok then 377 if not ok then
368 warn_bad_c_config() 378 warn_bad_c_config()
369 end 379 end
370 elseif args.key == "variables.LUA_LIBDIR" then 380 elseif args.key == "variables.LUA_LIBDIR" then
371 local ok = report_on_lua_libdir_config(args.value, lua_version) 381 local ok = report_on_lua_libdir_config(args.value)
372 if not ok then 382 if not ok then
373 warn_bad_c_config() 383 warn_bad_c_config()
374 end 384 end
@@ -377,14 +387,14 @@ function config_cmd.command(args)
377 387
378 return ok, err 388 return ok, err
379 else 389 else
380 return print_entry(args.key, cfg, args.json) 390 return print_entry(args.key, cfg as PersistableTable, args.json)
381 end 391 end
382 end 392 end
383 393
384 if args.json then 394 if args.json then
385 return print_json(config.get_config_for_display(cfg)) 395 return print_json(config.get_config_for_display(cfg as PersistableTable) as {string : any})
386 else 396 else
387 print(config.to_string(cfg)) 397 print(config.to_string(cfg as PersistableTable))
388 return true 398 return true
389 end 399 end
390end 400end