diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2017-10-05 12:46:42 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2017-10-05 12:56:35 -0300 |
commit | d993464422e850689f73a6f7c35ae980ca3823d0 (patch) | |
tree | 11e07b379d6a299a1fb3d83756775596286ac947 | |
parent | ff73ca923833900ff7ebb3e98b3ab838214ca32c (diff) | |
download | luarocks-d993464422e850689f73a6f7c35ae980ca3823d0.tar.gz luarocks-d993464422e850689f73a6f7c35ae980ca3823d0.tar.bz2 luarocks-d993464422e850689f73a6f7c35ae980ca3823d0.zip |
luarocks.fun: a basic set of functional utilities
Move `array_contains` away from the `luarocks.util` kitchen sink,
and add a `luarocks.fun` module with some basic functional utilities.
I considered using luafun instead, but at this point the basic
functionality of LuaRocks has no mandatory external dependencies,
so I thought I'd just add these functions internally instead.
-rw-r--r-- | src/luarocks/build.lua | 3 | ||||
-rw-r--r-- | src/luarocks/fun.lua | 40 | ||||
-rw-r--r-- | src/luarocks/tools/patch.lua | 10 | ||||
-rw-r--r-- | src/luarocks/util.lua | 9 |
4 files changed, 47 insertions, 15 deletions
diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 860cbdc1..74135379 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua | |||
@@ -3,6 +3,7 @@ local build = {} | |||
3 | 3 | ||
4 | local path = require("luarocks.path") | 4 | local path = require("luarocks.path") |
5 | local util = require("luarocks.util") | 5 | local util = require("luarocks.util") |
6 | local fun = require("luarocks.fun") | ||
6 | local fetch = require("luarocks.fetch") | 7 | local fetch = require("luarocks.fetch") |
7 | local fs = require("luarocks.fs") | 8 | local fs = require("luarocks.fs") |
8 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
@@ -254,7 +255,7 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m | |||
254 | build_spec.type = "builtin" | 255 | build_spec.type = "builtin" |
255 | end | 256 | end |
256 | 257 | ||
257 | if cfg.accepted_build_types and util.array_contains(cfg.accepted_build_types, build_spec.type) then | 258 | if cfg.accepted_build_types and fun.contains(cfg.accepted_build_types, build_spec.type) then |
258 | return nil, "This rockspec uses the '"..build_spec.type.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration." | 259 | return nil, "This rockspec uses the '"..build_spec.type.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration." |
259 | end | 260 | end |
260 | 261 | ||
diff --git a/src/luarocks/fun.lua b/src/luarocks/fun.lua new file mode 100644 index 00000000..e8e0c6bb --- /dev/null +++ b/src/luarocks/fun.lua | |||
@@ -0,0 +1,40 @@ | |||
1 | |||
2 | --- A set of basic functional utilities | ||
3 | local fun = {} | ||
4 | |||
5 | function fun.concat(xs, ys) | ||
6 | local rs = {} | ||
7 | local n = #xs | ||
8 | for i = 1, n do | ||
9 | rs[i] = xs[i] | ||
10 | end | ||
11 | for i = 1, #ys do | ||
12 | rs[i + n] = ys[i] | ||
13 | end | ||
14 | return rs | ||
15 | end | ||
16 | |||
17 | function fun.contains(xs, v) | ||
18 | for _, x in ipairs(xs) do | ||
19 | if v == x then | ||
20 | return true | ||
21 | end | ||
22 | end | ||
23 | return false | ||
24 | end | ||
25 | |||
26 | function fun.map(f, xs) | ||
27 | local rs = {} | ||
28 | for i = 1, #xs do | ||
29 | rs[i] = f(xs[i]) | ||
30 | end | ||
31 | return rs | ||
32 | end | ||
33 | |||
34 | function fun.traverse(f, t) | ||
35 | return fun.map(function(x) | ||
36 | return type(x) == "table" and fun.traverse(f, x) or f(x) | ||
37 | end, t) | ||
38 | end | ||
39 | |||
40 | return fun | ||
diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index 30ed1419..2e95e879 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua | |||
@@ -11,7 +11,7 @@ | |||
11 | local patch = {} | 11 | local patch = {} |
12 | 12 | ||
13 | local fs = require("luarocks.fs") | 13 | local fs = require("luarocks.fs") |
14 | local util = require("luarocks.util") | 14 | local fun = require("luarocks.fun") |
15 | 15 | ||
16 | local io = io | 16 | local io = io |
17 | local os = os | 17 | local os = os |
@@ -284,7 +284,7 @@ function patch.read_patch(filename, data) | |||
284 | local advance | 284 | local advance |
285 | if state == 'filenames' then | 285 | if state == 'filenames' then |
286 | if startswith(line, "--- ") then | 286 | if startswith(line, "--- ") then |
287 | if util.array_contains(files.source, nextfileno) then | 287 | if fun.contains(files.source, nextfileno) then |
288 | all_ok = false | 288 | all_ok = false |
289 | warning(format("skipping invalid patch for %s", | 289 | warning(format("skipping invalid patch for %s", |
290 | files.source[nextfileno+1])) | 290 | files.source[nextfileno+1])) |
@@ -307,7 +307,7 @@ function patch.read_patch(filename, data) | |||
307 | table.insert(files.source, match) | 307 | table.insert(files.source, match) |
308 | end | 308 | end |
309 | elseif not startswith(line, "+++ ") then | 309 | elseif not startswith(line, "+++ ") then |
310 | if util.array_contains(files.source, nextfileno) then | 310 | if fun.contains(files.source, nextfileno) then |
311 | all_ok = false | 311 | all_ok = false |
312 | warning(format("skipping invalid patch with no target for %s", | 312 | warning(format("skipping invalid patch with no target for %s", |
313 | files.source[nextfileno+1])) | 313 | files.source[nextfileno+1])) |
@@ -318,7 +318,7 @@ function patch.read_patch(filename, data) | |||
318 | end | 318 | end |
319 | state = 'header' | 319 | state = 'header' |
320 | else | 320 | else |
321 | if util.array_contains(files.target, nextfileno) then | 321 | if fun.contains(files.target, nextfileno) then |
322 | all_ok = false | 322 | all_ok = false |
323 | warning(format("skipping invalid patch - double target at line %d", | 323 | warning(format("skipping invalid patch - double target at line %d", |
324 | lineno+1)) | 324 | lineno+1)) |
@@ -360,7 +360,7 @@ function patch.read_patch(filename, data) | |||
360 | if not advance and state == 'hunkhead' then | 360 | if not advance and state == 'hunkhead' then |
361 | local m1, m2, m3, m4 = match_linerange(line) | 361 | local m1, m2, m3, m4 = match_linerange(line) |
362 | if not m1 then | 362 | if not m1 then |
363 | if not util.array_contains(files.hunks, nextfileno-1) then | 363 | if not fun.contains(files.hunks, nextfileno-1) then |
364 | all_ok = false | 364 | all_ok = false |
365 | warning(format("skipping invalid patch with no hunks for file %s", | 365 | warning(format("skipping invalid patch with no hunks for file %s", |
366 | files.target[nextfileno])) | 366 | files.target[nextfileno])) |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 2c4724f4..59173449 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -442,15 +442,6 @@ function util.get_default_rockspec() | |||
442 | end | 442 | end |
443 | end | 443 | end |
444 | 444 | ||
445 | function util.array_contains(tbl, value) | ||
446 | for _, v in ipairs(tbl) do | ||
447 | if v == value then | ||
448 | return true | ||
449 | end | ||
450 | end | ||
451 | return false | ||
452 | end | ||
453 | |||
454 | -- Quote Lua string, analogous to fs.Q. | 445 | -- Quote Lua string, analogous to fs.Q. |
455 | -- @param s A string, such as "hello" | 446 | -- @param s A string, such as "hello" |
456 | -- @return string: A quoted string, such as '"hello"' | 447 | -- @return string: A quoted string, such as '"hello"' |