From 14f42909edf4fd79a058484fc47013a3a22f6ab4 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 26 Aug 2011 19:35:08 -0300 Subject: simplify two functions into one. --- src/luarocks/install.lua | 2 +- src/luarocks/path.lua | 26 +++++++++++--------------- src/luarocks/search.lua | 5 +---- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index d7d87192..e99b4ce0 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -28,7 +28,7 @@ or a filename of a locally available rock. function install_binary_rock(rock_file) assert(type(rock_file) == "string") - local name, version, arch = path.parse_rock_name(rock_file) + local name, version, arch = path.parse_name(rock_file) if not name then return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." end diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index fe241aa5..219164ea 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -193,22 +193,18 @@ function bin_dir(name, version, repo) return dir.path(rocks_dir(repo), name, version, "bin") end ---- Extract name, version and arch of a rock filename. --- @param rock_file string: pathname of a rock +--- Extract name, version and arch of a rock filename, +-- or name, version and "rockspec" from a rockspec name. +-- @param file_name string: pathname of a rock or rockspec -- @return (string, string, string) or nil: name, version and arch --- of rock, or nil if name could not be parsed -function parse_rock_name(rock_file) - assert(type(rock_file) == "string") - return dir.base_name(rock_file):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") -end - ---- Extract name and version of a rockspec filename. --- @param rockspec_file string: pathname of a rockspec --- @return (string, string) or nil: name and version --- of rockspec, or nil if name could not be parsed -function parse_rockspec_name(rockspec_file) - assert(type(rockspec_file) == "string") - return dir.base_name(rockspec_file):match("(.*)-([^-]+-%d+)%.(rockspec)") +-- or nil if name could not be parsed +function parse_name(file_name) + assert(type(file_name) == "string") + if file_name:match("%.rock$") then + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") + else + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") + end end --- Make a rockspec or rock URL. diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 493c2f45..127bba19 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -138,10 +138,7 @@ function disk_search(repo, query, results) for _, name in pairs(fs.list_dir(repo)) do local pathname = dir.path(repo, name) - local rname, rversion, rarch = path.parse_rock_name(name) - if not rname then - rname, rversion, rarch = path.parse_rockspec_name(name) - end + local rname, rversion, rarch = path.parse_name(name) if fs.is_dir(pathname) then for _, version in pairs(fs.list_dir(pathname)) do if version:match("-%d+$") then -- cgit v1.2.3-55-g6feb