diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-07-30 11:57:55 +0300 |
---|---|---|
committer | V1K1NGbg <victor@ilchev.com> | 2024-08-05 20:51:31 +0300 |
commit | b026e7250ff05b675cafd92cefb1a2f640b16bc1 (patch) | |
tree | 01748b57664da80ef9c0f93b457b0745d2d9b619 | |
parent | eda4b59d4ed8653dd1169e1675d92a1dd1b91a66 (diff) | |
download | luarocks-b026e7250ff05b675cafd92cefb1a2f640b16bc1.tar.gz luarocks-b026e7250ff05b675cafd92cefb1a2f640b16bc1.tar.bz2 luarocks-b026e7250ff05b675cafd92cefb1a2f640b16bc1.zip |
generated util
-rw-r--r-- | src/luarocks/core/cfg.d.tl | 2 | ||||
-rw-r--r-- | src/luarocks/fs.d.tl | 2 | ||||
-rw-r--r-- | src/luarocks/fun.tl | 1 | ||||
-rw-r--r-- | src/luarocks/persist.tl | 60 | ||||
-rw-r--r-- | src/luarocks/util.lua | 8 | ||||
-rw-r--r-- | 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 | |||
62 | GPG: string | 62 | GPG: string |
63 | end | 63 | end |
64 | rocks_provided: {Rockspec} | 64 | rocks_provided: {Rockspec} |
65 | -- persist | ||
66 | home: string | ||
65 | end | 67 | end |
66 | 68 | ||
67 | return cfg \ No newline at end of file | 69 | 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 | |||
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 | make_dir: function(string): boolean, string |
13 | is_file: function(file: string): boolean | 13 | is_file: function(file: string): boolean |
14 | current_dir: function(): string | 14 | current_dir: function(): string |
15 | list_dir: function(string): {string} | 15 | 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 | |||
12 | for i = 1, #ys do | 12 | for i = 1, #ys do |
13 | rs[i + n] = ys[i] | 13 | rs[i + n] = ys[i] |
14 | end | 14 | end |
15 | |||
15 | return rs | 16 | return rs |
16 | end | 17 | end |
17 | 18 | ||
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") | |||
10 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
11 | local dir = require("luarocks.dir") | 11 | local dir = require("luarocks.dir") |
12 | local fs = require("luarocks.fs") | 12 | local fs = require("luarocks.fs") |
13 | local cfg = require("luarocks.core.cfg") | ||
14 | |||
15 | local type Config = cfg | ||
16 | local type PersistableTable = { string | number : string | number | boolean | PersistableTable } | ||
13 | 17 | ||
14 | persist.run_file = core.run_file | 18 | persist.run_file = core.run_file |
15 | persist.load_into_table = core.load_into_table | 19 | persist.load_into_table = core.load_into_table |
@@ -19,7 +23,7 @@ local interface Writer | |||
19 | buffer: {number | string} | 23 | buffer: {number | string} |
20 | end | 24 | end |
21 | 25 | ||
22 | local write_table: function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy<number | string>) | 26 | local write_table: function(out: Writer, tbl: PersistableTable, level: integer, sort_by: util.SortBy<number | string>) |
23 | 27 | ||
24 | --- Write a value as Lua code. | 28 | --- Write a value as Lua code. |
25 | -- This function handles only numbers and strings, invoking write_table | 29 | -- 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}, | |||
30 | -- @param sub_order table: optional prioritization table | 34 | -- @param sub_order table: optional prioritization table |
31 | -- @see write_table | 35 | -- @see write_table |
32 | function persist.write_value(out: Writer, v: any, level: integer, sub_order?: util.SortBy<number | string>) | 36 | function persist.write_value(out: Writer, v: any, level: integer, sub_order?: util.SortBy<number | string>) |
33 | if v is {number | string: number | string} then | 37 | if v is PersistableTable then |
34 | level = level or 0 | 38 | level = level or 0 |
35 | write_table(out, v, level + 1, sub_order) | 39 | write_table(out, v, level + 1, sub_order) |
36 | elseif v is string then | 40 | elseif v is string then |
@@ -102,14 +106,14 @@ end | |||
102 | -- @param out table or userdata: a writer object supporting :write() method. | 106 | -- @param out table or userdata: a writer object supporting :write() method. |
103 | -- @param tbl table: the table to be written. | 107 | -- @param tbl table: the table to be written. |
104 | -- @param level number: the indentation level | 108 | -- @param level number: the indentation level |
105 | -- @param field_order table: optional prioritization table | 109 | -- @param sort_by table: optional prioritization table |
106 | write_table = function(out: Writer, tbl: {number | string: number| string}, level: integer, field_order: util.SortBy<number | string>) | 110 | write_table = function(out: Writer, tbl: PersistableTable, level: integer, sort_by: util.SortBy<number | string>) |
107 | out:write("{") | 111 | out:write("{") |
108 | local sep = "\n" | 112 | local sep = "\n" |
109 | local indentation = " " | 113 | local indentation = " " |
110 | local indent = true | 114 | local indent = true |
111 | local i = 1 | 115 | local i = 1 |
112 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 116 | for k, v, sub_order in util.sortedpairs(tbl, sort_by) do |
113 | out:write(sep) | 117 | out:write(sep) |
114 | if indent then | 118 | if indent then |
115 | for _ = 1, level do out:write(indentation) end | 119 | for _ = 1, level do out:write(indentation) end |
@@ -140,11 +144,11 @@ end | |||
140 | --- Write a table as series of assignments to a writer object. | 144 | --- Write a table as series of assignments to a writer object. |
141 | -- @param out table or userdata: a writer object supporting :write() method. | 145 | -- @param out table or userdata: a writer object supporting :write() method. |
142 | -- @param tbl table: the table to be written. | 146 | -- @param tbl table: the table to be written. |
143 | -- @param field_order table: optional prioritization table | 147 | -- @param sort_by table: optional prioritization table |
144 | -- @return true if successful; nil and error message if failed. | 148 | -- @return true if successful; nil and error message if failed. |
145 | local function write_table_as_assignments(out: Writer, tbl: {number | string: number| string}, field_order: util.SortBy<number | string>): boolean, string | 149 | local function write_table_as_assignments(out: Writer, tbl: PersistableTable, sort_by: util.SortBy<number | string>): boolean, string |
146 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 150 | for k, v, sub_order in util.sortedpairs(tbl, sort_by) do |
147 | if not is_valid_plain_key(k) then --? tostring | 151 | if not (k is string and is_valid_plain_key(k)) then |
148 | return nil, "cannot store '"..tostring(k).."' as a plain key." | 152 | return nil, "cannot store '"..tostring(k).."' as a plain key." |
149 | end | 153 | end |
150 | out:write(k.." = ") | 154 | out:write(k.." = ") |
@@ -157,9 +161,9 @@ end | |||
157 | --- Write a table using Lua table syntax to a writer object. | 161 | --- Write a table using Lua table syntax to a writer object. |
158 | -- @param out table or userdata: a writer object supporting :write() method. | 162 | -- @param out table or userdata: a writer object supporting :write() method. |
159 | -- @param tbl table: the table to be written. | 163 | -- @param tbl table: the table to be written. |
160 | local function write_table_as_table(out: Writer, tbl: {number | string: number| string}) | 164 | local function write_table_as_table(out: Writer, tbl: PersistableTable) |
161 | out:write("return {\n") | 165 | out:write("return {\n") |
162 | for k, v, sub_order in util.sortedpairs(tbl) do --! that has the right inputs and outputs yet it throws an error | 166 | for k, v, sub_order in util.sortedpairs(tbl) do |
163 | out:write(" ") | 167 | out:write(" ") |
164 | write_table_key_assignment(out, k, 1) | 168 | write_table_key_assignment(out, k, 1) |
165 | persist.write_value(out, v, 1, sub_order) | 169 | persist.write_value(out, v, 1, sub_order) |
@@ -173,12 +177,12 @@ end | |||
173 | -- Only numbers, strings and tables (containing numbers, strings | 177 | -- Only numbers, strings and tables (containing numbers, strings |
174 | -- or other recursively processed tables) are supported. | 178 | -- or other recursively processed tables) are supported. |
175 | -- @param tbl table: the table containing the data to be written | 179 | -- @param tbl table: the table containing the data to be written |
176 | -- @param field_order table: an optional array indicating the order of top-level fields. | 180 | -- @param sort_by table: an optional array indicating the order of top-level fields. |
177 | -- @return persisted data as string; or nil and an error message | 181 | -- @return persisted data as string; or nil and an error message |
178 | function persist.save_from_table_to_string(tbl: {number | string: number | string}, field_order: util.SortBy<number | string>): string, string | 182 | function persist.save_from_table_to_string(tbl: PersistableTable, sort_by: util.SortBy<number | string>): string, string |
179 | local out: Writer = {buffer = {}} | 183 | local out: Writer = {buffer = {}} |
180 | function out:write(data: string) table.insert(self.buffer, data) end | 184 | function out:write(data: string) table.insert(self.buffer, data) end |
181 | local ok, err = write_table_as_assignments(out, tbl, field_order) | 185 | local ok, err = write_table_as_assignments(out, tbl, sort_by) |
182 | if not ok then | 186 | if not ok then |
183 | return nil, err | 187 | return nil, err |
184 | end | 188 | end |
@@ -191,17 +195,17 @@ end | |||
191 | -- or other recursively processed tables) are supported. | 195 | -- or other recursively processed tables) are supported. |
192 | -- @param filename string: the output filename | 196 | -- @param filename string: the output filename |
193 | -- @param tbl table: the table containing the data to be written | 197 | -- @param tbl table: the table containing the data to be written |
194 | -- @param field_order table: an optional array indicating the order of top-level fields. | 198 | -- @param sort_by table: an optional array indicating the order of top-level fields. |
195 | -- @return boolean or (nil, string): true if successful, or nil and a | 199 | -- @return boolean or (nil, string): true if successful, or nil and a |
196 | -- message in case of errors. | 200 | -- message in case of errors. |
197 | function persist.save_from_table(filename: string, tbl: {number | string : number | string}, field_order: util.SortBy<number | string>): boolean, string | 201 | function persist.save_from_table(filename: string, tbl: PersistableTable, sort_by: util.SortBy<number | string>): boolean, string |
198 | local prefix = dir.dir_name(filename) | 202 | local prefix = dir.dir_name(filename) |
199 | fs.make_dir(prefix) | 203 | fs.make_dir(prefix) |
200 | local out = io.open(filename, "w") | 204 | local out = io.open(filename, "w") |
201 | if not out then | 205 | if not out then |
202 | return nil, "Cannot create file at "..filename | 206 | return nil, "Cannot create file at "..filename |
203 | end | 207 | end |
204 | local ok, err = write_table_as_assignments(out, tbl, field_order) --! interface problems | 208 | local ok, err = write_table_as_assignments(out as Writer, tbl, sort_by) |
205 | out:close() | 209 | out:close() |
206 | if not ok then | 210 | if not ok then |
207 | return nil, err | 211 | return nil, err |
@@ -217,19 +221,19 @@ end | |||
217 | -- @param tbl table: the table containing the data to be written | 221 | -- @param tbl table: the table containing the data to be written |
218 | -- @return boolean or (nil, string): true if successful, or nil and a | 222 | -- @return boolean or (nil, string): true if successful, or nil and a |
219 | -- message in case of errors. | 223 | -- message in case of errors. |
220 | function persist.save_as_module(filename: string, tbl: {number | string : number | string}): boolean, string | 224 | function persist.save_as_module(filename: string, tbl: PersistableTable): boolean, string |
221 | local out = io.open(filename, "w") | 225 | local out = io.open(filename, "w") |
222 | if not out then | 226 | if not out then |
223 | return nil, "Cannot create file at "..filename | 227 | return nil, "Cannot create file at "..filename |
224 | end | 228 | end |
225 | write_table_as_table(out, tbl) --! interface problems | 229 | write_table_as_table(out as Writer, tbl) |
226 | out:close() | 230 | out:close() |
227 | return true | 231 | return true |
228 | end | 232 | end |
229 | 233 | ||
230 | function persist.load_config_file_if_basic(filename: string, cfg): boolean | {number | string : number | string}, string --! cfg type | 234 | function persist.load_config_file_if_basic(filename: string, config: Config): PersistableTable, string |
231 | local env = { | 235 | local env = { |
232 | home = cfg.home | 236 | home = config.home |
233 | } | 237 | } |
234 | local result, _, errcode = persist.load_into_table(filename, env) | 238 | local result, _, errcode = persist.load_into_table(filename, env) |
235 | if errcode == "load" or errcode == "run" then | 239 | if errcode == "load" or errcode == "run" then |
@@ -237,26 +241,26 @@ function persist.load_config_file_if_basic(filename: string, cfg): boolean | {nu | |||
237 | return nil, "Could not read existing config file " .. filename | 241 | return nil, "Could not read existing config file " .. filename |
238 | end | 242 | end |
239 | 243 | ||
240 | local tbl: {number | string : number | string} | 244 | local tbl: PersistableTable |
241 | if errcode == "open" then | 245 | if errcode == "open" then |
242 | -- could not open, maybe file does not exist | 246 | -- could not open, maybe file does not exist |
243 | tbl = {} | 247 | tbl = {} |
244 | else | 248 | else |
245 | tbl = result --! strongger definition --? make the load into table generic | 249 | tbl = result as PersistableTable -- the shape of result is not validated |
246 | tbl.home = nil | 250 | tbl.home = nil |
247 | end | 251 | end |
248 | 252 | ||
249 | return tbl | 253 | return tbl |
250 | end | 254 | end |
251 | 255 | ||
252 | function persist.save_default_lua_version(prefix, lua_version) | 256 | function persist.save_default_lua_version(prefix: string, lua_version: string): boolean, string |
253 | local ok, err = fs.make_dir(prefix) | 257 | local ok, err_makedir = fs.make_dir(prefix) |
254 | if not ok then | 258 | if not ok then |
255 | return nil, err | 259 | return nil, err_makedir |
256 | end | 260 | end |
257 | local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") | 261 | local fd, err_open = io.open(dir.path(prefix, "default-lua-version.lua"), "w") |
258 | if not fd then | 262 | if not fd then |
259 | return nil, err | 263 | return nil, err_open |
260 | end | 264 | end |
261 | fd:write('return "' .. lua_version .. '"\n') | 265 | fd:write('return "' .. lua_version .. '"\n') |
262 | fd:close() | 266 | 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 = {} | |||
63 | 63 | ||
64 | 64 | ||
65 | function util.schedule_function(f, ...) | 65 | function util.schedule_function(f, ...) |
66 | -- local pack = table.pack or function(...) return { n = select("#", ...), ... } end --! FOR TESTS TO PASS | ||
67 | |||
68 | local item = { fn = f, args = table.pack(...) } | 66 | local item = { fn = f, args = table.pack(...) } |
69 | table.insert(scheduled_functions, item) | 67 | table.insert(scheduled_functions, item) |
70 | return item | 68 | return item |
@@ -553,6 +551,12 @@ end | |||
553 | 551 | ||
554 | 552 | ||
555 | 553 | ||
554 | |||
555 | |||
556 | |||
557 | |||
558 | |||
559 | |||
556 | function util.get_rocks_provided(rockspec) | 560 | function util.get_rocks_provided(rockspec) |
557 | 561 | ||
558 | if not rockspec and cfg.cache.rocks_provided then | 562 | 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") | |||
10 | local record util | 10 | local record util |
11 | cleanup_path: function(string, string, string, boolean): string | 11 | cleanup_path: function(string, string, string, boolean): string |
12 | split_string: function(string, string, number): {string} | 12 | split_string: function(string, string, number): {string} |
13 | sortedpairs: function<K, V>(tbl: {K: V}, sort_by: core.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> |
14 | deep_merge: function({any : any}, {any : any}) | 14 | deep_merge: function({any : any}, {any : any}) |
15 | deep_merge_under: function({any : any}, {any : any}) | 15 | deep_merge_under: function({any : any}, {any : any}) |
16 | popen_read: function(string, ?string): string | 16 | popen_read: function(string, ?string): string |