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 | |
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.
-rw-r--r-- | spec/loader_spec.lua | 25 | ||||
-rw-r--r-- | src/luarocks/cmd/which.lua | 22 | ||||
-rw-r--r-- | src/luarocks/loader.lua | 46 |
3 files changed, 66 insertions, 27 deletions
diff --git a/spec/loader_spec.lua b/spec/loader_spec.lua index 35179080..c8526117 100644 --- a/spec/loader_spec.lua +++ b/spec/loader_spec.lua | |||
@@ -4,10 +4,21 @@ local testing_paths = test_env.testing_paths | |||
4 | local write_file = test_env.write_file | 4 | local write_file = test_env.write_file |
5 | 5 | ||
6 | describe("luarocks.loader", function() | 6 | describe("luarocks.loader", function() |
7 | |||
8 | before_each(function() | ||
9 | test_env.setup_specs() | ||
10 | end) | ||
11 | |||
7 | describe("#unit", function() | 12 | describe("#unit", function() |
8 | it("starts", function() | 13 | it("starts", function() |
9 | assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) | 14 | assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) |
10 | end) | 15 | end) |
16 | |||
17 | describe("which", function() | ||
18 | it("finds modules using package.path", function() | ||
19 | assert(run.lua_bool([[-e "loader = require 'luarocks.loader'; local x,y,z,p = loader.which('luarocks.loader', 'p'); assert(p == 'p')"]])) | ||
20 | end) | ||
21 | end) | ||
11 | end) | 22 | end) |
12 | 23 | ||
13 | describe("#integration", function() | 24 | describe("#integration", function() |
@@ -21,13 +32,13 @@ describe("luarocks.loader", function() | |||
21 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_01.lua" | 32 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_01.lua" |
22 | } | 33 | } |
23 | build = { | 34 | build = { |
24 | type = "builtin", | 35 | type = "builtin", |
25 | modules = { | 36 | modules = { |
26 | rock_b = "rock_b_01.lua" | 37 | rock_b = "rock_b_01.lua" |
27 | } | 38 | } |
28 | } | 39 | } |
29 | ]], finally) | 40 | ]], finally) |
30 | 41 | ||
31 | write_file("rock_b_10.lua", "print('ROCK B 1.0'); return {}", finally) | 42 | write_file("rock_b_10.lua", "print('ROCK B 1.0'); return {}", finally) |
32 | write_file("rock_b-1.0-1.rockspec", [[ | 43 | write_file("rock_b-1.0-1.rockspec", [[ |
33 | package = "rock_b" | 44 | package = "rock_b" |
@@ -36,13 +47,13 @@ describe("luarocks.loader", function() | |||
36 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_10.lua" | 47 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_10.lua" |
37 | } | 48 | } |
38 | build = { | 49 | build = { |
39 | type = "builtin", | 50 | type = "builtin", |
40 | modules = { | 51 | modules = { |
41 | rock_b = "rock_b_10.lua" | 52 | rock_b = "rock_b_10.lua" |
42 | } | 53 | } |
43 | } | 54 | } |
44 | ]], finally) | 55 | ]], finally) |
45 | 56 | ||
46 | write_file("rock_a.lua", "require('rock_b'); return {}", finally) | 57 | write_file("rock_a.lua", "require('rock_b'); return {}", finally) |
47 | write_file("rock_a-2.0-1.rockspec", [[ | 58 | write_file("rock_a-2.0-1.rockspec", [[ |
48 | package = "rock_a" | 59 | package = "rock_a" |
@@ -54,7 +65,7 @@ describe("luarocks.loader", function() | |||
54 | "rock_b < 1.0", | 65 | "rock_b < 1.0", |
55 | } | 66 | } |
56 | build = { | 67 | build = { |
57 | type = "builtin", | 68 | type = "builtin", |
58 | modules = { | 69 | modules = { |
59 | rock_a = "rock_a.lua" | 70 | rock_a = "rock_a.lua" |
60 | } | 71 | } |
@@ -64,9 +75,9 @@ describe("luarocks.loader", function() | |||
64 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-0.1-1.rockspec")) | 75 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-0.1-1.rockspec")) |
65 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-1.0-1.rockspec --keep")) | 76 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-1.0-1.rockspec --keep")) |
66 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_a-2.0-1.rockspec")) | 77 | print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_a-2.0-1.rockspec")) |
67 | 78 | ||
68 | local output = run.lua([[-e "require 'luarocks.loader'; require('rock_a')"]]) | 79 | local output = run.lua([[-e "require 'luarocks.loader'; require('rock_a')"]]) |
69 | 80 | ||
70 | assert.matches("ROCK B 0.1", output, 1, true) | 81 | assert.matches("ROCK B 0.1", output, 1, true) |
71 | end) | 82 | end) |
72 | end) | 83 | end) |
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. |