From 235124b431f2007dc2ecb6ee061c318d5f9b84a3 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 20 Mar 2014 22:15:55 +0400 Subject: Unmoduled luarocks.util --- src/luarocks/util.lua | 83 ++++++++++++++++++++++++++------------------------- 1 file 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 @@ -- inside specific functions) to avoid interdependencies, -- as this is used in the bootstrapping stage of luarocks.cfg. -module("luarocks.util", package.seeall) +--module("luarocks.util", package.seeall) +local util = {} local scheduled_functions = {} local debug = require("debug") @@ -16,7 +17,7 @@ local debug = require("debug") -- @param ... arguments to be passed to function. -- @return table: A token representing the scheduled execution, -- which can be used to remove the item later from the list. -function schedule_function(f, ...) +function util.schedule_function(f, ...) assert(type(f) == "function") local item = { fn = f, args = {...} } @@ -28,7 +29,7 @@ end -- This is useful for cancelling a rollback of a completed operation. -- @param item table: The token representing the scheduled function that was -- returned from the schedule_function call. -function remove_scheduled_function(item) +function util.remove_scheduled_function(item) for k, v in pairs(scheduled_functions) do if v == item then table.remove(scheduled_functions, k) @@ -42,7 +43,7 @@ end -- corresponding cleanup functions. Calling this function will run -- these function, erasing temporaries. -- Functions are executed in the inverse order they were scheduled. -function run_scheduled_functions() +function util.run_scheduled_functions() local fs = require("luarocks.fs") fs.change_dir_to_root() for i = #scheduled_functions, 1, -1 do @@ -56,7 +57,7 @@ end -- so it does not include beginning- and end-of-string markers (^$) -- @param s string: The input string -- @return string: The equivalent pattern -function matchquote(s) +function util.matchquote(s) return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) end @@ -65,7 +66,7 @@ end -- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz", -- it would return the following: -- {["bla"] = true, ["tux"] = "beep", ["baz"] = true}, "foo", "bar". -function parse_flags(...) +function util.parse_flags(...) local args = {...} local flags = {} for i = #args, 1, -1 do @@ -90,7 +91,7 @@ end -- in the flags table. If no flags are passed as varargs, the -- entire flags table is forwarded. -- @return string... A variable number of strings -function forward_flags(flags, ...) +function util.forward_flags(flags, ...) assert(type(flags) == "table") local out = {} local filter = select('#', ...) @@ -119,14 +120,14 @@ end -- @param dst Destination table, which will receive src's contents. -- @param src Table which provides new contents to dst. -- @see platform_overrides -function deep_merge(dst, src) +function util.deep_merge(dst, src) for k, v in pairs(src) do if type(v) == "table" then if not dst[k] then dst[k] = {} end if type(dst[k]) == "table" then - deep_merge(dst[k], v) + util.deep_merge(dst[k], v) else dst[k] = v end @@ -149,7 +150,7 @@ end -- tbl.x are preserved). -- @param tbl table or nil: Table which may contain a "platforms" field; -- if it doesn't (or if nil is passed), this function does nothing. -function platform_overrides(tbl) +function util.platform_overrides(tbl) assert(type(tbl) == "table" or not tbl) local cfg = require("luarocks.cfg") @@ -160,7 +161,7 @@ function platform_overrides(tbl) for _, platform in ipairs(cfg.platforms) do local platform_tbl = tbl.platforms[platform] if platform_tbl then - deep_merge(tbl, platform_tbl) + util.deep_merge(tbl, platform_tbl) end end end @@ -192,7 +193,7 @@ end -- @param needed_set: a set where keys are the names of -- needed variables. -- @param msg string: the warning message to display. -function warn_if_not_used(var_defs, needed_set, msg) +function util.warn_if_not_used(var_defs, needed_set, msg) needed_set = make_shallow_copy(needed_set) for var,val in pairs(var_defs) do for used in val:gmatch(var_format_pattern) do @@ -200,7 +201,7 @@ function warn_if_not_used(var_defs, needed_set, msg) end end for var,_ in pairs(needed_set) do - warning(msg:format(var)) + util.warning(msg:format(var)) end end @@ -211,7 +212,7 @@ local function warn_failed_matches(line) local any_failed = false if line:match(var_format_pattern) then for unmatched in line:gmatch(var_format_pattern) do - warning("unmatched variable " .. unmatched) + util.warning("unmatched variable " .. unmatched) any_failed = true end end @@ -226,7 +227,7 @@ end -- @param tbl table: Table to have its string values modified. -- @param vars table: Table containing string-string key-value pairs -- representing variables to replace in the strings values of tbl. -function variable_substitutions(tbl, vars) +function util.variable_substitutions(tbl, vars) assert(type(tbl) == "table") assert(type(vars) == "table") @@ -247,7 +248,7 @@ end --- Return an array of keys of a table. -- @param tbl table: The input table. -- @return table: The array of keys. -function keys(tbl) +function util.keys(tbl) local ks = {} for k,_ in pairs(tbl) do table.insert(ks, k) @@ -275,7 +276,7 @@ end -- to be used by table.sort when sorting keys. -- @see sortedpairs local function sortedpairs_iterator(tbl, sort_function) - local ks = keys(tbl) + local ks = util.keys(tbl) if not sort_function or type(sort_function) == "function" then table.sort(ks, sort_function or default_sort) for _, k in ipairs(ks) do @@ -313,11 +314,11 @@ end -- is a string representing the field name, and the second element is a priority table -- for that key. -- @return function: the iterator function. -function sortedpairs(tbl, sort_function) +function util.sortedpairs(tbl, sort_function) return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) end -function lua_versions() +function util.lua_versions() local versions = { "5.1", "5.2" } local i = 0 return function() @@ -326,37 +327,37 @@ function lua_versions() end end -function starts_with(s, prefix) +function util.starts_with(s, prefix) return s:sub(1,#prefix) == prefix end --- Print a line to standard output -function printout(...) +function util.printout(...) io.stdout:write(table.concat({...},"\t")) io.stdout:write("\n") end --- Print a line to standard error -function printerr(...) +function util.printerr(...) io.stderr:write(table.concat({...},"\t")) io.stderr:write("\n") end --- Display a warning message. -- @param msg string: the warning message -function warning(msg) - printerr("Warning: "..msg) +function util.warning(msg) + util.printerr("Warning: "..msg) end -function title(msg, porcelain, underline) +function util.title(msg, porcelain, underline) if porcelain then return end - printout() - printout(msg) - printout((underline or "-"):rep(#msg)) - printout() + util.printout() + util.printout(msg) + util.printout((underline or "-"):rep(#msg)) + util.printout() end -function this_program(default) +function util.this_program(default) local i = 1 local last, cur = default, default while i do @@ -369,7 +370,7 @@ function this_program(default) return last:sub(2) end -function deps_mode_help(program) +function util.deps_mode_help(program) local cfg = require("luarocks.cfg") return [[ --deps-mode= How to handle dependencies. Four modes are supported: @@ -383,18 +384,18 @@ function deps_mode_help(program) The default mode may be set with the deps_mode entry in the configuration file. The current default is "]]..cfg.deps_mode..[[". - Type ']]..this_program(program or "luarocks")..[[' with no arguments to see + Type ']]..util.this_program(program or "luarocks")..[[' with no arguments to see your list of rocks trees. ]] end -function see_help(command, program) - return "See '"..this_program(program or "luarocks")..' help '..command.."'." +function util.see_help(command, program) + return "See '"..util.this_program(program or "luarocks")..' help '..command.."'." end -- from http://lua-users.org/wiki/SplitJoin -- by PhilippeLhoste -function split_string(str, delim, maxNb) +function util.split_string(str, delim, maxNb) -- Eliminate bad cases... if string.find(str, delim) == nil then return { str } @@ -423,10 +424,10 @@ end -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". -- @param list string: A path string (from $PATH or package.path) -- @param sep string: The separator -function remove_path_dupes(list, sep) +function util.remove_path_dupes(list, sep) assert(type(list) == "string") assert(type(sep) == "string") - local parts = split_string(list, sep) + local parts = util.split_string(list, sep) local final, entries = {}, {} for _, part in ipairs(parts) do if not entries[part] then @@ -454,7 +455,7 @@ end -- @param name string: is the name of the table (optional) -- @param indent string: is a first indentation (optional). -- @return string: the pretty-printed table -function show_table(t, name, indent) +function util.show_table(t, name, indent) local cart -- a container local autoref -- for self references @@ -520,7 +521,7 @@ function show_table(t, name, indent) return cart .. autoref end -function array_contains(tbl, value) +function util.array_contains(tbl, value) for _, v in ipairs(tbl) do if v == value then return true @@ -532,6 +533,8 @@ end -- Quote Lua string, analogous to fs.Q. -- @param s A string, such as "hello" -- @return string: A quoted string, such as '"hello"' -function LQ(s) +function util.LQ(s) return ("%q"):format(s) end + +return util -- cgit v1.2.3-55-g6feb