diff options
author | mpeterv <petjamelnik@yandex.ru> | 2014-03-20 22:15:55 +0400 |
---|---|---|
committer | mpeterv <petjamelnik@yandex.ru> | 2014-03-20 22:15:55 +0400 |
commit | 235124b431f2007dc2ecb6ee061c318d5f9b84a3 (patch) | |
tree | 7eaeed584466ba3c9f57d80a0e0c08f97dce8515 | |
parent | b63e863cf3ac9c830c5c81ece8e30f91edba4adc (diff) | |
download | luarocks-235124b431f2007dc2ecb6ee061c318d5f9b84a3.tar.gz luarocks-235124b431f2007dc2ecb6ee061c318d5f9b84a3.tar.bz2 luarocks-235124b431f2007dc2ecb6ee061c318d5f9b84a3.zip |
Unmoduled luarocks.util
-rw-r--r-- | src/luarocks/util.lua | 83 |
1 files changed, 43 insertions, 40 deletions
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 4e70aa91..2c704a52 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -4,7 +4,8 @@ | |||
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.cfg. | 5 | -- as this is used in the bootstrapping stage of luarocks.cfg. |
6 | 6 | ||
7 | module("luarocks.util", package.seeall) | 7 | --module("luarocks.util", package.seeall) |
8 | local util = {} | ||
8 | 9 | ||
9 | local scheduled_functions = {} | 10 | local scheduled_functions = {} |
10 | local debug = require("debug") | 11 | local debug = require("debug") |
@@ -16,7 +17,7 @@ local debug = require("debug") | |||
16 | -- @param ... arguments to be passed to function. | 17 | -- @param ... arguments to be passed to function. |
17 | -- @return table: A token representing the scheduled execution, | 18 | -- @return table: A token representing the scheduled execution, |
18 | -- which can be used to remove the item later from the list. | 19 | -- which can be used to remove the item later from the list. |
19 | function schedule_function(f, ...) | 20 | function util.schedule_function(f, ...) |
20 | assert(type(f) == "function") | 21 | assert(type(f) == "function") |
21 | 22 | ||
22 | local item = { fn = f, args = {...} } | 23 | local item = { fn = f, args = {...} } |
@@ -28,7 +29,7 @@ end | |||
28 | -- This is useful for cancelling a rollback of a completed operation. | 29 | -- This is useful for cancelling a rollback of a completed operation. |
29 | -- @param item table: The token representing the scheduled function that was | 30 | -- @param item table: The token representing the scheduled function that was |
30 | -- returned from the schedule_function call. | 31 | -- returned from the schedule_function call. |
31 | function remove_scheduled_function(item) | 32 | function util.remove_scheduled_function(item) |
32 | for k, v in pairs(scheduled_functions) do | 33 | for k, v in pairs(scheduled_functions) do |
33 | if v == item then | 34 | if v == item then |
34 | table.remove(scheduled_functions, k) | 35 | table.remove(scheduled_functions, k) |
@@ -42,7 +43,7 @@ end | |||
42 | -- corresponding cleanup functions. Calling this function will run | 43 | -- corresponding cleanup functions. Calling this function will run |
43 | -- these function, erasing temporaries. | 44 | -- these function, erasing temporaries. |
44 | -- Functions are executed in the inverse order they were scheduled. | 45 | -- Functions are executed in the inverse order they were scheduled. |
45 | function run_scheduled_functions() | 46 | function util.run_scheduled_functions() |
46 | local fs = require("luarocks.fs") | 47 | local fs = require("luarocks.fs") |
47 | fs.change_dir_to_root() | 48 | fs.change_dir_to_root() |
48 | for i = #scheduled_functions, 1, -1 do | 49 | for i = #scheduled_functions, 1, -1 do |
@@ -56,7 +57,7 @@ end | |||
56 | -- so it does not include beginning- and end-of-string markers (^$) | 57 | -- so it does not include beginning- and end-of-string markers (^$) |
57 | -- @param s string: The input string | 58 | -- @param s string: The input string |
58 | -- @return string: The equivalent pattern | 59 | -- @return string: The equivalent pattern |
59 | function matchquote(s) | 60 | function util.matchquote(s) |
60 | return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) | 61 | return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) |
61 | end | 62 | end |
62 | 63 | ||
@@ -65,7 +66,7 @@ end | |||
65 | -- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz", | 66 | -- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz", |
66 | -- it would return the following: | 67 | -- it would return the following: |
67 | -- {["bla"] = true, ["tux"] = "beep", ["baz"] = true}, "foo", "bar". | 68 | -- {["bla"] = true, ["tux"] = "beep", ["baz"] = true}, "foo", "bar". |
68 | function parse_flags(...) | 69 | function util.parse_flags(...) |
69 | local args = {...} | 70 | local args = {...} |
70 | local flags = {} | 71 | local flags = {} |
71 | for i = #args, 1, -1 do | 72 | for i = #args, 1, -1 do |
@@ -90,7 +91,7 @@ end | |||
90 | -- in the flags table. If no flags are passed as varargs, the | 91 | -- in the flags table. If no flags are passed as varargs, the |
91 | -- entire flags table is forwarded. | 92 | -- entire flags table is forwarded. |
92 | -- @return string... A variable number of strings | 93 | -- @return string... A variable number of strings |
93 | function forward_flags(flags, ...) | 94 | function util.forward_flags(flags, ...) |
94 | assert(type(flags) == "table") | 95 | assert(type(flags) == "table") |
95 | local out = {} | 96 | local out = {} |
96 | local filter = select('#', ...) | 97 | local filter = select('#', ...) |
@@ -119,14 +120,14 @@ end | |||
119 | -- @param dst Destination table, which will receive src's contents. | 120 | -- @param dst Destination table, which will receive src's contents. |
120 | -- @param src Table which provides new contents to dst. | 121 | -- @param src Table which provides new contents to dst. |
121 | -- @see platform_overrides | 122 | -- @see platform_overrides |
122 | function deep_merge(dst, src) | 123 | function util.deep_merge(dst, src) |
123 | for k, v in pairs(src) do | 124 | for k, v in pairs(src) do |
124 | if type(v) == "table" then | 125 | if type(v) == "table" then |
125 | if not dst[k] then | 126 | if not dst[k] then |
126 | dst[k] = {} | 127 | dst[k] = {} |
127 | end | 128 | end |
128 | if type(dst[k]) == "table" then | 129 | if type(dst[k]) == "table" then |
129 | deep_merge(dst[k], v) | 130 | util.deep_merge(dst[k], v) |
130 | else | 131 | else |
131 | dst[k] = v | 132 | dst[k] = v |
132 | end | 133 | end |
@@ -149,7 +150,7 @@ end | |||
149 | -- tbl.x are preserved). | 150 | -- tbl.x are preserved). |
150 | -- @param tbl table or nil: Table which may contain a "platforms" field; | 151 | -- @param tbl table or nil: Table which may contain a "platforms" field; |
151 | -- if it doesn't (or if nil is passed), this function does nothing. | 152 | -- if it doesn't (or if nil is passed), this function does nothing. |
152 | function platform_overrides(tbl) | 153 | function util.platform_overrides(tbl) |
153 | assert(type(tbl) == "table" or not tbl) | 154 | assert(type(tbl) == "table" or not tbl) |
154 | 155 | ||
155 | local cfg = require("luarocks.cfg") | 156 | local cfg = require("luarocks.cfg") |
@@ -160,7 +161,7 @@ function platform_overrides(tbl) | |||
160 | for _, platform in ipairs(cfg.platforms) do | 161 | for _, platform in ipairs(cfg.platforms) do |
161 | local platform_tbl = tbl.platforms[platform] | 162 | local platform_tbl = tbl.platforms[platform] |
162 | if platform_tbl then | 163 | if platform_tbl then |
163 | deep_merge(tbl, platform_tbl) | 164 | util.deep_merge(tbl, platform_tbl) |
164 | end | 165 | end |
165 | end | 166 | end |
166 | end | 167 | end |
@@ -192,7 +193,7 @@ end | |||
192 | -- @param needed_set: a set where keys are the names of | 193 | -- @param needed_set: a set where keys are the names of |
193 | -- needed variables. | 194 | -- needed variables. |
194 | -- @param msg string: the warning message to display. | 195 | -- @param msg string: the warning message to display. |
195 | function warn_if_not_used(var_defs, needed_set, msg) | 196 | function util.warn_if_not_used(var_defs, needed_set, msg) |
196 | needed_set = make_shallow_copy(needed_set) | 197 | needed_set = make_shallow_copy(needed_set) |
197 | for var,val in pairs(var_defs) do | 198 | for var,val in pairs(var_defs) do |
198 | for used in val:gmatch(var_format_pattern) do | 199 | for used in val:gmatch(var_format_pattern) do |
@@ -200,7 +201,7 @@ function warn_if_not_used(var_defs, needed_set, msg) | |||
200 | end | 201 | end |
201 | end | 202 | end |
202 | for var,_ in pairs(needed_set) do | 203 | for var,_ in pairs(needed_set) do |
203 | warning(msg:format(var)) | 204 | util.warning(msg:format(var)) |
204 | end | 205 | end |
205 | end | 206 | end |
206 | 207 | ||
@@ -211,7 +212,7 @@ local function warn_failed_matches(line) | |||
211 | local any_failed = false | 212 | local any_failed = false |
212 | if line:match(var_format_pattern) then | 213 | if line:match(var_format_pattern) then |
213 | for unmatched in line:gmatch(var_format_pattern) do | 214 | for unmatched in line:gmatch(var_format_pattern) do |
214 | warning("unmatched variable " .. unmatched) | 215 | util.warning("unmatched variable " .. unmatched) |
215 | any_failed = true | 216 | any_failed = true |
216 | end | 217 | end |
217 | end | 218 | end |
@@ -226,7 +227,7 @@ end | |||
226 | -- @param tbl table: Table to have its string values modified. | 227 | -- @param tbl table: Table to have its string values modified. |
227 | -- @param vars table: Table containing string-string key-value pairs | 228 | -- @param vars table: Table containing string-string key-value pairs |
228 | -- representing variables to replace in the strings values of tbl. | 229 | -- representing variables to replace in the strings values of tbl. |
229 | function variable_substitutions(tbl, vars) | 230 | function util.variable_substitutions(tbl, vars) |
230 | assert(type(tbl) == "table") | 231 | assert(type(tbl) == "table") |
231 | assert(type(vars) == "table") | 232 | assert(type(vars) == "table") |
232 | 233 | ||
@@ -247,7 +248,7 @@ end | |||
247 | --- Return an array of keys of a table. | 248 | --- Return an array of keys of a table. |
248 | -- @param tbl table: The input table. | 249 | -- @param tbl table: The input table. |
249 | -- @return table: The array of keys. | 250 | -- @return table: The array of keys. |
250 | function keys(tbl) | 251 | function util.keys(tbl) |
251 | local ks = {} | 252 | local ks = {} |
252 | for k,_ in pairs(tbl) do | 253 | for k,_ in pairs(tbl) do |
253 | table.insert(ks, k) | 254 | table.insert(ks, k) |
@@ -275,7 +276,7 @@ end | |||
275 | -- to be used by table.sort when sorting keys. | 276 | -- to be used by table.sort when sorting keys. |
276 | -- @see sortedpairs | 277 | -- @see sortedpairs |
277 | local function sortedpairs_iterator(tbl, sort_function) | 278 | local function sortedpairs_iterator(tbl, sort_function) |
278 | local ks = keys(tbl) | 279 | local ks = util.keys(tbl) |
279 | if not sort_function or type(sort_function) == "function" then | 280 | if not sort_function or type(sort_function) == "function" then |
280 | table.sort(ks, sort_function or default_sort) | 281 | table.sort(ks, sort_function or default_sort) |
281 | for _, k in ipairs(ks) do | 282 | for _, k in ipairs(ks) do |
@@ -313,11 +314,11 @@ end | |||
313 | -- is a string representing the field name, and the second element is a priority table | 314 | -- is a string representing the field name, and the second element is a priority table |
314 | -- for that key. | 315 | -- for that key. |
315 | -- @return function: the iterator function. | 316 | -- @return function: the iterator function. |
316 | function sortedpairs(tbl, sort_function) | 317 | function util.sortedpairs(tbl, sort_function) |
317 | return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) | 318 | return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) |
318 | end | 319 | end |
319 | 320 | ||
320 | function lua_versions() | 321 | function util.lua_versions() |
321 | local versions = { "5.1", "5.2" } | 322 | local versions = { "5.1", "5.2" } |
322 | local i = 0 | 323 | local i = 0 |
323 | return function() | 324 | return function() |
@@ -326,37 +327,37 @@ function lua_versions() | |||
326 | end | 327 | end |
327 | end | 328 | end |
328 | 329 | ||
329 | function starts_with(s, prefix) | 330 | function util.starts_with(s, prefix) |
330 | return s:sub(1,#prefix) == prefix | 331 | return s:sub(1,#prefix) == prefix |
331 | end | 332 | end |
332 | 333 | ||
333 | --- Print a line to standard output | 334 | --- Print a line to standard output |
334 | function printout(...) | 335 | function util.printout(...) |
335 | io.stdout:write(table.concat({...},"\t")) | 336 | io.stdout:write(table.concat({...},"\t")) |
336 | io.stdout:write("\n") | 337 | io.stdout:write("\n") |
337 | end | 338 | end |
338 | 339 | ||
339 | --- Print a line to standard error | 340 | --- Print a line to standard error |
340 | function printerr(...) | 341 | function util.printerr(...) |
341 | io.stderr:write(table.concat({...},"\t")) | 342 | io.stderr:write(table.concat({...},"\t")) |
342 | io.stderr:write("\n") | 343 | io.stderr:write("\n") |
343 | end | 344 | end |
344 | 345 | ||
345 | --- Display a warning message. | 346 | --- Display a warning message. |
346 | -- @param msg string: the warning message | 347 | -- @param msg string: the warning message |
347 | function warning(msg) | 348 | function util.warning(msg) |
348 | printerr("Warning: "..msg) | 349 | util.printerr("Warning: "..msg) |
349 | end | 350 | end |
350 | 351 | ||
351 | function title(msg, porcelain, underline) | 352 | function util.title(msg, porcelain, underline) |
352 | if porcelain then return end | 353 | if porcelain then return end |
353 | printout() | 354 | util.printout() |
354 | printout(msg) | 355 | util.printout(msg) |
355 | printout((underline or "-"):rep(#msg)) | 356 | util.printout((underline or "-"):rep(#msg)) |
356 | printout() | 357 | util.printout() |
357 | end | 358 | end |
358 | 359 | ||
359 | function this_program(default) | 360 | function util.this_program(default) |
360 | local i = 1 | 361 | local i = 1 |
361 | local last, cur = default, default | 362 | local last, cur = default, default |
362 | while i do | 363 | while i do |
@@ -369,7 +370,7 @@ function this_program(default) | |||
369 | return last:sub(2) | 370 | return last:sub(2) |
370 | end | 371 | end |
371 | 372 | ||
372 | function deps_mode_help(program) | 373 | function util.deps_mode_help(program) |
373 | local cfg = require("luarocks.cfg") | 374 | local cfg = require("luarocks.cfg") |
374 | return [[ | 375 | return [[ |
375 | --deps-mode=<mode> How to handle dependencies. Four modes are supported: | 376 | --deps-mode=<mode> How to handle dependencies. Four modes are supported: |
@@ -383,18 +384,18 @@ function deps_mode_help(program) | |||
383 | The default mode may be set with the deps_mode entry | 384 | The default mode may be set with the deps_mode entry |
384 | in the configuration file. | 385 | in the configuration file. |
385 | The current default is "]]..cfg.deps_mode..[[". | 386 | The current default is "]]..cfg.deps_mode..[[". |
386 | Type ']]..this_program(program or "luarocks")..[[' with no arguments to see | 387 | Type ']]..util.this_program(program or "luarocks")..[[' with no arguments to see |
387 | your list of rocks trees. | 388 | your list of rocks trees. |
388 | ]] | 389 | ]] |
389 | end | 390 | end |
390 | 391 | ||
391 | function see_help(command, program) | 392 | function util.see_help(command, program) |
392 | return "See '"..this_program(program or "luarocks")..' help '..command.."'." | 393 | return "See '"..util.this_program(program or "luarocks")..' help '..command.."'." |
393 | end | 394 | end |
394 | 395 | ||
395 | -- from http://lua-users.org/wiki/SplitJoin | 396 | -- from http://lua-users.org/wiki/SplitJoin |
396 | -- by PhilippeLhoste | 397 | -- by PhilippeLhoste |
397 | function split_string(str, delim, maxNb) | 398 | function util.split_string(str, delim, maxNb) |
398 | -- Eliminate bad cases... | 399 | -- Eliminate bad cases... |
399 | if string.find(str, delim) == nil then | 400 | if string.find(str, delim) == nil then |
400 | return { str } | 401 | return { str } |
@@ -423,10 +424,10 @@ end | |||
423 | -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". | 424 | -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". |
424 | -- @param list string: A path string (from $PATH or package.path) | 425 | -- @param list string: A path string (from $PATH or package.path) |
425 | -- @param sep string: The separator | 426 | -- @param sep string: The separator |
426 | function remove_path_dupes(list, sep) | 427 | function util.remove_path_dupes(list, sep) |
427 | assert(type(list) == "string") | 428 | assert(type(list) == "string") |
428 | assert(type(sep) == "string") | 429 | assert(type(sep) == "string") |
429 | local parts = split_string(list, sep) | 430 | local parts = util.split_string(list, sep) |
430 | local final, entries = {}, {} | 431 | local final, entries = {}, {} |
431 | for _, part in ipairs(parts) do | 432 | for _, part in ipairs(parts) do |
432 | if not entries[part] then | 433 | if not entries[part] then |
@@ -454,7 +455,7 @@ end | |||
454 | -- @param name string: is the name of the table (optional) | 455 | -- @param name string: is the name of the table (optional) |
455 | -- @param indent string: is a first indentation (optional). | 456 | -- @param indent string: is a first indentation (optional). |
456 | -- @return string: the pretty-printed table | 457 | -- @return string: the pretty-printed table |
457 | function show_table(t, name, indent) | 458 | function util.show_table(t, name, indent) |
458 | local cart -- a container | 459 | local cart -- a container |
459 | local autoref -- for self references | 460 | local autoref -- for self references |
460 | 461 | ||
@@ -520,7 +521,7 @@ function show_table(t, name, indent) | |||
520 | return cart .. autoref | 521 | return cart .. autoref |
521 | end | 522 | end |
522 | 523 | ||
523 | function array_contains(tbl, value) | 524 | function util.array_contains(tbl, value) |
524 | for _, v in ipairs(tbl) do | 525 | for _, v in ipairs(tbl) do |
525 | if v == value then | 526 | if v == value then |
526 | return true | 527 | return true |
@@ -532,6 +533,8 @@ end | |||
532 | -- Quote Lua string, analogous to fs.Q. | 533 | -- Quote Lua string, analogous to fs.Q. |
533 | -- @param s A string, such as "hello" | 534 | -- @param s A string, such as "hello" |
534 | -- @return string: A quoted string, such as '"hello"' | 535 | -- @return string: A quoted string, such as '"hello"' |
535 | function LQ(s) | 536 | function util.LQ(s) |
536 | return ("%q"):format(s) | 537 | return ("%q"):format(s) |
537 | end | 538 | end |
539 | |||
540 | return util | ||