diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2022-04-11 17:00:43 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2022-04-11 17:21:14 -0300 |
| commit | 8ee4ef81cf015adca29ac51a7d649a5ee04b3edd (patch) | |
| tree | c0c28f9aac86f16a4294665dfac702aa4612f1a9 | |
| parent | eddf776d64941454377f996733f96873cb79bcae (diff) | |
| download | luarocks-8ee4ef81cf015adca29ac51a7d649a5ee04b3edd.tar.gz luarocks-8ee4ef81cf015adca29ac51a7d649a5ee04b3edd.tar.bz2 luarocks-8ee4ef81cf015adca29ac51a7d649a5ee04b3edd.zip | |
simplify check if tool is available
| -rw-r--r-- | src/luarocks/fetch/svn.lua | 2 | ||||
| -rw-r--r-- | src/luarocks/fs/freebsd.lua | 11 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 9 | ||||
| -rw-r--r-- | src/luarocks/fs/netbsd.lua | 14 | ||||
| -rw-r--r-- | src/luarocks/fs/tools.lua | 6 | ||||
| -rw-r--r-- | src/luarocks/fs/unix.lua | 11 | ||||
| -rw-r--r-- | src/luarocks/fs/unix/tools.lua | 2 | ||||
| -rw-r--r-- | src/luarocks/fs/win32.lua | 11 |
8 files changed, 30 insertions, 36 deletions
diff --git a/src/luarocks/fetch/svn.lua b/src/luarocks/fetch/svn.lua index b2ae59c9..b6618afc 100644 --- a/src/luarocks/fetch/svn.lua +++ b/src/luarocks/fetch/svn.lua | |||
| @@ -20,7 +20,7 @@ function svn.get_sources(rockspec, extract, dest_dir) | |||
| 20 | assert(type(dest_dir) == "string" or not dest_dir) | 20 | assert(type(dest_dir) == "string" or not dest_dir) |
| 21 | 21 | ||
| 22 | local svn_cmd = rockspec.variables.SVN | 22 | local svn_cmd = rockspec.variables.SVN |
| 23 | local ok, err_msg = fs.is_tool_available(svn_cmd, "Subversion", "--version") | 23 | local ok, err_msg = fs.is_tool_available(svn_cmd, "Subversion") |
| 24 | if not ok then | 24 | if not ok then |
| 25 | return nil, err_msg | 25 | return nil, err_msg |
| 26 | end | 26 | end |
diff --git a/src/luarocks/fs/freebsd.lua b/src/luarocks/fs/freebsd.lua deleted file mode 100644 index f72faa29..00000000 --- a/src/luarocks/fs/freebsd.lua +++ /dev/null | |||
| @@ -1,11 +0,0 @@ | |||
| 1 | --- FreeBSD implementation of filesystem and platform abstractions. | ||
| 2 | local freebsd = {} | ||
| 3 | |||
| 4 | local fs = require("luarocks.fs") | ||
| 5 | |||
| 6 | function freebsd.init() | ||
| 7 | fs.set_tool_available("zip", true) | ||
| 8 | fs.set_tool_available("unzip", true) | ||
| 9 | end | ||
| 10 | |||
| 11 | return freebsd | ||
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 6bd80485..2619d136 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -114,19 +114,16 @@ end | |||
| 114 | -- The tool is executed using a flag, usually just to ask its version. | 114 | -- The tool is executed using a flag, usually just to ask its version. |
| 115 | -- @param tool_cmd string: The command to be used to check the tool's presence (e.g. hg in case of Mercurial) | 115 | -- @param tool_cmd string: The command to be used to check the tool's presence (e.g. hg in case of Mercurial) |
| 116 | -- @param tool_name string: The actual name of the tool (e.g. Mercurial) | 116 | -- @param tool_name string: The actual name of the tool (e.g. Mercurial) |
| 117 | -- @param arg string: The flag to pass to the tool. '--version' by default. | 117 | function fs_lua.is_tool_available(tool_cmd, tool_name) |
| 118 | function fs_lua.is_tool_available(tool_cmd, tool_name, arg) | ||
| 119 | assert(type(tool_cmd) == "string") | 118 | assert(type(tool_cmd) == "string") |
| 120 | assert(type(tool_name) == "string") | 119 | assert(type(tool_name) == "string") |
| 121 | 120 | ||
| 122 | arg = arg or "--version" | ||
| 123 | assert(type(arg) == "string") | ||
| 124 | |||
| 125 | local ok | 121 | local ok |
| 126 | if tool_available_cache[tool_name] ~= nil then | 122 | if tool_available_cache[tool_name] ~= nil then |
| 127 | ok = tool_available_cache[tool_name] | 123 | ok = tool_available_cache[tool_name] |
| 128 | else | 124 | else |
| 129 | ok = fs.execute_quiet(tool_cmd, arg) | 125 | local tool_cmd_no_args = tool_cmd:gsub(" .*", "") |
| 126 | ok = fs.search_in_path(tool_cmd_no_args) | ||
| 130 | tool_available_cache[tool_name] = (ok == true) | 127 | tool_available_cache[tool_name] = (ok == true) |
| 131 | end | 128 | end |
| 132 | 129 | ||
diff --git a/src/luarocks/fs/netbsd.lua b/src/luarocks/fs/netbsd.lua deleted file mode 100644 index b9610339..00000000 --- a/src/luarocks/fs/netbsd.lua +++ /dev/null | |||
| @@ -1,14 +0,0 @@ | |||
| 1 | --- NetBSD implementation of filesystem and platform abstractions. | ||
| 2 | local netbsd = {} | ||
| 3 | |||
| 4 | local fs = require("luarocks.fs") | ||
| 5 | |||
| 6 | function netbsd.init() | ||
| 7 | local uz=io.open("/usr/bin/unzip", "r") | ||
| 8 | if uz ~= nil then | ||
| 9 | io.close(uz) | ||
| 10 | fs.set_tool_available("unzip", true) | ||
| 11 | end | ||
| 12 | end | ||
| 13 | |||
| 14 | return netbsd | ||
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0a154794..5c317f65 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua | |||
| @@ -22,8 +22,8 @@ do | |||
| 22 | md5checker = { | 22 | md5checker = { |
| 23 | desc = "MD5 checker", | 23 | desc = "MD5 checker", |
| 24 | { var = "MD5SUM", name = "md5sum" }, | 24 | { var = "MD5SUM", name = "md5sum" }, |
| 25 | { var = "OPENSSL", name = "openssl", cmdarg = "md5", checkarg = "version" }, | 25 | { var = "OPENSSL", name = "openssl", cmdarg = "md5" }, |
| 26 | { var = "MD5", name = "md5", checkarg = "-shello" }, | 26 | { var = "MD5", name = "md5" }, |
| 27 | }, | 27 | }, |
| 28 | } | 28 | } |
| 29 | 29 | ||
| @@ -33,7 +33,7 @@ do | |||
| 33 | if not tool then | 33 | if not tool then |
| 34 | for _, opt in ipairs(tool_options[tooltype]) do | 34 | for _, opt in ipairs(tool_options[tooltype]) do |
| 35 | table.insert(names, opt.name) | 35 | table.insert(names, opt.name) |
| 36 | if fs.is_tool_available(vars[opt.var], opt.name, opt.checkarg) then | 36 | if fs.is_tool_available(vars[opt.var], opt.name) then |
| 37 | tool = opt | 37 | tool = opt |
| 38 | tool_cache[tooltype] = opt | 38 | tool_cache[tooltype] = opt |
| 39 | break | 39 | break |
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 0065753e..28189089 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
| @@ -234,4 +234,15 @@ function unix.system_cache_dir() | |||
| 234 | return dir.path(fs.system_temp_dir(), "cache") | 234 | return dir.path(fs.system_temp_dir(), "cache") |
| 235 | end | 235 | end |
| 236 | 236 | ||
| 237 | function unix.search_in_path(program) | ||
| 238 | for d in (os.getenv("PATH") or ""):gmatch("([^:]+)") do | ||
| 239 | local fd = io.open(dir.path(d, program), "r") | ||
| 240 | if fd then | ||
| 241 | fd:close() | ||
| 242 | return true, d | ||
| 243 | end | ||
| 244 | end | ||
| 245 | return false | ||
| 246 | end | ||
| 247 | |||
| 237 | return unix | 248 | return unix |
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index d36d407d..f0c0a05c 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua | |||
| @@ -141,7 +141,7 @@ end | |||
| 141 | -- @return boolean: true on success, nil and error message on failure. | 141 | -- @return boolean: true on success, nil and error message on failure. |
| 142 | function tools.unzip(zipfile) | 142 | function tools.unzip(zipfile) |
| 143 | assert(zipfile) | 143 | assert(zipfile) |
| 144 | local ok, err = fs.is_tool_available(vars.UNZIP, "unzip", "--help") | 144 | local ok, err = fs.is_tool_available(vars.UNZIP, "unzip") |
| 145 | if not ok then | 145 | if not ok then |
| 146 | return nil, err | 146 | return nil, err |
| 147 | end | 147 | end |
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index a3f42df7..6c49f447 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
| @@ -363,4 +363,15 @@ function win32.system_cache_dir() | |||
| 363 | return dir.path(fs.system_temp_dir(), "cache") | 363 | return dir.path(fs.system_temp_dir(), "cache") |
| 364 | end | 364 | end |
| 365 | 365 | ||
| 366 | function win32.search_in_path(program) | ||
| 367 | for d in (os.getenv("PATH") or ""):gmatch("([^;]+)") do | ||
| 368 | local fd = io.open(dir.path(d, program .. ".exe"), "r") | ||
| 369 | if fd then | ||
| 370 | fd:close() | ||
| 371 | return true, d | ||
| 372 | end | ||
| 373 | end | ||
| 374 | return false | ||
| 375 | end | ||
| 376 | |||
| 366 | return win32 | 377 | return win32 |
