diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:57 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | f2697b158f0b94f615dffee553ac18cfbd72cd52 (patch) | |
tree | 1053d0676f796298b256c928c8b195f7db35c805 | |
parent | 08351f44d22f07f0eaf87fcc5732a32e7e5282df (diff) | |
download | luarocks-f2697b158f0b94f615dffee553ac18cfbd72cd52.tar.gz luarocks-f2697b158f0b94f615dffee553ac18cfbd72cd52.tar.bz2 luarocks-f2697b158f0b94f615dffee553ac18cfbd72cd52.zip |
Teal: convert luarocks.persist
-rw-r--r-- | src/luarocks/persist.tl (renamed from src/luarocks/persist.lua) | 100 |
1 files changed, 58 insertions, 42 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.tl index 4dcd930a..62260290 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.tl | |||
@@ -1,17 +1,34 @@ | |||
1 | 1 | ||
2 | --- Utility module for loading files into tables and | 2 | --- Utility module for loading files into tables and |
3 | -- saving tables into files. | 3 | -- saving tables into files. |
4 | local persist = {} | 4 | local record persist |
5 | run_file: function(string, {string:any}): boolean, any | string, string | ||
6 | load_into_table: function(string, ?{string:any}) : {any: any}, {any: any} | string, string | ||
7 | |||
8 | interface Writer | ||
9 | write: function(self: Writer, data: string) | ||
10 | buffer: {number | string} | ||
11 | end | ||
12 | end | ||
5 | 13 | ||
6 | local core = require("luarocks.core.persist") | 14 | local core = require("luarocks.core.persist") |
7 | local util = require("luarocks.util") | 15 | local util = require("luarocks.util") |
8 | local dir = require("luarocks.dir") | 16 | local dir = require("luarocks.dir") |
9 | local fs = require("luarocks.fs") | 17 | local fs = require("luarocks.fs") |
18 | local cfg = require("luarocks.core.cfg") | ||
19 | |||
20 | local type Config = cfg | ||
21 | |||
22 | local type SortBy = require("luarocks.core.types.ordering").SortBy | ||
23 | |||
24 | local type PersistableTable = require("luarocks.core.types.persist").PersistableTable | ||
25 | |||
26 | local type Writer = persist.Writer | ||
10 | 27 | ||
11 | persist.run_file = core.run_file | 28 | persist.run_file = core.run_file |
12 | persist.load_into_table = core.load_into_table | 29 | persist.load_into_table = core.load_into_table |
13 | 30 | ||
14 | local write_table | 31 | local write_table: function(out: Writer, tbl: PersistableTable, level: integer, sort_by: SortBy<number | string>) |
15 | 32 | ||
16 | --- Write a value as Lua code. | 33 | --- Write a value as Lua code. |
17 | -- This function handles only numbers and strings, invoking write_table | 34 | -- This function handles only numbers and strings, invoking write_table |
@@ -21,11 +38,11 @@ local write_table | |||
21 | -- @param level number: the indentation level | 38 | -- @param level number: the indentation level |
22 | -- @param sub_order table: optional prioritization table | 39 | -- @param sub_order table: optional prioritization table |
23 | -- @see write_table | 40 | -- @see write_table |
24 | function persist.write_value(out, v, level, sub_order) | 41 | function persist.write_value(out: Writer, v: any, level?: integer, sub_order?: SortBy<number | string>) |
25 | if type(v) == "table" then | 42 | if v is PersistableTable then |
26 | level = level or 0 | 43 | level = level or 0 |
27 | write_table(out, v, level + 1, sub_order) | 44 | write_table(out, v, level + 1, sub_order) |
28 | elseif type(v) == "string" then | 45 | elseif v is string then |
29 | if v:match("[\r\n]") then | 46 | if v:match("[\r\n]") then |
30 | local open, close = "[[", "]]" | 47 | local open, close = "[[", "]]" |
31 | local equals = 0 | 48 | local equals = 0 |
@@ -44,9 +61,9 @@ function persist.write_value(out, v, level, sub_order) | |||
44 | end | 61 | end |
45 | end | 62 | end |
46 | 63 | ||
47 | local is_valid_plain_key | 64 | local is_valid_plain_key: function(string): boolean |
48 | do | 65 | do |
49 | local keywords = { | 66 | local keywords: {string: boolean} = { |
50 | ["and"] = true, | 67 | ["and"] = true, |
51 | ["break"] = true, | 68 | ["break"] = true, |
52 | ["do"] = true, | 69 | ["do"] = true, |
@@ -70,15 +87,14 @@ do | |||
70 | ["until"] = true, | 87 | ["until"] = true, |
71 | ["while"] = true, | 88 | ["while"] = true, |
72 | } | 89 | } |
73 | function is_valid_plain_key(k) | 90 | function is_valid_plain_key(k: string): boolean |
74 | return type(k) == "string" | 91 | return k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") |
75 | and k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") | ||
76 | and not keywords[k] | 92 | and not keywords[k] |
77 | end | 93 | end |
78 | end | 94 | end |
79 | 95 | ||
80 | local function write_table_key_assignment(out, k, level) | 96 | local function write_table_key_assignment(out: Writer, k: string | number, level: integer) |
81 | if is_valid_plain_key(k) then | 97 | if k is string and is_valid_plain_key(k) then |
82 | out:write(k) | 98 | out:write(k) |
83 | else | 99 | else |
84 | out:write("[") | 100 | out:write("[") |
@@ -95,27 +111,27 @@ end | |||
95 | -- @param out table or userdata: a writer object supporting :write() method. | 111 | -- @param out table or userdata: a writer object supporting :write() method. |
96 | -- @param tbl table: the table to be written. | 112 | -- @param tbl table: the table to be written. |
97 | -- @param level number: the indentation level | 113 | -- @param level number: the indentation level |
98 | -- @param field_order table: optional prioritization table | 114 | -- @param sort_by table: optional prioritization table |
99 | write_table = function(out, tbl, level, field_order) | 115 | write_table = function(out: Writer, tbl: PersistableTable, level: integer, sort_by: SortBy<number | string>) |
100 | out:write("{") | 116 | out:write("{") |
101 | local sep = "\n" | 117 | local sep = "\n" |
102 | local indentation = " " | 118 | local indentation = " " |
103 | local indent = true | 119 | local indent = true |
104 | local i = 1 | 120 | local i = 1 |
105 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 121 | for k, v, sub_order in util.sortedpairs(tbl, sort_by) do |
106 | out:write(sep) | 122 | out:write(sep) |
107 | if indent then | 123 | if indent then |
108 | for _ = 1, level do out:write(indentation) end | 124 | for _ = 1, level do out:write(indentation) end |
109 | end | 125 | end |
110 | 126 | ||
111 | if k == i then | 127 | if k is number then |
112 | i = i + 1 | 128 | i = i + 1 |
113 | else | 129 | else |
114 | write_table_key_assignment(out, k, level) | 130 | write_table_key_assignment(out, k, level) |
115 | end | 131 | end |
116 | 132 | ||
117 | persist.write_value(out, v, level, sub_order) | 133 | persist.write_value(out, v, level, sub_order) |
118 | if type(v) == "number" then | 134 | if v is number then |
119 | sep = ", " | 135 | sep = ", " |
120 | indent = false | 136 | indent = false |
121 | else | 137 | else |
@@ -133,11 +149,11 @@ end | |||
133 | --- Write a table as series of assignments to a writer object. | 149 | --- Write a table as series of assignments to a writer object. |
134 | -- @param out table or userdata: a writer object supporting :write() method. | 150 | -- @param out table or userdata: a writer object supporting :write() method. |
135 | -- @param tbl table: the table to be written. | 151 | -- @param tbl table: the table to be written. |
136 | -- @param field_order table: optional prioritization table | 152 | -- @param sort_by table: optional prioritization table |
137 | -- @return true if successful; nil and error message if failed. | 153 | -- @return true if successful; nil and error message if failed. |
138 | local function write_table_as_assignments(out, tbl, field_order) | 154 | local function write_table_as_assignments(out: Writer, tbl: PersistableTable, sort_by: SortBy<number | string>): boolean, string |
139 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 155 | for k, v, sub_order in util.sortedpairs(tbl, sort_by) do |
140 | if not is_valid_plain_key(k) then | 156 | if not (k is string and is_valid_plain_key(k)) then |
141 | return nil, "cannot store '"..tostring(k).."' as a plain key." | 157 | return nil, "cannot store '"..tostring(k).."' as a plain key." |
142 | end | 158 | end |
143 | out:write(k.." = ") | 159 | out:write(k.." = ") |
@@ -150,7 +166,7 @@ end | |||
150 | --- Write a table using Lua table syntax to a writer object. | 166 | --- Write a table using Lua table syntax to a writer object. |
151 | -- @param out table or userdata: a writer object supporting :write() method. | 167 | -- @param out table or userdata: a writer object supporting :write() method. |
152 | -- @param tbl table: the table to be written. | 168 | -- @param tbl table: the table to be written. |
153 | local function write_table_as_table(out, tbl) | 169 | local function write_table_as_table(out: Writer, tbl: PersistableTable) |
154 | out:write("return {\n") | 170 | out:write("return {\n") |
155 | for k, v, sub_order in util.sortedpairs(tbl) do | 171 | for k, v, sub_order in util.sortedpairs(tbl) do |
156 | out:write(" ") | 172 | out:write(" ") |
@@ -166,12 +182,12 @@ end | |||
166 | -- Only numbers, strings and tables (containing numbers, strings | 182 | -- Only numbers, strings and tables (containing numbers, strings |
167 | -- or other recursively processed tables) are supported. | 183 | -- or other recursively processed tables) are supported. |
168 | -- @param tbl table: the table containing the data to be written | 184 | -- @param tbl table: the table containing the data to be written |
169 | -- @param field_order table: an optional array indicating the order of top-level fields. | 185 | -- @param sort_by table: an optional array indicating the order of top-level fields. |
170 | -- @return persisted data as string; or nil and an error message | 186 | -- @return persisted data as string; or nil and an error message |
171 | function persist.save_from_table_to_string(tbl, field_order) | 187 | function persist.save_from_table_to_string(tbl: PersistableTable, sort_by?: SortBy<number | string>): string, string |
172 | local out = {buffer = {}} | 188 | local out: Writer = {buffer = {}} |
173 | function out:write(data) table.insert(self.buffer, data) end | 189 | function out:write(data: string) table.insert(self.buffer, data) end |
174 | local ok, err = write_table_as_assignments(out, tbl, field_order) | 190 | local ok, err = write_table_as_assignments(out, tbl, sort_by) |
175 | if not ok then | 191 | if not ok then |
176 | return nil, err | 192 | return nil, err |
177 | end | 193 | end |
@@ -184,17 +200,17 @@ end | |||
184 | -- or other recursively processed tables) are supported. | 200 | -- or other recursively processed tables) are supported. |
185 | -- @param filename string: the output filename | 201 | -- @param filename string: the output filename |
186 | -- @param tbl table: the table containing the data to be written | 202 | -- @param tbl table: the table containing the data to be written |
187 | -- @param field_order table: an optional array indicating the order of top-level fields. | 203 | -- @param sort_by table: an optional array indicating the order of top-level fields. |
188 | -- @return boolean or (nil, string): true if successful, or nil and a | 204 | -- @return boolean or (nil, string): true if successful, or nil and a |
189 | -- message in case of errors. | 205 | -- message in case of errors. |
190 | function persist.save_from_table(filename, tbl, field_order) | 206 | function persist.save_from_table(filename: string, tbl: PersistableTable, sort_by?: SortBy<number | string>): boolean, string |
191 | local prefix = dir.dir_name(filename) | 207 | local prefix = dir.dir_name(filename) |
192 | fs.make_dir(prefix) | 208 | fs.make_dir(prefix) |
193 | local out = io.open(filename, "w") | 209 | local out = io.open(filename, "w") |
194 | if not out then | 210 | if not out then |
195 | return nil, "Cannot create file at "..filename | 211 | return nil, "Cannot create file at "..filename |
196 | end | 212 | end |
197 | local ok, err = write_table_as_assignments(out, tbl, field_order) | 213 | local ok, err = write_table_as_assignments(out as Writer, tbl, sort_by) |
198 | out:close() | 214 | out:close() |
199 | if not ok then | 215 | if not ok then |
200 | return nil, err | 216 | return nil, err |
@@ -210,46 +226,46 @@ end | |||
210 | -- @param tbl table: the table containing the data to be written | 226 | -- @param tbl table: the table containing the data to be written |
211 | -- @return boolean or (nil, string): true if successful, or nil and a | 227 | -- @return boolean or (nil, string): true if successful, or nil and a |
212 | -- message in case of errors. | 228 | -- message in case of errors. |
213 | function persist.save_as_module(filename, tbl) | 229 | function persist.save_as_module(filename: string, tbl: PersistableTable): boolean, string |
214 | local out = io.open(filename, "w") | 230 | local out = io.open(filename, "w") |
215 | if not out then | 231 | if not out then |
216 | return nil, "Cannot create file at "..filename | 232 | return nil, "Cannot create file at "..filename |
217 | end | 233 | end |
218 | write_table_as_table(out, tbl) | 234 | write_table_as_table(out as Writer, tbl) |
219 | out:close() | 235 | out:close() |
220 | return true | 236 | return true |
221 | end | 237 | end |
222 | 238 | ||
223 | function persist.load_config_file_if_basic(filename, cfg) | 239 | function persist.load_config_file_if_basic(filename: string, config: Config): PersistableTable, string |
224 | local env = { | 240 | local env = { |
225 | home = cfg.home | 241 | home = config.home |
226 | } | 242 | } |
227 | local result, err, errcode = persist.load_into_table(filename, env) | 243 | local result, _, errcode = persist.load_into_table(filename, env) |
228 | if errcode == "load" or errcode == "run" then | 244 | if errcode == "load" or errcode == "run" then |
229 | -- bad config file or depends on env, so error out | 245 | -- bad config file or depends on env, so error out |
230 | return nil, "Could not read existing config file " .. filename | 246 | return nil, "Could not read existing config file " .. filename |
231 | end | 247 | end |
232 | 248 | ||
233 | local tbl | 249 | local tbl: PersistableTable |
234 | if errcode == "open" then | 250 | if errcode == "open" then |
235 | -- could not open, maybe file does not exist | 251 | -- could not open, maybe file does not exist |
236 | tbl = {} | 252 | tbl = {} |
237 | else | 253 | else |
238 | tbl = result | 254 | tbl = result as PersistableTable -- the shape of result is not validated |
239 | tbl.home = nil | 255 | tbl.home = nil |
240 | end | 256 | end |
241 | 257 | ||
242 | return tbl | 258 | return tbl |
243 | end | 259 | end |
244 | 260 | ||
245 | function persist.save_default_lua_version(prefix, lua_version) | 261 | function persist.save_default_lua_version(prefix: string, lua_version: string): boolean, string |
246 | local ok, err = fs.make_dir(prefix) | 262 | local ok, err_makedir = fs.make_dir(prefix) |
247 | if not ok then | 263 | if not ok then |
248 | return nil, err | 264 | return nil, err_makedir |
249 | end | 265 | end |
250 | local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") | 266 | local fd, err_open = io.open(dir.path(prefix, "default-lua-version.lua"), "w") |
251 | if not fd then | 267 | if not fd then |
252 | return nil, err | 268 | return nil, err_open |
253 | end | 269 | end |
254 | fd:write('return "' .. lua_version .. '"\n') | 270 | fd:write('return "' .. lua_version .. '"\n') |
255 | fd:close() | 271 | fd:close() |