aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-07-25 18:12:29 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commit1f01bc98f91546a03b777202ce0f52f827dbd321 (patch)
tree822f11b41d2ad82ba886aadd6be31306b2e52520
parent2bce023b2397b79731150d68d7b3c053959abf9c (diff)
downloadluarocks-1f01bc98f91546a03b777202ce0f52f827dbd321.tar.gz
luarocks-1f01bc98f91546a03b777202ce0f52f827dbd321.tar.bz2
luarocks-1f01bc98f91546a03b777202ce0f52f827dbd321.zip
almost finished util and persist
-rw-r--r--src/luarocks/core/cfg.d.tl3
-rw-r--r--src/luarocks/core/util.tl7
-rw-r--r--src/luarocks/fs.d.tl1
-rw-r--r--src/luarocks/path.tl2
-rw-r--r--src/luarocks/persist.tl17
-rw-r--r--src/luarocks/util.tl84
6 files changed, 41 insertions, 73 deletions
diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl
index 1cc35061..f21bd64c 100644
--- a/src/luarocks/core/cfg.d.tl
+++ b/src/luarocks/core/cfg.d.tl
@@ -54,8 +54,9 @@ local record cfg
54 record cache 54 record cache
55 luajit_version_checked: boolean 55 luajit_version_checked: boolean
56 luajit_version: string 56 luajit_version: string
57 rocks_provided: {Rockspec} 57 rocks_provided: {string: string} --? right type? infered from
58 end 58 end
59
59 record variables 60 record variables
60 LUA: string 61 LUA: string
61 end 62 end
diff --git a/src/luarocks/core/util.tl b/src/luarocks/core/util.tl
index e934c52a..d23d1b59 100644
--- a/src/luarocks/core/util.tl
+++ b/src/luarocks/core/util.tl
@@ -5,15 +5,14 @@ local record util
5 5
6 sub_orders: {K: Ordering<K>} 6 sub_orders: {K: Ordering<K>}
7 end 7 end
8
9 type SortBy<K> = table.SortFunction<K> | util.Ordering<K>
8end 10end
9 11
10-------------------------------------------------------------------------------- 12--------------------------------------------------------------------------------
11 13
12local dir_sep = package.config:sub(1, 1) 14local dir_sep = package.config:sub(1, 1)
13 15
14local type SortBy<K> = table.SortFunction<K> | util.Ordering<K>
15
16
17--- Run a process and read a its output. 16--- Run a process and read a its output.
18-- Equivalent to io.popen(cmd):read("*l"), except that it 17-- Equivalent to io.popen(cmd):read("*l"), except that it
19-- closes the fd right away. 18-- closes the fd right away.
@@ -293,7 +292,7 @@ end
293-- for that key, which is returned by the iterator as the third value after the key 292-- for that key, which is returned by the iterator as the third value after the key
294-- and the value. 293-- and the value.
295-- @return function: the iterator function. 294-- @return function: the iterator function.
296function util.sortedpairs<K, V>(tbl: {K: V}, sort_by?: SortBy<K>): function(): K, V, util.Ordering<K> 295function util.sortedpairs<K, V>(tbl: {K: V}, sort_by?: util.SortBy<K>): function(): K, V, util.Ordering<K>
297 local keys = util.keys(tbl) 296 local keys = util.keys(tbl)
298 local sub_orders: {K: util.Ordering<K>} = nil 297 local sub_orders: {K: util.Ordering<K>} = nil
299 298
diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl
index 0334329d..fe96b742 100644
--- a/src/luarocks/fs.d.tl
+++ b/src/luarocks/fs.d.tl
@@ -9,6 +9,7 @@ local record fs
9 -- util 9 -- util
10 is_dir: function(dir: string): boolean 10 is_dir: function(dir: string): boolean
11 dir: function(dir: string): function(): string --? right iterator 11 dir: function(dir: string): function(): string --? right iterator
12 make_dir: function
12 is_file: function(file: string): boolean 13 is_file: function(file: string): boolean
13 current_dir: function(): string 14 current_dir: function(): string
14 list_dir: function(string): {string} 15 list_dir: function(string): {string}
diff --git a/src/luarocks/path.tl b/src/luarocks/path.tl
index ffcf26c8..5682a2f7 100644
--- a/src/luarocks/path.tl
+++ b/src/luarocks/path.tl
@@ -72,7 +72,7 @@ end
72-- @param tree string or nil: If given, specifies the local tree to use. 72-- @param tree string or nil: If given, specifies the local tree to use.
73-- @return string: The resulting path -- does not guarantee that 73-- @return string: The resulting path -- does not guarantee that
74-- the package (and by extension, the path) exists. 74-- the package (and by extension, the path) exists.
75function path.install_dir(name: string, version: string, tree: string | Tree): string 75function path.install_dir(name: string, version: string, tree?: string | Tree): string
76 assert(not name:match("/")) 76 assert(not name:match("/"))
77 return dir.path(path.rocks_dir(tree), name, version) 77 return dir.path(path.rocks_dir(tree), name, version)
78end 78end
diff --git a/src/luarocks/persist.tl b/src/luarocks/persist.tl
index f02fcfee..95fc39d9 100644
--- a/src/luarocks/persist.tl
+++ b/src/luarocks/persist.tl
@@ -7,7 +7,6 @@ local record persist
7end 7end
8 8
9local core = require("luarocks.core.persist") 9local core = require("luarocks.core.persist")
10local coreutil = require("luarocks.core.util")
11local util = require("luarocks.util") 10local util = require("luarocks.util")
12local dir = require("luarocks.dir") 11local dir = require("luarocks.dir")
13local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
@@ -15,13 +14,11 @@ local fs = require("luarocks.fs")
15persist.run_file = core.run_file 14persist.run_file = core.run_file
16persist.load_into_table = core.load_into_table 15persist.load_into_table = core.load_into_table
17 16
18local type SortBy<K> = table.SortFunction<K> | coreutil.Ordering<K>
19
20local interface Writer 17local interface Writer
21 write: function(self: Writer, data: string) 18 write: function(self: Writer, data: string)
22end 19end
23 20
24local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: SortBy<number | string>) 21local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy<number | string>)
25 22
26--- Write a value as Lua code. 23--- Write a value as Lua code.
27-- This function handles only numbers and strings, invoking write_table 24-- This function handles only numbers and strings, invoking write_table
@@ -31,7 +28,7 @@ local write_table: function(out: Writer, tbl: {number | string: number| string},
31-- @param level number: the indentation level 28-- @param level number: the indentation level
32-- @param sub_order table: optional prioritization table 29-- @param sub_order table: optional prioritization table
33-- @see write_table 30-- @see write_table
34function persist.write_value(out: Writer, v: any, level: integer, sub_order?: SortBy<number | string>) 31function persist.write_value(out: Writer, v: any, level: integer, sub_order?: util.SortBy<number | string>)
35 if v is {number | string: number | string} then 32 if v is {number | string: number | string} then
36 level = level or 0 33 level = level or 0
37 write_table(out, v, level + 1, sub_order) 34 write_table(out, v, level + 1, sub_order)
@@ -105,7 +102,7 @@ end
105-- @param tbl table: the table to be written. 102-- @param tbl table: the table to be written.
106-- @param level number: the indentation level 103-- @param level number: the indentation level
107-- @param field_order table: optional prioritization table 104-- @param field_order table: optional prioritization table
108write_table = function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: SortBy<number | string>) 105write_table = function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy<number | string>)
109 out:write("{") 106 out:write("{")
110 local sep = "\n" 107 local sep = "\n"
111 local indentation = " " 108 local indentation = " "
@@ -144,7 +141,7 @@ end
144-- @param tbl table: the table to be written. 141-- @param tbl table: the table to be written.
145-- @param field_order table: optional prioritization table 142-- @param field_order table: optional prioritization table
146-- @return true if successful; nil and error message if failed. 143-- @return true if successful; nil and error message if failed.
147local function write_table_as_assignments(out: Writer, tbl: {number | string: number| string}, field_order: SortBy<number | string>): boolean, string 144local function write_table_as_assignments(out: Writer, tbl: {number | string: number| string}, field_order: util.SortBy<number | string>): boolean, string
148 for k, v, sub_order in util.sortedpairs(tbl, field_order) do 145 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
149 if not is_valid_plain_key(k) then --? tostring 146 if not is_valid_plain_key(k) then --? tostring
150 return nil, "cannot store '"..tostring(k).."' as a plain key." 147 return nil, "cannot store '"..tostring(k).."' as a plain key."
@@ -177,9 +174,9 @@ end
177-- @param tbl table: the table containing the data to be written 174-- @param tbl table: the table containing the data to be written
178-- @param field_order table: an optional array indicating the order of top-level fields. 175-- @param field_order table: an optional array indicating the order of top-level fields.
179-- @return persisted data as string; or nil and an error message 176-- @return persisted data as string; or nil and an error message
180function persist.save_from_table_to_string(tbl: {number | string: number| string}, field_order: SortBy<number | string>): string 177function persist.save_from_table_to_string(tbl: {number | string: number | string}, field_order: util.SortBy<number | string>): string, string --? optional
181 local out: Writer = {buffer = {}} --! 178 local out: Writer = {buffer = {}} --!
182 function out:write(data) table.insert(self.buffer, data) end 179 function out:write(data: string) table.insert(self.buffer, data) end
183 local ok, err = write_table_as_assignments(out, tbl, field_order) 180 local ok, err = write_table_as_assignments(out, tbl, field_order)
184 if not ok then 181 if not ok then
185 return nil, err 182 return nil, err
@@ -196,7 +193,7 @@ end
196-- @param field_order table: an optional array indicating the order of top-level fields. 193-- @param field_order table: an optional array indicating the order of top-level fields.
197-- @return boolean or (nil, string): true if successful, or nil and a 194-- @return boolean or (nil, string): true if successful, or nil and a
198-- message in case of errors. 195-- message in case of errors.
199function persist.save_from_table(filename, tbl, field_order) 196function persist.save_from_table(filename: string, tbl: {number | string : number | string}, field_order: util.SortBy<number | string>)
200 local prefix = dir.dir_name(filename) 197 local prefix = dir.dir_name(filename)
201 fs.make_dir(prefix) 198 fs.make_dir(prefix)
202 local out = io.open(filename, "w") 199 local out = io.open(filename, "w")
diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl
index f1447e60..c1282eb1 100644
--- a/src/luarocks/util.tl
+++ b/src/luarocks/util.tl
@@ -7,12 +7,10 @@
7local core = require("luarocks.core.util") 7local core = require("luarocks.core.util")
8local cfg = require("luarocks.core.cfg") 8local cfg = require("luarocks.core.cfg")
9 9
10local type SortBy<K> = table.SortFunction<K> | core.Ordering<K>
11
12local record util 10local record util
13 cleanup_path: function(string, string, string, boolean): string 11 cleanup_path: function(string, string, string, boolean): string
14 split_string: function(string, string, number): {string} 12 split_string: function(string, string, number): {string}
15 sortedpairs: function<K, V>(tbl: {K: V}, sort_by: SortBy<K>): function(): K, V, core.Ordering<K> 13 sortedpairs: function<K, V>(tbl: {K: V}, sort_by: core.SortBy<K>): function(): K, V, core.Ordering<K>
16 deep_merge: function({any : any}, {any : any}) 14 deep_merge: function({any : any}, {any : any})
17 deep_merge_under: function({any : any}, {any : any}) 15 deep_merge_under: function({any : any}, {any : any})
18 popen_read: function(string, ?string): string 16 popen_read: function(string, ?string): string
@@ -22,6 +20,8 @@ local record util
22 keys: function<K, V>({K : V}): {K} 20 keys: function<K, V>({K : V}): {K}
23 matchquote: function(string): string 21 matchquote: function(string): string
24 22
23 type SortBy = core.SortBy
24
25 record Fn 25 record Fn
26 fn: function(...: any) 26 fn: function(...: any)
27 args: table.PackTable<any> 27 args: table.PackTable<any>
@@ -145,12 +145,12 @@ end
145-- @param tbl table: Table to have its string values modified. 145-- @param tbl table: Table to have its string values modified.
146-- @param vars table: Table containing string-string key-value pairs 146-- @param vars table: Table containing string-string key-value pairs
147-- representing variables to replace in the strings values of tbl. 147-- representing variables to replace in the strings values of tbl.
148function util.variable_substitutions(tbl: {string: string}, vars: {string: string}) 148function util.variable_substitutions(tbl: {string: any}, vars: {string: string})
149 149
150 local updated = {} 150 local updated: {string: string} = {}
151 for k, v in pairs(tbl) do 151 for k, v in pairs(tbl) do
152 if v is string then 152 if v is string then
153 updated[k] = v:gsub(var_format_pattern, vars) 153 updated[k] = string.gsub(v, var_format_pattern, vars) --! table 100% (there is an assert in src/luarocks/util.lua)
154 if warn_failed_matches(updated[k]) then 154 if warn_failed_matches(updated[k]) then
155 updated[k] = updated[k]:gsub(var_format_pattern, "") 155 updated[k] = updated[k]:gsub(var_format_pattern, "")
156 end 156 end
@@ -268,7 +268,7 @@ function util.see_also(text: string): string
268end 268end
269 269
270function util.announce_install(rockspec: Rockspec) 270function util.announce_install(rockspec: Rockspec)
271 local path = require("luarocks.core.path") --TEAL BUG? 271 local path = require("luarocks.path") --TEAL BUG?
272 272
273 local suffix = "" 273 local suffix = ""
274 if rockspec.description and rockspec.description.license then 274 if rockspec.description and rockspec.description.license then
@@ -313,7 +313,10 @@ end
313--- Get default rockspec name for commands that take optional rockspec name. 313--- Get default rockspec name for commands that take optional rockspec name.
314-- @return string or (nil, string): path to the rockspec or nil and error message. 314-- @return string or (nil, string): path to the rockspec or nil and error message.
315function util.get_default_rockspec(): string, string 315function util.get_default_rockspec(): string, string
316 local versions, paths, unnamed_paths = {}, {}, {} 316
317 local versions: {string: string} = {}
318 local paths: {string: string} = {}
319 local unnamed_paths: {string} = {}
317 -- Look for rockspecs in some common locations. 320 -- Look for rockspecs in some common locations.
318 collect_rockspecs(versions, paths, unnamed_paths, ".") 321 collect_rockspecs(versions, paths, unnamed_paths, ".")
319 collect_rockspecs(versions, paths, unnamed_paths, "rockspec") 322 collect_rockspecs(versions, paths, unnamed_paths, "rockspec")
@@ -340,7 +343,7 @@ function util.get_default_rockspec(): string, string
340 343
341 if rock then 344 if rock then
342 -- If there are rockspecs for multiple rocks it's ambiguous. 345 -- If there are rockspecs for multiple rocks it's ambiguous.
343 if next(versions, rock) then 346 if next(versions, rock) ~= nil then --! why expect boolean
344 return nil, "Please specify which rockspec file to use." 347 return nil, "Please specify which rockspec file to use."
345 else 348 else
346 return paths[rock] 349 return paths[rock]
@@ -427,7 +430,7 @@ function util.lua_is_wrapper(interp: string): boolean, string
427 if not data then 430 if not data then
428 return nil, err 431 return nil, err
429 end 432 end
430 return not not data:match("LUAROCKS_SYSCONFDIR") --! not not?!?! string to true? 433 return not not data:match("LUAROCKS_SYSCONFDIR") --! not not ->? string to true?
431end 434end
432 435
433do 436do
@@ -478,14 +481,7 @@ do
478 do 481 do
479 local exe_suffix = (package.config:sub(1, 1) == "\\" and ".exe" or "") 482 local exe_suffix = (package.config:sub(1, 1) == "\\" and ".exe" or "")
480 483
481 local record names_record 484 local function insert_lua_variants(names: {string}, luaver: string)
482 names_hash: {string: string}
483 names_array: {string}
484 end
485
486 local type HashArrayRecord = names_record
487
488 local function insert_lua_variants(names: HashArrayRecord, luaver: string) --! atempt 1 bellow comented, this is atempt 2, original in the util-original file
489 local variants = { 485 local variants = {
490 "lua" .. luaver .. exe_suffix, 486 "lua" .. luaver .. exe_suffix,
491 "lua" .. luaver:gsub("%.", "") .. exe_suffix, 487 "lua" .. luaver:gsub("%.", "") .. exe_suffix,
@@ -493,40 +489,12 @@ do
493 "lua-" .. luaver:gsub("%.", "") .. exe_suffix, 489 "lua-" .. luaver:gsub("%.", "") .. exe_suffix,
494 } 490 }
495 for _, name in ipairs(variants) do 491 for _, name in ipairs(variants) do
496 names.names_hash[name] = luaver --! this wants indexing by string --! names is never used as a hesh 492 table.insert(names, name)
497 table.insert(names.names_array, name) --! this wants indexing by integer
498 end 493 end
499 end 494 end
500 495
501 -- local function insert_lua_variants(names: {string}, luaver: string)
502 -- local variants = {
503 -- "lua" .. luaver .. exe_suffix,
504 -- "lua" .. luaver:gsub("%.", "") .. exe_suffix,
505 -- "lua-" .. luaver .. exe_suffix,
506 -- "lua-" .. luaver:gsub("%.", "") .. exe_suffix,
507 -- }
508 -- local names_length = #names
509 -- for i, name in ipairs(variants) do
510 -- names[name] = luaver --! this wants indexing by string
511 -- names[i + names_length] = name
512 -- end
513 -- end
514
515 -- local function insert_lua_variants(names, luaver)
516 -- local variants = {
517 -- "lua" .. luaver .. exe_suffix,
518 -- "lua" .. luaver:gsub("%.", "") .. exe_suffix,
519 -- "lua-" .. luaver .. exe_suffix,
520 -- "lua-" .. luaver:gsub("%.", "") .. exe_suffix,
521 -- }
522 -- for _, name in ipairs(variants) do
523 -- names[name] = luaver
524 -- table.insert(names, name)
525 -- end
526 -- end
527
528 find_lua_bindir = function(prefix: string, luaver: string, verbose: string): string, string, string 496 find_lua_bindir = function(prefix: string, luaver: string, verbose: string): string, string, string
529 local names: HashArrayRecord = {} 497 local names: {string} = {}
530 if luaver then 498 if luaver then
531 insert_lua_variants(names, luaver) 499 insert_lua_variants(names, luaver)
532 else 500 else
@@ -535,14 +503,14 @@ do
535 end 503 end
536 end 504 end
537 if luaver == "5.1" or not luaver then 505 if luaver == "5.1" or not luaver then
538 table.insert(names.names_array, "luajit" .. exe_suffix) 506 table.insert(names, "luajit" .. exe_suffix)
539 end 507 end
540 table.insert(names.names_array, "lua" .. exe_suffix) 508 table.insert(names, "lua" .. exe_suffix)
541 509
542 local tried = {} 510 local tried = {}
543 local dir_sep = package.config:sub(1, 1) 511 local dir_sep = package.config:sub(1, 1)
544 for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do 512 for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do
545 for _, name in ipairs(names.names_array) do 513 for _, name in ipairs(names) do
546 local lua = d .. dir_sep .. name 514 local lua = d .. dir_sep .. name
547 local is_wrapper, err = util.lua_is_wrapper(lua) 515 local is_wrapper, err = util.lua_is_wrapper(lua)
548 if is_wrapper == false then 516 if is_wrapper == false then
@@ -581,16 +549,18 @@ do
581 end 549 end
582end 550end
583 551
584function util.opts_table(type_name: string, valid_opts: {string: string}) 552function util.opts_table(type_name: string, valid_opts: {string: string}): function({string : string}): {string : string}
585 local opts_mt: metatable<string> = {} --Option?? 553 local opts_mt: metatable<{string : string}> = {
554 -- type: function(): string --!
555 }
586 556
587 opts_mt.__index = opts_mt 557 opts_mt.__index = opts_mt
588 558
589 function opts_mt.type(): string 559 function opts_mt.type(): string --!
590 return type_name 560 return type_name
591 end 561 end
592 562
593 return function(opts: {string: string}) 563 return function(opts: {string: string}): {string: string}
594 for k, v in pairs(opts) do 564 for k, v in pairs(opts) do
595 local tv = type(v) 565 local tv = type(v)
596 if not valid_opts[k] then 566 if not valid_opts[k] then
@@ -618,9 +588,9 @@ end
618-- @return a table with rock names as keys and versions and values, 588-- @return a table with rock names as keys and versions and values,
619-- specifying modules that are already provided by the VM (including 589-- specifying modules that are already provided by the VM (including
620-- "lua" for the Lua version and, for format 3.0+, "luajit" if detected). 590-- "lua" for the Lua version and, for format 3.0+, "luajit" if detected).
621function util.get_rocks_provided(rockspec: Rockspec): {Rockspec} 591function util.get_rocks_provided(rockspec: Rockspec): {string: string}
622 592
623 if not rockspec and cfg.cache.rocks_provided then --rocks_provided: Rockspec 593 if not rockspec and cfg.cache.rocks_provided then
624 return cfg.cache.rocks_provided 594 return cfg.cache.rocks_provided
625 end 595 end
626 596