From 1f01bc98f91546a03b777202ce0f52f827dbd321 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 25 Jul 2024 18:12:29 +0300 Subject: almost finished util and persist --- src/luarocks/core/cfg.d.tl | 3 +- src/luarocks/core/util.tl | 7 ++-- src/luarocks/fs.d.tl | 1 + src/luarocks/path.tl | 2 +- src/luarocks/persist.tl | 17 ++++------ src/luarocks/util.tl | 84 +++++++++++++++------------------------------- 6 files changed, 41 insertions(+), 73 deletions(-) (limited to 'src') 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 record cache luajit_version_checked: boolean luajit_version: string - rocks_provided: {Rockspec} + rocks_provided: {string: string} --? right type? infered from end + record variables LUA: string 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 sub_orders: {K: Ordering} end + + type SortBy = table.SortFunction | util.Ordering end -------------------------------------------------------------------------------- local dir_sep = package.config:sub(1, 1) -local type SortBy = table.SortFunction | util.Ordering - - --- Run a process and read a its output. -- Equivalent to io.popen(cmd):read("*l"), except that it -- closes the fd right away. @@ -293,7 +292,7 @@ end -- for that key, which is returned by the iterator as the third value after the key -- and the value. -- @return function: the iterator function. -function util.sortedpairs(tbl: {K: V}, sort_by?: SortBy): function(): K, V, util.Ordering +function util.sortedpairs(tbl: {K: V}, sort_by?: util.SortBy): function(): K, V, util.Ordering local keys = util.keys(tbl) local sub_orders: {K: util.Ordering} = nil 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 -- util is_dir: function(dir: string): boolean dir: function(dir: string): function(): string --? right iterator + make_dir: function is_file: function(file: string): boolean current_dir: function(): string 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 -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function path.install_dir(name: string, version: string, tree: string | Tree): string +function path.install_dir(name: string, version: string, tree?: string | Tree): string assert(not name:match("/")) return dir.path(path.rocks_dir(tree), name, version) end 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 end local core = require("luarocks.core.persist") -local coreutil = require("luarocks.core.util") local util = require("luarocks.util") local dir = require("luarocks.dir") local fs = require("luarocks.fs") @@ -15,13 +14,11 @@ local fs = require("luarocks.fs") persist.run_file = core.run_file persist.load_into_table = core.load_into_table -local type SortBy = table.SortFunction | coreutil.Ordering - local interface Writer write: function(self: Writer, data: string) end -local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: SortBy) +local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy) --- Write a value as Lua code. -- 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}, -- @param level number: the indentation level -- @param sub_order table: optional prioritization table -- @see write_table -function persist.write_value(out: Writer, v: any, level: integer, sub_order?: SortBy) +function persist.write_value(out: Writer, v: any, level: integer, sub_order?: util.SortBy) if v is {number | string: number | string} then level = level or 0 write_table(out, v, level + 1, sub_order) @@ -105,7 +102,7 @@ end -- @param tbl table: the table to be written. -- @param level number: the indentation level -- @param field_order table: optional prioritization table -write_table = function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: SortBy) +write_table = function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy) out:write("{") local sep = "\n" local indentation = " " @@ -144,7 +141,7 @@ end -- @param tbl table: the table to be written. -- @param field_order table: optional prioritization table -- @return true if successful; nil and error message if failed. -local function write_table_as_assignments(out: Writer, tbl: {number | string: number| string}, field_order: SortBy): boolean, string +local function write_table_as_assignments(out: Writer, tbl: {number | string: number| string}, field_order: util.SortBy): boolean, string for k, v, sub_order in util.sortedpairs(tbl, field_order) do if not is_valid_plain_key(k) then --? tostring return nil, "cannot store '"..tostring(k).."' as a plain key." @@ -177,9 +174,9 @@ end -- @param tbl table: the table containing the data to be written -- @param field_order table: an optional array indicating the order of top-level fields. -- @return persisted data as string; or nil and an error message -function persist.save_from_table_to_string(tbl: {number | string: number| string}, field_order: SortBy): string +function persist.save_from_table_to_string(tbl: {number | string: number | string}, field_order: util.SortBy): string, string --? optional local out: Writer = {buffer = {}} --! - function out:write(data) table.insert(self.buffer, data) end + function out:write(data: string) table.insert(self.buffer, data) end local ok, err = write_table_as_assignments(out, tbl, field_order) if not ok then return nil, err @@ -196,7 +193,7 @@ end -- @param field_order table: an optional array indicating the order of top-level fields. -- @return boolean or (nil, string): true if successful, or nil and a -- message in case of errors. -function persist.save_from_table(filename, tbl, field_order) +function persist.save_from_table(filename: string, tbl: {number | string : number | string}, field_order: util.SortBy) local prefix = dir.dir_name(filename) fs.make_dir(prefix) 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 @@ local core = require("luarocks.core.util") local cfg = require("luarocks.core.cfg") -local type SortBy = table.SortFunction | core.Ordering - local record util cleanup_path: function(string, string, string, boolean): string split_string: function(string, string, number): {string} - sortedpairs: function(tbl: {K: V}, sort_by: SortBy): function(): K, V, core.Ordering + sortedpairs: function(tbl: {K: V}, sort_by: core.SortBy): function(): K, V, core.Ordering deep_merge: function({any : any}, {any : any}) deep_merge_under: function({any : any}, {any : any}) popen_read: function(string, ?string): string @@ -22,6 +20,8 @@ local record util keys: function({K : V}): {K} matchquote: function(string): string + type SortBy = core.SortBy + record Fn fn: function(...: any) args: table.PackTable @@ -145,12 +145,12 @@ end -- @param tbl table: Table to have its string values modified. -- @param vars table: Table containing string-string key-value pairs -- representing variables to replace in the strings values of tbl. -function util.variable_substitutions(tbl: {string: string}, vars: {string: string}) +function util.variable_substitutions(tbl: {string: any}, vars: {string: string}) - local updated = {} + local updated: {string: string} = {} for k, v in pairs(tbl) do if v is string then - updated[k] = v:gsub(var_format_pattern, vars) + updated[k] = string.gsub(v, var_format_pattern, vars) --! table 100% (there is an assert in src/luarocks/util.lua) if warn_failed_matches(updated[k]) then updated[k] = updated[k]:gsub(var_format_pattern, "") end @@ -268,7 +268,7 @@ function util.see_also(text: string): string end function util.announce_install(rockspec: Rockspec) - local path = require("luarocks.core.path") --TEAL BUG? + local path = require("luarocks.path") --TEAL BUG? local suffix = "" if rockspec.description and rockspec.description.license then @@ -313,7 +313,10 @@ end --- Get default rockspec name for commands that take optional rockspec name. -- @return string or (nil, string): path to the rockspec or nil and error message. function util.get_default_rockspec(): string, string - local versions, paths, unnamed_paths = {}, {}, {} + + local versions: {string: string} = {} + local paths: {string: string} = {} + local unnamed_paths: {string} = {} -- Look for rockspecs in some common locations. collect_rockspecs(versions, paths, unnamed_paths, ".") collect_rockspecs(versions, paths, unnamed_paths, "rockspec") @@ -340,7 +343,7 @@ function util.get_default_rockspec(): string, string if rock then -- If there are rockspecs for multiple rocks it's ambiguous. - if next(versions, rock) then + if next(versions, rock) ~= nil then --! why expect boolean return nil, "Please specify which rockspec file to use." else return paths[rock] @@ -427,7 +430,7 @@ function util.lua_is_wrapper(interp: string): boolean, string if not data then return nil, err end - return not not data:match("LUAROCKS_SYSCONFDIR") --! not not?!?! string to true? + return not not data:match("LUAROCKS_SYSCONFDIR") --! not not ->? string to true? end do @@ -478,14 +481,7 @@ do do local exe_suffix = (package.config:sub(1, 1) == "\\" and ".exe" or "") - local record names_record - names_hash: {string: string} - names_array: {string} - end - - local type HashArrayRecord = names_record - - local function insert_lua_variants(names: HashArrayRecord, luaver: string) --! atempt 1 bellow comented, this is atempt 2, original in the util-original file + local function insert_lua_variants(names: {string}, luaver: string) local variants = { "lua" .. luaver .. exe_suffix, "lua" .. luaver:gsub("%.", "") .. exe_suffix, @@ -493,40 +489,12 @@ do "lua-" .. luaver:gsub("%.", "") .. exe_suffix, } for _, name in ipairs(variants) do - names.names_hash[name] = luaver --! this wants indexing by string --! names is never used as a hesh - table.insert(names.names_array, name) --! this wants indexing by integer + table.insert(names, name) end end - -- local function insert_lua_variants(names: {string}, luaver: string) - -- local variants = { - -- "lua" .. luaver .. exe_suffix, - -- "lua" .. luaver:gsub("%.", "") .. exe_suffix, - -- "lua-" .. luaver .. exe_suffix, - -- "lua-" .. luaver:gsub("%.", "") .. exe_suffix, - -- } - -- local names_length = #names - -- for i, name in ipairs(variants) do - -- names[name] = luaver --! this wants indexing by string - -- names[i + names_length] = name - -- end - -- end - - -- local function insert_lua_variants(names, luaver) - -- local variants = { - -- "lua" .. luaver .. exe_suffix, - -- "lua" .. luaver:gsub("%.", "") .. exe_suffix, - -- "lua-" .. luaver .. exe_suffix, - -- "lua-" .. luaver:gsub("%.", "") .. exe_suffix, - -- } - -- for _, name in ipairs(variants) do - -- names[name] = luaver - -- table.insert(names, name) - -- end - -- end - find_lua_bindir = function(prefix: string, luaver: string, verbose: string): string, string, string - local names: HashArrayRecord = {} + local names: {string} = {} if luaver then insert_lua_variants(names, luaver) else @@ -535,14 +503,14 @@ do end end if luaver == "5.1" or not luaver then - table.insert(names.names_array, "luajit" .. exe_suffix) + table.insert(names, "luajit" .. exe_suffix) end - table.insert(names.names_array, "lua" .. exe_suffix) + table.insert(names, "lua" .. exe_suffix) local tried = {} local dir_sep = package.config:sub(1, 1) for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do - for _, name in ipairs(names.names_array) do + for _, name in ipairs(names) do local lua = d .. dir_sep .. name local is_wrapper, err = util.lua_is_wrapper(lua) if is_wrapper == false then @@ -581,16 +549,18 @@ do end end -function util.opts_table(type_name: string, valid_opts: {string: string}) - local opts_mt: metatable = {} --Option?? +function util.opts_table(type_name: string, valid_opts: {string: string}): function({string : string}): {string : string} + local opts_mt: metatable<{string : string}> = { + -- type: function(): string --! + } opts_mt.__index = opts_mt - function opts_mt.type(): string + function opts_mt.type(): string --! return type_name end - return function(opts: {string: string}) + return function(opts: {string: string}): {string: string} for k, v in pairs(opts) do local tv = type(v) if not valid_opts[k] then @@ -618,9 +588,9 @@ end -- @return a table with rock names as keys and versions and values, -- specifying modules that are already provided by the VM (including -- "lua" for the Lua version and, for format 3.0+, "luajit" if detected). -function util.get_rocks_provided(rockspec: Rockspec): {Rockspec} +function util.get_rocks_provided(rockspec: Rockspec): {string: string} - if not rockspec and cfg.cache.rocks_provided then --rocks_provided: Rockspec + if not rockspec and cfg.cache.rocks_provided then return cfg.cache.rocks_provided end -- cgit v1.2.3-55-g6feb