From 6898b379a8c25db3e21ba27b96680c7d0c5683f2 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 5 Oct 2017 12:59:50 -0300 Subject: Add `luarocks which` command. (#733) --- src/bin/luarocks | 1 + src/luarocks/cmd/which.lua | 30 ++++++++++++++++++++++++++++++ src/luarocks/loader.lua | 7 ++++--- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/luarocks/cmd/which.lua diff --git a/src/bin/luarocks b/src/bin/luarocks index 88a1d1ca..491809dd 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -28,6 +28,7 @@ commands = { doc = "luarocks.cmd.doc", upload = "luarocks.cmd.upload", config = "luarocks.cmd.config", + which = "luarocks.cmd.which", } command_line.run_command(...) diff --git a/src/luarocks/cmd/which.lua b/src/luarocks/cmd/which.lua new file mode 100644 index 00000000..0116fdb8 --- /dev/null +++ b/src/luarocks/cmd/which.lua @@ -0,0 +1,30 @@ + +--- @module luarocks.which_cmd +-- Driver for the `luarocks which` command. +local which_cmd = {} + +local loader = require("luarocks.loader") +local cfg = require("luarocks.core.cfg") +local util = require("luarocks.util") + +which_cmd.help_summary = "Tell which file corresponds to a given module name." +which_cmd.help_arguments = "" +which_cmd.help = [[ +Given a module name like "foo.bar", output which file would be loaded to resolve +that module by luarocks.loader, like "/usr/local/lua/]]..cfg.lua_version..[[/foo/bar.lua". +]] + +--- Driver function for "lua" command. +-- @return boolean This function terminates the interpreter. +function which_cmd.command(_, modname) + local pathname, rock_name, rock_version = loader.which(modname) + if not pathname then + return nil, "Module '" .. modname .. "' not found by luarocks.loader." + end + util.printout(pathname) + util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") + return true +end + +return which_cmd + diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua index c8259689..84cdd696 100644 --- a/src/luarocks/loader.lua +++ b/src/luarocks/loader.lua @@ -222,10 +222,11 @@ end --- Return the pathname of the file that would be loaded for a module. -- @param module string: module name (eg. "socket.core") --- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") +-- @return filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), +-- the rock name and the rock version. function loader.which(module) - local _, _, file_name = select_module(module, path.which_i) - return file_name + local rock_name, rock_version, file_name = select_module(module, path.which_i) + return file_name, rock_name, rock_version end --- Package loader for LuaRocks support. -- cgit v1.2.3-55-g6feb