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 From 22c8993ebe5f509ee55b8af6b7a8be147b3884e6 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 25 May 2016 11:17:14 +0300 Subject: luarocks new-version: make rockspec arg optional Infer it same way `luarocks make` does. --- src/luarocks/new_version.lua | 13 +++++++++---- test/testing.sh | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua index 6969d4b2..3382b85c 100644 --- a/src/luarocks/new_version.lua +++ b/src/luarocks/new_version.lua @@ -11,13 +11,14 @@ local fs = require("luarocks.fs") local type_check = require("luarocks.type_check") new_version.help_summary = "Auto-write a rockspec for a new version of a rock." -new_version.help_arguments = "[--tag=] {|} [] []" +new_version.help_arguments = "[--tag=] [|] [] []" new_version.help = [[ This is a utility function that writes a new rockspec, updating data from a previous one. If a package name is given, it downloads the latest rockspec from the -default server. If a rockspec is given, it uses it instead. +default server. If a rockspec is given, it uses it instead. If no argument +is given, it looks for a rockspec same way 'luarocks make' does. If the version number is not given and tag is passed using --tag, it is used as the version, with 'v' removed from beginning. @@ -125,12 +126,16 @@ end function new_version.run(...) local flags, input, version, url = util.parse_flags(...) if not input then - return nil, "Missing argument: expected package or rockspec. "..util.see_help("new_version") + local err + input, err = util.get_default_rockspec() + if not input then + return nil, err + end end assert(type(input) == "string") local filename = input - if not input:match(".rockspec$") then + if not input:match("rockspec$") then local err filename, err = download.download("rockspec", input) if not filename then diff --git a/test/testing.sh b/test/testing.sh index f56f2aa9..cb10441c 100755 --- a/test/testing.sh +++ b/test/testing.sh @@ -363,7 +363,6 @@ fail_unpack_noarg() { $luarocks unpack; } fail_upload_noarg() { $luarocks upload; } fail_remove_noarg() { $luarocks remove; } fail_doc_noarg() { $luarocks doc; } -fail_new_version_noarg() { $luarocks new_version; } fail_build_invalid() { $luarocks build invalid; } fail_download_invalid() { $luarocks download invalid; } @@ -446,6 +445,7 @@ test_make_pack_binary_rock() { rm -rf ./lxsh-${verrev_lxsh} && $luarocks downlo test_new_version() { $luarocks download --rockspec luacov ${version_luacov} && $luarocks new_version ./luacov-${version_luacov}-1.rockspec 0.2 && rm ./luacov-0.*; } test_new_version_url() { $luarocks download --rockspec abelhas 1.0 && $luarocks new_version ./abelhas-1.0-1.rockspec 1.1 https://github.com/downloads/ittner/abelhas/abelhas-1.1.tar.gz && rm ./abelhas-*; } test_new_version_tag() { $luarocks download --rockspec luacov ${version_luacov} && $luarocks new_version ./luacov-${version_luacov}-1.rockspec --tag v0.3 && rm ./luacov-0.3-1.rockspec; } +test_new_version_tag_without_arg() { rm -rf ./*rockspec && $luarocks download --rockspec luacov ${version_luacov} && $luarocks new_version --tag v0.3 && rm ./luacov-0.3-1.rockspec; } test_pack() { $luarocks list && $luarocks pack luacov && rm ./luacov-*.rock; } test_pack_src() { $luarocks install $luasec && $luarocks download --rockspec luasocket && $luarocks pack ./luasocket-${verrev_luasocket}.rockspec && rm ./luasocket-${version_luasocket}-*.rock; } -- cgit v1.2.3-55-g6feb