diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2019-03-20 12:16:48 -0400 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-04-01 17:32:53 -0400 |
commit | b46978faaebeb5954d57b13ff786fea789ab775c (patch) | |
tree | f32cce4bae3d52dd73534344ee935c7d6731e80d | |
parent | 18842a049784cdbba66010fb30f06525e9016600 (diff) | |
download | luarocks-b46978faaebeb5954d57b13ff786fea789ab775c.tar.gz luarocks-b46978faaebeb5954d57b13ff786fea789ab775c.tar.bz2 luarocks-b46978faaebeb5954d57b13ff786fea789ab775c.zip |
Refactor opts_table from build to reuse in install
-rw-r--r-- | src/luarocks/build.lua | 42 | ||||
-rw-r--r-- | 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") | |||
12 | local repos = require("luarocks.repos") | 12 | local repos = require("luarocks.repos") |
13 | local writer = require("luarocks.manif.writer") | 13 | local writer = require("luarocks.manif.writer") |
14 | 14 | ||
15 | local opts_mt = {} | 15 | build.opts = util.opts_table("build.opts", { |
16 | 16 | need_to_fetch = "boolean", | |
17 | opts_mt.__index = opts_mt | 17 | minimal_mode = "boolean", |
18 | 18 | deps_mode = "string", | |
19 | function opts_mt.type() | 19 | build_only_deps = "boolean", |
20 | return "build.opts" | 20 | namespace = "string?", |
21 | end | 21 | branch = "boolean", |
22 | 22 | }) | |
23 | function build.opts(opts) | ||
24 | local valid_opts = { | ||
25 | need_to_fetch = "boolean", | ||
26 | minimal_mode = "boolean", | ||
27 | deps_mode = "string", | ||
28 | build_only_deps = "boolean", | ||
29 | namespace = "string?", | ||
30 | branch = "boolean", | ||
31 | } | ||
32 | for k, v in pairs(opts) do | ||
33 | local tv = type(v) | ||
34 | if not valid_opts[k] then | ||
35 | error("invalid build option: "..k) | ||
36 | end | ||
37 | local vo, optional = valid_opts[k]:match("^(.-)(%??)$") | ||
38 | if not (tv == vo or (optional == "?" and tv == nil)) then | ||
39 | error("invalid type build option: "..k.." - got "..tv..", expected "..vo) | ||
40 | end | ||
41 | end | ||
42 | for k, v in pairs(valid_opts) do | ||
43 | if (not v:find("?", 1, true)) and opts[k] == nil then | ||
44 | error("missing build option: "..k) | ||
45 | end | ||
46 | end | ||
47 | return setmetatable(opts, opts_mt) | ||
48 | end | ||
49 | 23 | ||
50 | do | 24 | do |
51 | --- Write to the current directory the contents of a table, | 25 | --- 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 | |||
657 | end | 657 | end |
658 | end | 658 | end |
659 | 659 | ||
660 | function util.opts_table(type_name, valid_opts) | ||
661 | local opts_mt = {} | ||
662 | |||
663 | opts_mt.__index = opts_mt | ||
664 | |||
665 | function opts_mt.type() | ||
666 | return type_name | ||
667 | end | ||
668 | |||
669 | return function(opts) | ||
670 | for k, v in pairs(opts) do | ||
671 | local tv = type(v) | ||
672 | if not valid_opts[k] then | ||
673 | error("invalid option: "..k) | ||
674 | end | ||
675 | local vo, optional = valid_opts[k]:match("^(.-)(%??)$") | ||
676 | if not (tv == vo or (optional == "?" and tv == nil)) then | ||
677 | error("invalid type option: "..k.." - got "..tv..", expected "..vo) | ||
678 | end | ||
679 | end | ||
680 | for k, v in pairs(valid_opts) do | ||
681 | if (not v:find("?", 1, true)) and opts[k] == nil then | ||
682 | error("missing option: "..k) | ||
683 | end | ||
684 | end | ||
685 | return setmetatable(opts, opts_mt) | ||
686 | end | ||
687 | end | ||
688 | |||
660 | return util | 689 | return util |
661 | 690 | ||