diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2022-06-06 15:28:14 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2022-06-06 17:14:52 -0300 |
| commit | bec4a9cbf72c8e392163f50f7b6bbb18763d9f90 (patch) | |
| tree | ec131971a48b331bdd05b074eb365f3bae1d3d36 /src | |
| parent | 292a274fd02b88a729d474b544829c53e138a57b (diff) | |
| download | luarocks-bec4a9cbf72c8e392163f50f7b6bbb18763d9f90.tar.gz luarocks-bec4a9cbf72c8e392163f50f7b6bbb18763d9f90.tar.bz2 luarocks-bec4a9cbf72c8e392163f50f7b6bbb18763d9f90.zip | |
loader.which: new option for searching package.path and cpath
Adds a new second argument, `where`, a string which indicates places
to search for the module.
If `where` contains "l", it will search using the LuaRocks loader; if it
contains "p", it will look in the filesystem using package.path and
package.cpath. You can use both at the same time.
If successful, it will return four values.
* If found using the LuaRocks loader, it will return:
* filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"),
* rock name
* rock version
* "l" to indicate the match comes from the loader.
* If found scanning package.path and package.cpath, it will return:
* filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"),
* "path" or "cpath"
* nil
* "p" to indicate the match comes from scanning package.path and cpath.
If unsuccessful, nothing is returned.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/cmd/which.lua | 22 | ||||
| -rw-r--r-- | src/luarocks/loader.lua | 46 |
2 files changed, 48 insertions, 20 deletions
diff --git a/src/luarocks/cmd/which.lua b/src/luarocks/cmd/which.lua index 7503df00..f50a43c3 100644 --- a/src/luarocks/cmd/which.lua +++ b/src/luarocks/cmd/which.lua | |||
| @@ -6,7 +6,6 @@ local which_cmd = {} | |||
| 6 | local loader = require("luarocks.loader") | 6 | local loader = require("luarocks.loader") |
| 7 | local cfg = require("luarocks.core.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
| 8 | local util = require("luarocks.util") | 8 | local util = require("luarocks.util") |
| 9 | local fs = require("luarocks.fs") | ||
| 10 | 9 | ||
| 11 | function which_cmd.add_to_parser(parser) | 10 | function which_cmd.add_to_parser(parser) |
| 12 | local cmd = parser:command("which", 'Given a module name like "foo.bar", '.. | 11 | local cmd = parser:command("which", 'Given a module name like "foo.bar", '.. |
| @@ -21,24 +20,17 @@ end | |||
| 21 | --- Driver function for "which" command. | 20 | --- Driver function for "which" command. |
| 22 | -- @return boolean This function terminates the interpreter. | 21 | -- @return boolean This function terminates the interpreter. |
| 23 | function which_cmd.command(args) | 22 | function which_cmd.command(args) |
| 24 | local pathname, rock_name, rock_version = loader.which(args.modname) | 23 | local pathname, rock_name, rock_version, where = loader.which(args.modname, "lp") |
| 25 | 24 | ||
| 26 | if pathname then | 25 | if pathname then |
| 27 | util.printout(pathname) | 26 | util.printout(pathname) |
| 28 | util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") | 27 | if where == "l" then |
| 29 | return true | 28 | util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") |
| 30 | end | 29 | else |
| 31 | 30 | local key = rock_name | |
| 32 | local modpath = args.modname:gsub("%.", "/") | 31 | util.printout("(found directly via package." .. key.. " -- not installed as a rock?)") |
| 33 | for _, v in ipairs({"path", "cpath"}) do | ||
| 34 | for p in package[v]:gmatch("([^;]+)") do | ||
| 35 | local pathname = p:gsub("%?", modpath) -- luacheck: ignore 421 | ||
| 36 | if fs.exists(pathname) then | ||
| 37 | util.printout(pathname) | ||
| 38 | util.printout("(found directly via package." .. v .. " -- not installed as a rock?)") | ||
| 39 | return true | ||
| 40 | end | ||
| 41 | end | 32 | end |
| 33 | return true | ||
| 42 | end | 34 | end |
| 43 | 35 | ||
| 44 | return nil, "Module '" .. args.modname .. "' not found." | 36 | return nil, "Module '" .. args.modname .. "' not found." |
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua index 825e4ce7..772fdfcb 100644 --- a/src/luarocks/loader.lua +++ b/src/luarocks/loader.lua | |||
| @@ -194,11 +194,47 @@ end | |||
| 194 | 194 | ||
| 195 | --- Return the pathname of the file that would be loaded for a module. | 195 | --- Return the pathname of the file that would be loaded for a module. |
| 196 | -- @param module string: module name (eg. "socket.core") | 196 | -- @param module string: module name (eg. "socket.core") |
| 197 | -- @return filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), | 197 | -- @param where string: places to look for the module. If `where` contains |
| 198 | -- the rock name and the rock version. | 198 | -- "l", it will search using the LuaRocks loader; if it contains "p", |
| 199 | function loader.which(module) | 199 | -- it will look in the filesystem using package.path and package.cpath. |
| 200 | local rock_name, rock_version, file_name = select_module(module, path.which_i) | 200 | -- You can use both at the same time. |
| 201 | return file_name, rock_name, rock_version | 201 | -- @return If successful, it will return four values. |
| 202 | -- * If found using the LuaRocks loader, it will return: | ||
| 203 | -- * filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), | ||
| 204 | -- * rock name | ||
| 205 | -- * rock version | ||
| 206 | -- * "l" to indicate the match comes from the loader. | ||
| 207 | -- * If found scanning package.path and package.cpath, it will return: | ||
| 208 | -- * filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), | ||
| 209 | -- * "path" or "cpath" | ||
| 210 | -- * nil | ||
| 211 | -- * "p" to indicate the match comes from scanning package.path and cpath. | ||
| 212 | -- If unsuccessful, nothing is returned. | ||
| 213 | function loader.which(module, where) | ||
| 214 | where = where or "l" | ||
| 215 | if where:match("l") then | ||
| 216 | local rock_name, rock_version, file_name = select_module(module, path.which_i) | ||
| 217 | if rock_name then | ||
| 218 | local fd = io.open(file_name) | ||
| 219 | if fd then | ||
| 220 | fd:close() | ||
| 221 | return file_name, rock_name, rock_version, "l" | ||
| 222 | end | ||
| 223 | end | ||
| 224 | end | ||
| 225 | if where:match("p") then | ||
| 226 | local modpath = module:gsub("%.", "/") | ||
| 227 | for _, v in ipairs({"path", "cpath"}) do | ||
| 228 | for p in package[v]:gmatch("([^;]+)") do | ||
| 229 | local file_name = p:gsub("%?", modpath) -- luacheck: ignore 421 | ||
| 230 | local fd = io.open(file_name) | ||
| 231 | if fd then | ||
| 232 | fd:close() | ||
| 233 | return file_name, v, nil, "p" | ||
| 234 | end | ||
| 235 | end | ||
| 236 | end | ||
| 237 | end | ||
| 202 | end | 238 | end |
| 203 | 239 | ||
| 204 | --- Package loader for LuaRocks support. | 240 | --- Package loader for LuaRocks support. |
