diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-25 11:09:15 +0300 |
---|---|---|
committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-25 11:10:12 +0300 |
commit | f41e1da03b2c2aaf4c145ff83d10dfa07c2395ca (patch) | |
tree | 53cab008636ed7157b8263b5aa0cdadfef3c18bd | |
parent | 4c12525d1f52bccc71c2a3054d3db328a0577179 (diff) | |
download | luarocks-f41e1da03b2c2aaf4c145ff83d10dfa07c2395ca.tar.gz luarocks-f41e1da03b2c2aaf4c145ff83d10dfa07c2395ca.tar.bz2 luarocks-f41e1da03b2c2aaf4c145ff83d10dfa07c2395ca.zip |
luarocks make: move rockspec picking into an util function
-rw-r--r-- | src/luarocks/make.lua | 59 | ||||
-rw-r--r-- | src/luarocks/util.lua | 65 |
2 files changed, 69 insertions, 55 deletions
diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index 78c43d91..5058da79 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua | |||
@@ -49,33 +49,6 @@ To install rocks, you'll normally want to use the "install" and | |||
49 | 49 | ||
50 | ]] | 50 | ]] |
51 | 51 | ||
52 | --- Collect rockspecs located in a subdirectory. | ||
53 | -- @param versions table: A table mapping rock names to newest rockspec versions. | ||
54 | -- @param paths table: A table mapping rock names to newest rockspec paths. | ||
55 | -- @param unnamed_paths table: An array of rockspec paths that don't contain rock | ||
56 | -- name and version in regular format. | ||
57 | -- @param subdir string: path to subdirectory. | ||
58 | local function collect_rockspecs(versions, paths, unnamed_paths, subdir) | ||
59 | if fs.is_dir(subdir) then | ||
60 | for file in fs.dir(subdir) do | ||
61 | file = dir.path(subdir, file) | ||
62 | |||
63 | if file:match("rockspec$") and fs.is_file(file) then | ||
64 | local rock, version = path.parse_name(file) | ||
65 | |||
66 | if rock then | ||
67 | if not versions[rock] or deps.compare_versions(version, versions[rock]) then | ||
68 | versions[rock] = version | ||
69 | paths[rock] = file | ||
70 | end | ||
71 | else | ||
72 | table.insert(unnamed_paths, file) | ||
73 | end | ||
74 | end | ||
75 | end | ||
76 | end | ||
77 | end | ||
78 | |||
79 | --- Driver function for "make" command. | 52 | --- Driver function for "make" command. |
80 | -- @param name string: A local rockspec. | 53 | -- @param name string: A local rockspec. |
81 | -- @return boolean or (nil, string, exitcode): True if build was successful; nil and an | 54 | -- @return boolean or (nil, string, exitcode): True if build was successful; nil and an |
@@ -85,34 +58,10 @@ function make.run(...) | |||
85 | assert(type(rockspec) == "string" or not rockspec) | 58 | assert(type(rockspec) == "string" or not rockspec) |
86 | 59 | ||
87 | if not rockspec then | 60 | if not rockspec then |
88 | -- Try to infer default rockspec name. | 61 | local err |
89 | local versions, paths, unnamed_paths = {}, {}, {} | 62 | rockspec, err = util.get_default_rockspec() |
90 | -- Look for rockspecs in some common locations. | 63 | if not rockspec then |
91 | collect_rockspecs(versions, paths, unnamed_paths, ".") | 64 | return nil, err |
92 | collect_rockspecs(versions, paths, unnamed_paths, "rockspec") | ||
93 | collect_rockspecs(versions, paths, unnamed_paths, "rockspecs") | ||
94 | |||
95 | if #unnamed_paths > 0 then | ||
96 | -- There are rockspecs not following "name-version.rockspec" format. | ||
97 | -- More than one are ambiguous. | ||
98 | if #unnamed_paths > 1 then | ||
99 | return nil, "Please specify which rockspec file to use." | ||
100 | else | ||
101 | rockspec = unnamed_paths[1] | ||
102 | end | ||
103 | else | ||
104 | local rock = next(versions) | ||
105 | |||
106 | if rock then | ||
107 | -- If there are rockspecs for multiple rocks it's ambiguous. | ||
108 | if next(versions, rock) then | ||
109 | return nil, "Please specify which rockspec file to use." | ||
110 | else | ||
111 | rockspec = paths[rock] | ||
112 | end | ||
113 | else | ||
114 | return nil, "Argument missing: please specify a rockspec to use on current directory." | ||
115 | end | ||
116 | end | 65 | end |
117 | end | 66 | end |
118 | if not rockspec:match("rockspec$") then | 67 | if not rockspec:match("rockspec$") then |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 520625c0..2f419a3d 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -520,6 +520,71 @@ function util.announce_install(rockspec) | |||
520 | util.printout() | 520 | util.printout() |
521 | end | 521 | end |
522 | 522 | ||
523 | --- Collect rockspecs located in a subdirectory. | ||
524 | -- @param versions table: A table mapping rock names to newest rockspec versions. | ||
525 | -- @param paths table: A table mapping rock names to newest rockspec paths. | ||
526 | -- @param unnamed_paths table: An array of rockspec paths that don't contain rock | ||
527 | -- name and version in regular format. | ||
528 | -- @param subdir string: path to subdirectory. | ||
529 | local function collect_rockspecs(versions, paths, unnamed_paths, subdir) | ||
530 | local fs = require("luarocks.fs") | ||
531 | local dir = require("luarocks.dir") | ||
532 | local path = require("luarocks.path") | ||
533 | local deps = require("luarocks.deps") | ||
534 | |||
535 | if fs.is_dir(subdir) then | ||
536 | for file in fs.dir(subdir) do | ||
537 | file = dir.path(subdir, file) | ||
538 | |||
539 | if file:match("rockspec$") and fs.is_file(file) then | ||
540 | local rock, version = path.parse_name(file) | ||
541 | |||
542 | if rock then | ||
543 | if not versions[rock] or deps.compare_versions(version, versions[rock]) then | ||
544 | versions[rock] = version | ||
545 | paths[rock] = file | ||
546 | end | ||
547 | else | ||
548 | table.insert(unnamed_paths, file) | ||
549 | end | ||
550 | end | ||
551 | end | ||
552 | end | ||
553 | end | ||
554 | |||
555 | --- Get default rockspec name for commands that take optional rockspec name. | ||
556 | -- @return string or (nil, string): path to the rockspec or nil and error message. | ||
557 | function util.get_default_rockspec() | ||
558 | local versions, paths, unnamed_paths = {}, {}, {} | ||
559 | -- Look for rockspecs in some common locations. | ||
560 | collect_rockspecs(versions, paths, unnamed_paths, ".") | ||
561 | collect_rockspecs(versions, paths, unnamed_paths, "rockspec") | ||
562 | collect_rockspecs(versions, paths, unnamed_paths, "rockspecs") | ||
563 | |||
564 | if #unnamed_paths > 0 then | ||
565 | -- There are rockspecs not following "name-version.rockspec" format. | ||
566 | -- More than one are ambiguous. | ||
567 | if #unnamed_paths > 1 then | ||
568 | return nil, "Please specify which rockspec file to use." | ||
569 | else | ||
570 | return unnamed_paths[1] | ||
571 | end | ||
572 | else | ||
573 | local rock = next(versions) | ||
574 | |||
575 | if rock then | ||
576 | -- If there are rockspecs for multiple rocks it's ambiguous. | ||
577 | if next(versions, rock) then | ||
578 | return nil, "Please specify which rockspec file to use." | ||
579 | else | ||
580 | return paths[rock] | ||
581 | end | ||
582 | else | ||
583 | return nil, "Argument missing: please specify a rockspec to use on current directory." | ||
584 | end | ||
585 | end | ||
586 | end | ||
587 | |||
523 | -- from http://lua-users.org/wiki/SplitJoin | 588 | -- from http://lua-users.org/wiki/SplitJoin |
524 | -- by PhilippeLhoste | 589 | -- by PhilippeLhoste |
525 | function util.split_string(str, delim, maxNb) | 590 | function util.split_string(str, delim, maxNb) |