From f41e1da03b2c2aaf4c145ff83d10dfa07c2395ca Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 25 May 2016 11:09:15 +0300 Subject: luarocks make: move rockspec picking into an util function --- src/luarocks/make.lua | 59 ++++------------------------------------------ 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 ]] ---- Collect rockspecs located in a subdirectory. --- @param versions table: A table mapping rock names to newest rockspec versions. --- @param paths table: A table mapping rock names to newest rockspec paths. --- @param unnamed_paths table: An array of rockspec paths that don't contain rock --- name and version in regular format. --- @param subdir string: path to subdirectory. -local function collect_rockspecs(versions, paths, unnamed_paths, subdir) - if fs.is_dir(subdir) then - for file in fs.dir(subdir) do - file = dir.path(subdir, file) - - if file:match("rockspec$") and fs.is_file(file) then - local rock, version = path.parse_name(file) - - if rock then - if not versions[rock] or deps.compare_versions(version, versions[rock]) then - versions[rock] = version - paths[rock] = file - end - else - table.insert(unnamed_paths, file) - end - end - end - end -end - --- Driver function for "make" command. -- @param name string: A local rockspec. -- @return boolean or (nil, string, exitcode): True if build was successful; nil and an @@ -85,34 +58,10 @@ function make.run(...) assert(type(rockspec) == "string" or not rockspec) if not rockspec then - -- Try to infer default rockspec name. - local versions, paths, unnamed_paths = {}, {}, {} - -- Look for rockspecs in some common locations. - collect_rockspecs(versions, paths, unnamed_paths, ".") - collect_rockspecs(versions, paths, unnamed_paths, "rockspec") - collect_rockspecs(versions, paths, unnamed_paths, "rockspecs") - - if #unnamed_paths > 0 then - -- There are rockspecs not following "name-version.rockspec" format. - -- More than one are ambiguous. - if #unnamed_paths > 1 then - return nil, "Please specify which rockspec file to use." - else - rockspec = unnamed_paths[1] - end - else - local rock = next(versions) - - if rock then - -- If there are rockspecs for multiple rocks it's ambiguous. - if next(versions, rock) then - return nil, "Please specify which rockspec file to use." - else - rockspec = paths[rock] - end - else - return nil, "Argument missing: please specify a rockspec to use on current directory." - end + local err + rockspec, err = util.get_default_rockspec() + if not rockspec then + return nil, err end end 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) util.printout() end +--- Collect rockspecs located in a subdirectory. +-- @param versions table: A table mapping rock names to newest rockspec versions. +-- @param paths table: A table mapping rock names to newest rockspec paths. +-- @param unnamed_paths table: An array of rockspec paths that don't contain rock +-- name and version in regular format. +-- @param subdir string: path to subdirectory. +local function collect_rockspecs(versions, paths, unnamed_paths, subdir) + local fs = require("luarocks.fs") + local dir = require("luarocks.dir") + local path = require("luarocks.path") + local deps = require("luarocks.deps") + + if fs.is_dir(subdir) then + for file in fs.dir(subdir) do + file = dir.path(subdir, file) + + if file:match("rockspec$") and fs.is_file(file) then + local rock, version = path.parse_name(file) + + if rock then + if not versions[rock] or deps.compare_versions(version, versions[rock]) then + versions[rock] = version + paths[rock] = file + end + else + table.insert(unnamed_paths, file) + end + end + end + end +end + +--- Get default rockspec name for commands that take optional rockspec name. +-- @return string or (nil, string): path to the rockspec or nil and error message. +function util.get_default_rockspec() + local versions, paths, unnamed_paths = {}, {}, {} + -- Look for rockspecs in some common locations. + collect_rockspecs(versions, paths, unnamed_paths, ".") + collect_rockspecs(versions, paths, unnamed_paths, "rockspec") + collect_rockspecs(versions, paths, unnamed_paths, "rockspecs") + + if #unnamed_paths > 0 then + -- There are rockspecs not following "name-version.rockspec" format. + -- More than one are ambiguous. + if #unnamed_paths > 1 then + return nil, "Please specify which rockspec file to use." + else + return unnamed_paths[1] + end + else + local rock = next(versions) + + if rock then + -- If there are rockspecs for multiple rocks it's ambiguous. + if next(versions, rock) then + return nil, "Please specify which rockspec file to use." + else + return paths[rock] + end + else + return nil, "Argument missing: please specify a rockspec to use on current directory." + end + end +end + -- from http://lua-users.org/wiki/SplitJoin -- by PhilippeLhoste function util.split_string(str, delim, maxNb) -- cgit v1.2.3-55-g6feb