From fba7100c8217edec2cd41b5d8620514b274d0afd Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 5 Jun 2019 12:25:02 -0300 Subject: Always assume that zip and unzip are available on FreeBSD Fixes #1022 --- src/luarocks/fs.lua | 18 ++++++++++++++---- src/luarocks/fs/freebsd.lua | 11 +++++++++++ src/luarocks/fs/lua.lua | 21 ++++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/luarocks/fs/freebsd.lua (limited to 'src') diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 8122525e..849ee4d3 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua @@ -49,12 +49,15 @@ do end do - local function load_fns(fs_table) + local function load_fns(fs_table, inits) for name, fn in pairs(fs_table) do if not fs[name] then fs[name] = fn end end + if fs_table.init then + table.insert(inits, fs_table.init) + end end function fs.init() @@ -67,27 +70,34 @@ do error("cfg is not initialized, please run cfg.init() first") end + local inits = {} + -- Load platform-specific functions local loaded_platform = nil for platform in cfg.each_platform() do local ok, fs_plat = pcall(require, "luarocks.fs."..platform) if ok and fs_plat then loaded_platform = platform - load_fns(fs_plat) + load_fns(fs_plat, inits) break end end -- Load platform-independent pure-Lua functionality local fs_lua = require("luarocks.fs.lua") - load_fns(fs_lua) + load_fns(fs_lua, inits) -- Load platform-specific fallbacks for missing Lua modules local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") if ok and fs_plat_tools then - load_fns(fs_plat_tools) + load_fns(fs_plat_tools, inits) load_fns(require("luarocks.fs.tools")) end + + -- Run platform-specific initializations after everything is loaded + for _, init in ipairs(inits) do + init() + end end end diff --git a/src/luarocks/fs/freebsd.lua b/src/luarocks/fs/freebsd.lua new file mode 100644 index 00000000..f72faa29 --- /dev/null +++ b/src/luarocks/fs/freebsd.lua @@ -0,0 +1,11 @@ +--- FreeBSD implementation of filesystem and platform abstractions. +local freebsd = {} + +local fs = require("luarocks.fs") + +function freebsd.init() + fs.set_tool_available("zip", true) + fs.set_tool_available("unzip", true) +end + +return freebsd diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 22bffce0..b589654f 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -99,6 +99,13 @@ function fs.execute_env(env, command, ...) return fs.execute_string(table.concat(envstr, "\n") .. "\n" .. quote_args(command, ...)) end +local tool_available_cache = {} + +function fs_lua.set_tool_available(tool_name, value) + assert(type(value) == "boolean") + tool_available_cache[tool_name] = value +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) @@ -111,12 +118,20 @@ function fs_lua.is_tool_available(tool_cmd, tool_name, arg) arg = arg or "--version" assert(type(arg) == "string") - if not fs.execute_quiet(tool_cmd, arg) then + local ok + if tool_available_cache[tool_name] ~= nil then + ok = tool_available_cache[tool_name] + else + ok = fs.execute_quiet(tool_cmd, arg) + tool_available_cache[tool_name] = (ok == true) + end + + if ok then + return true + else 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 '%s')" return nil, msg:format(tool_cmd, tool_name, tool_name:upper(), cfg.config_files.nearest) - else - return true end end -- cgit v1.2.3-55-g6feb