From c9b7f0bc5453251730c73040904a17a8fd32eef3 Mon Sep 17 00:00:00 2001 From: Ignacio BurgueƱo Date: Thu, 18 Jun 2015 16:29:25 -0300 Subject: Factors the code to check for a tool. Changes 'not found' message wording. Adds the function fs.is_tool_available to check if a given tool (git, hg, cmake, etc) is available. --- src/luarocks/build/cmake.lua | 5 +++-- src/luarocks/fetch/cvs.lua | 5 +++-- src/luarocks/fetch/git.lua | 5 +++-- src/luarocks/fetch/hg.lua | 5 +++-- src/luarocks/fetch/svn.lua | 5 +++-- src/luarocks/fs/lua.lua | 21 +++++++++++++++++++++ 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/luarocks/build/cmake.lua b/src/luarocks/build/cmake.lua index e5b29147..c8f5a669 100644 --- a/src/luarocks/build/cmake.lua +++ b/src/luarocks/build/cmake.lua @@ -23,8 +23,9 @@ function cmake.run(rockspec) util.variable_substitutions(variables, rockspec.variables) - if not fs.execute_quiet(rockspec.variables.CMAKE, "--help") then - return nil, "'"..rockspec.variables.CMAKE.."' program not found. Is cmake installed? You may want to edit variables.CMAKE" + local ok, err_msg = fs.is_tool_available(rockspec.variables.CMAKE, "CMake") + if not ok then + return nil, err_msg end -- If inline cmake is present create CMakeLists.txt from it. diff --git a/src/luarocks/fetch/cvs.lua b/src/luarocks/fetch/cvs.lua index fad1ca0d..ccf928c4 100644 --- a/src/luarocks/fetch/cvs.lua +++ b/src/luarocks/fetch/cvs.lua @@ -21,8 +21,9 @@ function cvs.get_sources(rockspec, extract, dest_dir) assert(type(dest_dir) == "string" or not dest_dir) local cvs_cmd = rockspec.variables.CVS - if not fs.execute_quiet(cvs_cmd, "--version") then - return nil, "'"..cvs_cmd.."' program not found. Is CVS installed? You may want to edit variables.CVS" + local ok, err_msg = fs.is_tool_available(cvs_cmd, "CVS") + if not ok then + return nil, err_msg end local name_version = rockspec.name .. "-" .. rockspec.version diff --git a/src/luarocks/fetch/git.lua b/src/luarocks/fetch/git.lua index bea53fc8..e540d696 100644 --- a/src/luarocks/fetch/git.lua +++ b/src/luarocks/fetch/git.lua @@ -40,8 +40,9 @@ function git.get_sources(rockspec, extract, dest_dir, depth) -- Strip off .git from base name if present module = module:gsub("%.git$", "") - if not fs.execute_quiet(git_cmd, "--version") then - return nil, "'"..git_cmd.."' program not found. Is git installed? You may want to edit variables.GIT" + local ok, err_msg = fs.is_tool_available(git_cmd, "Git") + if not ok then + return nil, err_msg end local store_dir diff --git a/src/luarocks/fetch/hg.lua b/src/luarocks/fetch/hg.lua index 055e3bbe..518130b4 100644 --- a/src/luarocks/fetch/hg.lua +++ b/src/luarocks/fetch/hg.lua @@ -21,8 +21,9 @@ function hg.get_sources(rockspec, extract, dest_dir) assert(type(dest_dir) == "string" or not dest_dir) local hg_cmd = rockspec.variables.HG - if not fs.execute_quiet(hg_cmd, "--version") then - return nil, "'"..hg_cmd.."' program not found. Is Mercurial installed? You may want to edit variables.HG" + local ok, err_msg = fs.is_tool_available(hg_cmd, "Mercurial") + if not ok then + return nil, err_msg end local name_version = rockspec.name .. "-" .. rockspec.version diff --git a/src/luarocks/fetch/svn.lua b/src/luarocks/fetch/svn.lua index 8fe582f6..755e5e32 100644 --- a/src/luarocks/fetch/svn.lua +++ b/src/luarocks/fetch/svn.lua @@ -21,8 +21,9 @@ function svn.get_sources(rockspec, extract, dest_dir) assert(type(dest_dir) == "string" or not dest_dir) local svn_cmd = rockspec.variables.SVN - if not fs.execute_quiet(svn_cmd, "--version") then - return nil, "'"..svn_cmd.."' program not found. Is Subversion installed? You may want to edit variables.SVN" + local ok, err_msg = fs.is_tool_available(svn_cmd, "--version", "Subversion") + if not ok then + return nil, err_msg end local name_version = rockspec.name .. "-" .. rockspec.version diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index c0f6c1c6..73ae2698 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -122,6 +122,27 @@ function fs_lua.execute_quiet(command, ...) end end +--- Checks if the given tool is available. +-- The tool is executed using a flag, usually just to ask its version. +-- @param tool_cmd string: The command to be used to check the tool's presence (e.g. hg in case of Mercurial) +-- @param tool_name string: The actual name of the tool (e.g. Mercurial) +-- @param arg string: The flag to pass to the tool. '--version' by default. +function fs_lua.is_tool_available(tool_cmd, tool_name, arg) + assert(type(tool_cmd) == "string") + assert(type(tool_name) == "string") + + arg = arg or "--version" + assert(type(arg) == "string") + + if not fs.execute_quiet(tool_cmd, arg) then + local msg = "'%s' program not found. Make sure %s is installed and is available in your PATH " .. + "(or you may want to edit the 'variables.%s' value in file 'config.lua')" + return nil, msg:format(tool_cmd, tool_name, tool_cmd:upper()) + else + return true + end +end + --- Check the MD5 checksum for a file. -- @param file string: The file to be checked. -- @param md5sum string: The string with the expected MD5 checksum. -- cgit v1.2.3-55-g6feb