diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-07-14 19:59:47 +0300 |
---|---|---|
committer | V1K1NGbg <victor@ilchev.com> | 2024-08-05 20:49:17 +0300 |
commit | 568a5ea1ad0ed1612a92203cb3dd10ff0265c04c (patch) | |
tree | d4f278bc0990a3b757af3610cea8e911d2dae553 | |
parent | 0c9b6eb7ccd12be14c8d0f54782ef11ecf0e5cae (diff) | |
download | luarocks-568a5ea1ad0ed1612a92203cb3dd10ff0265c04c.tar.gz luarocks-568a5ea1ad0ed1612a92203cb3dd10ff0265c04c.tar.bz2 luarocks-568a5ea1ad0ed1612a92203cb3dd10ff0265c04c.zip |
util done halfway
-rw-r--r-- | src/luarocks/core/cfg.d.tl | 1 | ||||
-rw-r--r-- | src/luarocks/core/util.tl | 3 | ||||
-rw-r--r-- | src/luarocks/util-original.lua (renamed from src/luarocks/util.lua) | 0 | ||||
-rw-r--r-- | src/luarocks/util.tl | 97 |
4 files changed, 56 insertions, 45 deletions
diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl index 11d647ff..a741baaa 100644 --- a/src/luarocks/core/cfg.d.tl +++ b/src/luarocks/core/cfg.d.tl | |||
@@ -9,6 +9,7 @@ local record cfg | |||
9 | lib_modules_path: string | 9 | lib_modules_path: string |
10 | rocks_trees: {string| Tree} | 10 | rocks_trees: {string| Tree} |
11 | lua_version: string | 11 | lua_version: string |
12 | deps_mode: string | ||
12 | record Tree | 13 | record Tree |
13 | root: string | 14 | root: string |
14 | rocks_dir: string | 15 | rocks_dir: string |
diff --git a/src/luarocks/core/util.tl b/src/luarocks/core/util.tl index d20fa2e0..30461566 100644 --- a/src/luarocks/core/util.tl +++ b/src/luarocks/core/util.tl | |||
@@ -1,12 +1,13 @@ | |||
1 | 1 | ||
2 | local record util | 2 | local record util |
3 | type CompFn = function(any, any): boolean | ||
3 | end | 4 | end |
4 | 5 | ||
5 | -------------------------------------------------------------------------------- | 6 | -------------------------------------------------------------------------------- |
6 | 7 | ||
7 | local dir_sep = package.config:sub(1, 1) | 8 | local dir_sep = package.config:sub(1, 1) |
8 | 9 | ||
9 | local type CompFn = function(any, any): boolean | 10 | local type CompFn = util.CompFn |
10 | 11 | ||
11 | --- Run a process and read a its output. | 12 | --- Run a process and read a its output. |
12 | -- Equivalent to io.popen(cmd):read("*l"), except that it | 13 | -- Equivalent to io.popen(cmd):read("*l"), except that it |
diff --git a/src/luarocks/util.lua b/src/luarocks/util-original.lua index de9157fc..de9157fc 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util-original.lua | |||
diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl index f556cb03..a8233f31 100644 --- a/src/luarocks/util.tl +++ b/src/luarocks/util.tl | |||
@@ -4,46 +4,57 @@ | |||
4 | -- inside specific functions) to avoid interdependencies, | 4 | -- inside specific functions) to avoid interdependencies, |
5 | -- as this is used in the bootstrapping stage of luarocks.core.cfg. | 5 | -- as this is used in the bootstrapping stage of luarocks.core.cfg. |
6 | 6 | ||
7 | local record util | ||
8 | end | ||
9 | |||
10 | local core = require("luarocks.core.util") | 7 | local core = require("luarocks.core.util") |
11 | 8 | ||
12 | util.cleanup_path = core.cleanup_path --! | 9 | local type CompFn = core.CompFn |
13 | util.split_string = core.split_string | 10 | |
14 | util.sortedpairs = core.sortedpairs | 11 | local record util |
15 | util.deep_merge = core.deep_merge | 12 | cleanup_path: function(string, string, string, boolean): string |
16 | util.deep_merge_under = core.deep_merge_under | 13 | split_string: function(string, string, number): {string} |
17 | util.popen_read = core.popen_read | 14 | sortedpairs: function<K, V, S>({K : V}, CompFn | {K | {K, S}}): function(): K, V, S |
18 | util.show_table = core.show_table | 15 | deep_merge: function({any : any}, {any : any}) |
19 | util.printerr = core.printerr | 16 | deep_merge_under: function({any : any}, {any : any}) |
20 | util.warning = core.warning | 17 | popen_read: function(string, string): string |
21 | util.keys = core.keys | 18 | show_table: function({any : any}, string, string): string |
19 | printerr: function(...: string | number) | ||
20 | warning: function(string) | ||
21 | keys: function<K, V>({K : V}): {K} | ||
22 | |||
23 | record Fn | ||
24 | fn: function(...: any) | ||
25 | args: {any: any} | ||
26 | end | ||
27 | |||
28 | record Rockspec | ||
29 | name: string | ||
30 | version: string --? | ||
31 | end | ||
32 | end | ||
22 | 33 | ||
34 | local type Fn = util.Fn | ||
35 | local type Rockspec = util.Rockspec | ||
23 | 36 | ||
24 | local scheduled_functions: {integer: {string: any}} = {} --? infered from line 48-51 | 37 | local scheduled_functions: {Fn} = {} --? infered from line 48-51 |
25 | 38 | ||
26 | --- Schedule a function to be executed upon program termination. | 39 | --- Schedule a function to be executed upon program termination. |
27 | -- This is useful for actions such as deleting temporary directories | 40 | -- This is useful for actions such as deleting temporary directories |
28 | -- or failure rollbacks. | 41 | -- or failure rollbacks. |
29 | -- @param f function: Function to be executed. | 42 | -- @param f function: Function to be executed. |
30 | -- @param ... arguments to be passed to function. | 43 | -- @param ... arguments to be passed to function. |
31 | -- @return table: A token representing the scheduled execution, --? table -> map | 44 | -- @return table: A token representing the scheduled execution, |
32 | -- which can be used to remove the item later from the list. | 45 | -- which can be used to remove the item later from the list. |
33 | function util.schedule_function(f: function(), ...:any): {string:any} | 46 | function util.schedule_function(f: function(...: any), ...:any): Fn |
34 | assert(type(f) == "function") | 47 | local item: Fn = { fn = f, args = table.pack(...) } |
35 | |||
36 | local item = { fn = f, args = table.pack(...) } | ||
37 | table.insert(scheduled_functions, item) | 48 | table.insert(scheduled_functions, item) |
38 | return item | 49 | return item |
39 | end | 50 | end |
40 | 51 | ||
41 | --- Unschedule a function. | 52 | --- Unschedule a function. |
42 | -- This is useful for cancelling a rollback of a completed operation. | 53 | -- This is useful for cancelling a rollback of a completed operation. |
43 | -- @param item table: The token representing the scheduled function that was --? table -> map | 54 | -- @param item table: The token representing the scheduled function that was |
44 | -- returned from the schedule_function call. | 55 | -- returned from the schedule_function call. |
45 | function util.remove_scheduled_function(item: {string: any}) | 56 | function util.remove_scheduled_function(item: Fn) |
46 | for k, v in pairs(scheduled_functions) do | 57 | for k, v in ipairs(scheduled_functions) do --? ipairs |
47 | if v == item then | 58 | if v == item then |
48 | table.remove(scheduled_functions, k) | 59 | table.remove(scheduled_functions, k) |
49 | return | 60 | return |
@@ -63,7 +74,7 @@ function util.run_scheduled_functions() | |||
63 | end | 74 | end |
64 | for i = #scheduled_functions, 1, -1 do | 75 | for i = #scheduled_functions, 1, -1 do |
65 | local item = scheduled_functions[i] | 76 | local item = scheduled_functions[i] |
66 | item.fn(table.unpack(item.args, 1, item.args.n)) | 77 | item.fn(table.unpack(item.args, 1, item.args.n)) --! here a list and onn line 52 we produse a record |
67 | end | 78 | end |
68 | end | 79 | end |
69 | 80 | ||
@@ -72,7 +83,7 @@ end | |||
72 | -- so it does not include beginning- and end-of-string markers (^$) | 83 | -- so it does not include beginning- and end-of-string markers (^$) |
73 | -- @param s string: The input string | 84 | -- @param s string: The input string |
74 | -- @return string: The equivalent pattern | 85 | -- @return string: The equivalent pattern |
75 | function util.matchquote(s) | 86 | function util.matchquote(s: string): string |
76 | return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) | 87 | return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) |
77 | end | 88 | end |
78 | 89 | ||
@@ -88,7 +99,7 @@ local var_format_pattern = "%$%((%a[%a%d_]+)%)" | |||
88 | -- @param needed_set: a set where keys are the names of | 99 | -- @param needed_set: a set where keys are the names of |
89 | -- needed variables. | 100 | -- needed variables. |
90 | -- @param msg string: the warning message to display. | 101 | -- @param msg string: the warning message to display. |
91 | function util.warn_if_not_used(var_defs, needed_set, msg) | 102 | function util.warn_if_not_used(var_defs: {string: string}, needed_set: {string: string}, msg: string) |
92 | local seen = {} | 103 | local seen = {} |
93 | for _, val in pairs(var_defs) do | 104 | for _, val in pairs(var_defs) do |
94 | for used in val:gmatch(var_format_pattern) do | 105 | for used in val:gmatch(var_format_pattern) do |
@@ -105,7 +116,7 @@ end | |||
105 | -- Output any entries that might remain in $(XYZ) format, | 116 | -- Output any entries that might remain in $(XYZ) format, |
106 | -- warning the user that substitutions have failed. | 117 | -- warning the user that substitutions have failed. |
107 | -- @param line string: the input string | 118 | -- @param line string: the input string |
108 | local function warn_failed_matches(line) | 119 | local function warn_failed_matches(line: string): boolean |
109 | local any_failed = false | 120 | local any_failed = false |
110 | if line:match(var_format_pattern) then | 121 | if line:match(var_format_pattern) then |
111 | for unmatched in line:gmatch(var_format_pattern) do | 122 | for unmatched in line:gmatch(var_format_pattern) do |
@@ -124,9 +135,7 @@ end | |||
124 | -- @param tbl table: Table to have its string values modified. | 135 | -- @param tbl table: Table to have its string values modified. |
125 | -- @param vars table: Table containing string-string key-value pairs | 136 | -- @param vars table: Table containing string-string key-value pairs |
126 | -- representing variables to replace in the strings values of tbl. | 137 | -- representing variables to replace in the strings values of tbl. |
127 | function util.variable_substitutions(tbl, vars) | 138 | function util.variable_substitutions(tbl: {string: string}, vars: {string: string}) |
128 | assert(type(tbl) == "table") | ||
129 | assert(type(vars) == "table") | ||
130 | 139 | ||
131 | local updated = {} | 140 | local updated = {} |
132 | for k, v in pairs(tbl) do | 141 | for k, v in pairs(tbl) do |
@@ -142,24 +151,24 @@ function util.variable_substitutions(tbl, vars) | |||
142 | end | 151 | end |
143 | end | 152 | end |
144 | 153 | ||
145 | function util.lua_versions(sort) | 154 | function util.lua_versions(sort: string): function(): string |
146 | local versions = { "5.1", "5.2", "5.3", "5.4" } | 155 | local versions = { "5.1", "5.2", "5.3", "5.4" } |
147 | local i = 0 | 156 | local i = 0 |
148 | if sort == "descending" then | 157 | if sort == "descending" then |
149 | i = #versions + 1 | 158 | i = #versions + 1 |
150 | return function() | 159 | return function(): string |
151 | i = i - 1 | 160 | i = i - 1 |
152 | return versions[i] | 161 | return versions[i] |
153 | end | 162 | end |
154 | else | 163 | else |
155 | return function() | 164 | return function(): string |
156 | i = i + 1 | 165 | i = i + 1 |
157 | return versions[i] | 166 | return versions[i] |
158 | end | 167 | end |
159 | end | 168 | end |
160 | end | 169 | end |
161 | 170 | ||
162 | function util.lua_path_variables() | 171 | function util.lua_path_variables(): string, string |
163 | local cfg = require("luarocks.core.cfg") | 172 | local cfg = require("luarocks.core.cfg") |
164 | local lpath_var = "LUA_PATH" | 173 | local lpath_var = "LUA_PATH" |
165 | local lcpath_var = "LUA_CPATH" | 174 | local lcpath_var = "LUA_CPATH" |
@@ -176,17 +185,17 @@ function util.lua_path_variables() | |||
176 | return lpath_var, lcpath_var | 185 | return lpath_var, lcpath_var |
177 | end | 186 | end |
178 | 187 | ||
179 | function util.starts_with(s, prefix) | 188 | function util.starts_with(s: string, prefix: string): boolean |
180 | return s:sub(1,#prefix) == prefix | 189 | return s:sub(1,#prefix) == prefix |
181 | end | 190 | end |
182 | 191 | ||
183 | --- Print a line to standard output | 192 | --- Print a line to standard output |
184 | function util.printout(...) | 193 | function util.printout(...: string) |
185 | io.stdout:write(table.concat({...},"\t")) | 194 | io.stdout:write(table.concat({...},"\t")) |
186 | io.stdout:write("\n") | 195 | io.stdout:write("\n") |
187 | end | 196 | end |
188 | 197 | ||
189 | function util.title(msg, porcelain, underline) | 198 | function util.title(msg: string, porcelain: boolean, underline: string) |
190 | if porcelain then return end | 199 | if porcelain then return end |
191 | util.printout() | 200 | util.printout() |
192 | util.printout(msg) | 201 | util.printout(msg) |
@@ -194,7 +203,7 @@ function util.title(msg, porcelain, underline) | |||
194 | util.printout() | 203 | util.printout() |
195 | end | 204 | end |
196 | 205 | ||
197 | function util.this_program(default) | 206 | function util.this_program(default: string): string |
198 | local i = 1 | 207 | local i = 1 |
199 | local last, cur = default, default | 208 | local last, cur = default, default |
200 | while i do | 209 | while i do |
@@ -216,11 +225,11 @@ function util.this_program(default) | |||
216 | return prog | 225 | return prog |
217 | end | 226 | end |
218 | 227 | ||
219 | function util.format_rock_name(name, namespace, version) | 228 | function util.format_rock_name(name: string, namespace: string, version: string): string |
220 | return (namespace and namespace.."/" or "")..name..(version and " "..version or "") | 229 | return (namespace and namespace.."/" or "")..name..(version and " "..version or "") |
221 | end | 230 | end |
222 | 231 | ||
223 | function util.deps_mode_option(parser, program) | 232 | function util.deps_mode_option(parser: {string: any}, program: string) --? type of parser |
224 | local cfg = require("luarocks.core.cfg") | 233 | local cfg = require("luarocks.core.cfg") |
225 | 234 | ||
226 | parser:option("--deps-mode", "How to handle dependencies. Four modes are supported:\n".. | 235 | parser:option("--deps-mode", "How to handle dependencies. Four modes are supported:\n".. |
@@ -238,11 +247,11 @@ function util.deps_mode_option(parser, program) | |||
238 | parser:flag("--nodeps"):hidden(true) | 247 | parser:flag("--nodeps"):hidden(true) |
239 | end | 248 | end |
240 | 249 | ||
241 | function util.see_help(command, program) | 250 | function util.see_help(command: string, program: string): string |
242 | return "See '"..util.this_program(program or "luarocks")..' help'..(command and " "..command or "").."'." | 251 | return "See '"..util.this_program(program or "luarocks")..' help'..(command and " "..command or "").."'." |
243 | end | 252 | end |
244 | 253 | ||
245 | function util.see_also(text) | 254 | function util.see_also(text: string): string |
246 | local see_also = "See also:\n" | 255 | local see_also = "See also:\n" |
247 | if text then | 256 | if text then |
248 | see_also = see_also..text.."\n" | 257 | see_also = see_also..text.."\n" |
@@ -250,7 +259,7 @@ function util.see_also(text) | |||
250 | return see_also.." '"..util.this_program("luarocks").." help' for general options and configuration." | 259 | return see_also.." '"..util.this_program("luarocks").." help' for general options and configuration." |
251 | end | 260 | end |
252 | 261 | ||
253 | function util.announce_install(rockspec) | 262 | function util.announce_install(rockspec: Rockspec) |
254 | local cfg = require("luarocks.core.cfg") | 263 | local cfg = require("luarocks.core.cfg") |
255 | local path = require("luarocks.path") | 264 | local path = require("luarocks.path") |
256 | 265 | ||
@@ -269,7 +278,7 @@ end | |||
269 | -- @param unnamed_paths table: An array of rockspec paths that don't contain rock | 278 | -- @param unnamed_paths table: An array of rockspec paths that don't contain rock |
270 | -- name and version in regular format. | 279 | -- name and version in regular format. |
271 | -- @param subdir string: path to subdirectory. | 280 | -- @param subdir string: path to subdirectory. |
272 | local function collect_rockspecs(versions, paths, unnamed_paths, subdir) | 281 | local function collect_rockspecs(versions: {string: vers.Version}, paths: {string: string}, unnamed_paths: {string}, subdir: string) --! fs stuff |
273 | local fs = require("luarocks.fs") | 282 | local fs = require("luarocks.fs") |
274 | local dir = require("luarocks.dir") | 283 | local dir = require("luarocks.dir") |
275 | local path = require("luarocks.path") | 284 | local path = require("luarocks.path") |
@@ -277,7 +286,7 @@ local function collect_rockspecs(versions, paths, unnamed_paths, subdir) | |||
277 | 286 | ||
278 | if fs.is_dir(subdir) then | 287 | if fs.is_dir(subdir) then |
279 | for file in fs.dir(subdir) do | 288 | for file in fs.dir(subdir) do |
280 | file = dir.path(subdir, file) | 289 | file = dir.path(subdir, file) -- path: function(...: string): string |
281 | 290 | ||
282 | if file:match("rockspec$") and fs.is_file(file) then | 291 | if file:match("rockspec$") and fs.is_file(file) then |
283 | local rock, version = path.parse_name(file) | 292 | local rock, version = path.parse_name(file) |