From b026e7250ff05b675cafd92cefb1a2f640b16bc1 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Tue, 30 Jul 2024 11:57:55 +0300 Subject: generated util --- src/luarocks/core/cfg.d.tl | 2 ++ src/luarocks/fs.d.tl | 2 +- src/luarocks/fun.tl | 1 + src/luarocks/persist.tl | 60 ++++++++++++++++++++++++---------------------- src/luarocks/util.lua | 8 +++++-- src/luarocks/util.tl | 2 +- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl index 2218c172..33b9be66 100644 --- a/src/luarocks/core/cfg.d.tl +++ b/src/luarocks/core/cfg.d.tl @@ -62,6 +62,8 @@ local record cfg GPG: string end rocks_provided: {Rockspec} + -- persist + home: string end return cfg \ No newline at end of file diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl index 17a367cc..80747f9b 100644 --- a/src/luarocks/fs.d.tl +++ b/src/luarocks/fs.d.tl @@ -9,7 +9,7 @@ local record fs -- util is_dir: function(dir: string): boolean dir: function(dir: string): function(): string --? right iterator - make_dir: function + make_dir: function(string): boolean, string is_file: function(file: string): boolean current_dir: function(): string list_dir: function(string): {string} diff --git a/src/luarocks/fun.tl b/src/luarocks/fun.tl index f52e059d..b9960e3b 100644 --- a/src/luarocks/fun.tl +++ b/src/luarocks/fun.tl @@ -12,6 +12,7 @@ function fun.concat(xs: {any}, ys: {any}): {any} --? not generic becuse lua supo for i = 1, #ys do rs[i + n] = ys[i] end + return rs end diff --git a/src/luarocks/persist.tl b/src/luarocks/persist.tl index 6bd4f896..de8f3faf 100644 --- a/src/luarocks/persist.tl +++ b/src/luarocks/persist.tl @@ -10,6 +10,10 @@ local core = require("luarocks.core.persist") local util = require("luarocks.util") local dir = require("luarocks.dir") local fs = require("luarocks.fs") +local cfg = require("luarocks.core.cfg") + +local type Config = cfg +local type PersistableTable = { string | number : string | number | boolean | PersistableTable } persist.run_file = core.run_file persist.load_into_table = core.load_into_table @@ -19,7 +23,7 @@ local interface Writer buffer: {number | string} end -local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy) +local write_table: function(out: Writer, tbl: PersistableTable, level: integer, sort_by: util.SortBy) --- Write a value as Lua code. -- This function handles only numbers and strings, invoking write_table @@ -30,7 +34,7 @@ local write_table: function(out: Writer, tbl: {number | string: number| string}, -- @param sub_order table: optional prioritization table -- @see write_table function persist.write_value(out: Writer, v: any, level: integer, sub_order?: util.SortBy) - if v is {number | string: number | string} then + if v is PersistableTable then level = level or 0 write_table(out, v, level + 1, sub_order) elseif v is string then @@ -102,14 +106,14 @@ end -- @param out table or userdata: a writer object supporting :write() method. -- @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: util.SortBy) +-- @param sort_by table: optional prioritization table +write_table = function(out: Writer, tbl: PersistableTable, level: integer, sort_by: util.SortBy) out:write("{") local sep = "\n" local indentation = " " local indent = true local i = 1 - for k, v, sub_order in util.sortedpairs(tbl, field_order) do + for k, v, sub_order in util.sortedpairs(tbl, sort_by) do out:write(sep) if indent then for _ = 1, level do out:write(indentation) end @@ -140,11 +144,11 @@ end --- Write a table as series of assignments to a writer object. -- @param out table or userdata: a writer object supporting :write() method. -- @param tbl table: the table to be written. --- @param field_order table: optional prioritization table +-- @param sort_by 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: 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 +local function write_table_as_assignments(out: Writer, tbl: PersistableTable, sort_by: util.SortBy): boolean, string + for k, v, sub_order in util.sortedpairs(tbl, sort_by) do + if not (k is string and is_valid_plain_key(k)) then return nil, "cannot store '"..tostring(k).."' as a plain key." end out:write(k.." = ") @@ -157,9 +161,9 @@ end --- Write a table using Lua table syntax to a writer object. -- @param out table or userdata: a writer object supporting :write() method. -- @param tbl table: the table to be written. -local function write_table_as_table(out: Writer, tbl: {number | string: number| string}) +local function write_table_as_table(out: Writer, tbl: PersistableTable) out:write("return {\n") - for k, v, sub_order in util.sortedpairs(tbl) do --! that has the right inputs and outputs yet it throws an error + for k, v, sub_order in util.sortedpairs(tbl) do out:write(" ") write_table_key_assignment(out, k, 1) persist.write_value(out, v, 1, sub_order) @@ -173,12 +177,12 @@ end -- Only numbers, strings and tables (containing numbers, strings -- or other recursively processed tables) are supported. -- @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. +-- @param sort_by 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: util.SortBy): string, string +function persist.save_from_table_to_string(tbl: PersistableTable, sort_by: util.SortBy): string, string local out: Writer = {buffer = {}} function out:write(data: string) table.insert(self.buffer, data) end - local ok, err = write_table_as_assignments(out, tbl, field_order) + local ok, err = write_table_as_assignments(out, tbl, sort_by) if not ok then return nil, err end @@ -191,17 +195,17 @@ end -- or other recursively processed tables) are supported. -- @param filename string: the output filename -- @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. +-- @param sort_by 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: string, tbl: {number | string : number | string}, field_order: util.SortBy): boolean, string +function persist.save_from_table(filename: string, tbl: PersistableTable, sort_by: util.SortBy): boolean, string local prefix = dir.dir_name(filename) fs.make_dir(prefix) local out = io.open(filename, "w") if not out then return nil, "Cannot create file at "..filename end - local ok, err = write_table_as_assignments(out, tbl, field_order) --! interface problems + local ok, err = write_table_as_assignments(out as Writer, tbl, sort_by) out:close() if not ok then return nil, err @@ -217,19 +221,19 @@ end -- @param tbl table: the table containing the data to be written -- @return boolean or (nil, string): true if successful, or nil and a -- message in case of errors. -function persist.save_as_module(filename: string, tbl: {number | string : number | string}): boolean, string +function persist.save_as_module(filename: string, tbl: PersistableTable): boolean, string local out = io.open(filename, "w") if not out then return nil, "Cannot create file at "..filename end - write_table_as_table(out, tbl) --! interface problems + write_table_as_table(out as Writer, tbl) out:close() return true end -function persist.load_config_file_if_basic(filename: string, cfg): boolean | {number | string : number | string}, string --! cfg type +function persist.load_config_file_if_basic(filename: string, config: Config): PersistableTable, string local env = { - home = cfg.home + home = config.home } local result, _, errcode = persist.load_into_table(filename, env) if errcode == "load" or errcode == "run" then @@ -237,26 +241,26 @@ function persist.load_config_file_if_basic(filename: string, cfg): boolean | {nu return nil, "Could not read existing config file " .. filename end - local tbl: {number | string : number | string} + local tbl: PersistableTable if errcode == "open" then -- could not open, maybe file does not exist tbl = {} else - tbl = result --! strongger definition --? make the load into table generic + tbl = result as PersistableTable -- the shape of result is not validated tbl.home = nil end return tbl end -function persist.save_default_lua_version(prefix, lua_version) - local ok, err = fs.make_dir(prefix) +function persist.save_default_lua_version(prefix: string, lua_version: string): boolean, string + local ok, err_makedir = fs.make_dir(prefix) if not ok then - return nil, err + return nil, err_makedir end - local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") + local fd, err_open = io.open(dir.path(prefix, "default-lua-version.lua"), "w") if not fd then - return nil, err + return nil, err_open end fd:write('return "' .. lua_version .. '"\n') fd:close() diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 145993ea..2c8d8ca6 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -63,8 +63,6 @@ local scheduled_functions = {} function util.schedule_function(f, ...) - -- local pack = table.pack or function(...) return { n = select("#", ...), ... } end --! FOR TESTS TO PASS - local item = { fn = f, args = table.pack(...) } table.insert(scheduled_functions, item) return item @@ -553,6 +551,12 @@ end + + + + + + function util.get_rocks_provided(rockspec) if not rockspec and cfg.cache.rocks_provided then diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl index 72b8d995..80742419 100644 --- a/src/luarocks/util.tl +++ b/src/luarocks/util.tl @@ -10,7 +10,7 @@ local cfg = require("luarocks.core.cfg") 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: core.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 -- cgit v1.2.3-55-g6feb