From b46978faaebeb5954d57b13ff786fea789ab775c Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 20 Mar 2019 12:16:48 -0400 Subject: Refactor opts_table from build to reuse in install --- src/luarocks/build.lua | 42 ++++++++---------------------------------- src/luarocks/util.lua | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 2e9464cf..130ccd3a 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -12,40 +12,14 @@ local cfg = require("luarocks.core.cfg") local repos = require("luarocks.repos") local writer = require("luarocks.manif.writer") -local opts_mt = {} - -opts_mt.__index = opts_mt - -function opts_mt.type() - return "build.opts" -end - -function build.opts(opts) - local valid_opts = { - need_to_fetch = "boolean", - minimal_mode = "boolean", - deps_mode = "string", - build_only_deps = "boolean", - namespace = "string?", - branch = "boolean", - } - for k, v in pairs(opts) do - local tv = type(v) - if not valid_opts[k] then - error("invalid build option: "..k) - end - local vo, optional = valid_opts[k]:match("^(.-)(%??)$") - if not (tv == vo or (optional == "?" and tv == nil)) then - error("invalid type build option: "..k.." - got "..tv..", expected "..vo) - end - end - for k, v in pairs(valid_opts) do - if (not v:find("?", 1, true)) and opts[k] == nil then - error("missing build option: "..k) - end - end - return setmetatable(opts, opts_mt) -end +build.opts = util.opts_table("build.opts", { + need_to_fetch = "boolean", + minimal_mode = "boolean", + deps_mode = "string", + build_only_deps = "boolean", + namespace = "string?", + branch = "boolean", +}) do --- Write to the current directory the contents of a table, diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index bba39457..e370e688 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -657,5 +657,34 @@ do end end +function util.opts_table(type_name, valid_opts) + local opts_mt = {} + + opts_mt.__index = opts_mt + + function opts_mt.type() + return type_name + end + + return function(opts) + for k, v in pairs(opts) do + local tv = type(v) + if not valid_opts[k] then + error("invalid option: "..k) + end + local vo, optional = valid_opts[k]:match("^(.-)(%??)$") + if not (tv == vo or (optional == "?" and tv == nil)) then + error("invalid type option: "..k.." - got "..tv..", expected "..vo) + end + end + for k, v in pairs(valid_opts) do + if (not v:find("?", 1, true)) and opts[k] == nil then + error("missing option: "..k) + end + end + return setmetatable(opts, opts_mt) + end +end + return util -- cgit v1.2.3-55-g6feb