aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks/path.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/luarocks/path.lua')
-rw-r--r--src/luarocks/path.lua142
1 files changed, 2 insertions, 140 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua
index 6219d8c6..83e9c295 100644
--- a/src/luarocks/path.lua
+++ b/src/luarocks/path.lua
@@ -3,9 +3,10 @@
3-- All paths are configured in this module, making it a single 3-- All paths are configured in this module, making it a single
4-- point where the layout of the local installation is defined in LuaRocks. 4-- point where the layout of the local installation is defined in LuaRocks.
5local path = {} 5local path = {}
6setmetatable(path, { __index = require("luarocks.core.path") })
6 7
7local dir = require("luarocks.dir") 8local dir = require("luarocks.dir")
8local cfg = require("luarocks.cfg") 9local cfg = require("luarocks.core.cfg")
9local util = require("luarocks.util") 10local util = require("luarocks.util")
10 11
11--- Infer rockspec filename from a rock filename. 12--- Infer rockspec filename from a rock filename.
@@ -17,29 +18,11 @@ function path.rockspec_name_from_rock(rock_name)
17 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" 18 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec"
18end 19end
19 20
20function path.rocks_dir(tree)
21 if type(tree) == "string" then
22 return dir.path(tree, cfg.rocks_subdir)
23 else
24 assert(type(tree) == "table")
25 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir)
26 end
27end
28
29function path.root_dir(rocks_dir) 21function path.root_dir(rocks_dir)
30 assert(type(rocks_dir) == "string") 22 assert(type(rocks_dir) == "string")
31 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") 23 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$")
32end 24end
33 25
34function path.rocks_tree_to_string(tree)
35 if type(tree) == "string" then
36 return tree
37 else
38 assert(type(tree) == "table")
39 return tree.root
40 end
41end
42
43function path.deploy_bin_dir(tree) 26function path.deploy_bin_dir(tree)
44 if type(tree) == "string" then 27 if type(tree) == "string" then
45 return dir.path(tree, "bin") 28 return dir.path(tree, "bin")
@@ -49,24 +32,6 @@ function path.deploy_bin_dir(tree)
49 end 32 end
50end 33end
51 34
52function path.deploy_lua_dir(tree)
53 if type(tree) == "string" then
54 return dir.path(tree, cfg.lua_modules_path)
55 else
56 assert(type(tree) == "table")
57 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
58 end
59end
60
61function path.deploy_lib_dir(tree)
62 if type(tree) == "string" then
63 return dir.path(tree, cfg.lib_modules_path)
64 else
65 assert(type(tree) == "table")
66 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
67 end
68end
69
70function path.manifest_file(tree) 35function path.manifest_file(tree)
71 if type(tree) == "string" then 36 if type(tree) == "string" then
72 return dir.path(tree, cfg.rocks_subdir, "manifest") 37 return dir.path(tree, cfg.rocks_subdir, "manifest")
@@ -229,39 +194,6 @@ function path.make_url(pathname, name, version, arch)
229 return dir.path(pathname, filename) 194 return dir.path(pathname, filename)
230end 195end
231 196
232--- Convert a pathname to a module identifier.
233-- In Unix, for example, a path "foo/bar/baz.lua" is converted to
234-- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo".
235-- @param file string: Pathname of module
236-- @return string: The module identifier, or nil if given path is
237-- not a conformant module path (the function does not check if the
238-- path actually exists).
239function path.path_to_module(file)
240 assert(type(file) == "string")
241
242 local name = file:match("(.*)%."..cfg.lua_extension.."$")
243 if name then
244 name = name:gsub(dir.separator, ".")
245 local init = name:match("(.*)%.init$")
246 if init then
247 name = init
248 end
249 else
250 name = file:match("(.*)%."..cfg.lib_extension.."$")
251 if name then
252 name = name:gsub(dir.separator, ".")
253 else
254 name = file:match("(.*)%."..cfg.static_lib_extension.."$")
255 if name then
256 name = name:gsub(dir.separator, ".")
257 end
258 end
259 end
260 if not name then name = file end
261 name = name:gsub("^%.+", ""):gsub("%.+$", "")
262 return name
263end
264
265--- Obtain the directory name where a module should be stored. 197--- Obtain the directory name where a module should be stored.
266-- For example, on Unix, "foo.bar.baz" will return "foo/bar". 198-- For example, on Unix, "foo.bar.baz" will return "foo/bar".
267-- @param mod string: A module name in Lua dot-separated format. 199-- @param mod string: A module name in Lua dot-separated format.
@@ -291,22 +223,6 @@ function path.configure_paths(rockspec)
291 rockspec.variables = vars 223 rockspec.variables = vars
292end 224end
293 225
294--- Produce a versioned version of a filename.
295-- @param file string: filename (must start with prefix)
296-- @param prefix string: Path prefix for file
297-- @param name string: Rock name
298-- @param version string: Rock version
299-- @return string: a pathname with the same directory parts and a versioned basename.
300function path.versioned_name(file, prefix, name, version)
301 assert(type(file) == "string")
302 assert(type(name) == "string")
303 assert(type(version) == "string")
304
305 local rest = file:sub(#prefix+1):gsub("^/*", "")
306 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
307 return dir.path(prefix, name_version.."-"..rest)
308end
309
310function path.use_tree(tree) 226function path.use_tree(tree)
311 cfg.root_dir = tree 227 cfg.root_dir = tree
312 cfg.rocks_dir = path.rocks_dir(tree) 228 cfg.rocks_dir = path.rocks_dir(tree)
@@ -315,60 +231,6 @@ function path.use_tree(tree)
315 cfg.deploy_lib_dir = path.deploy_lib_dir(tree) 231 cfg.deploy_lib_dir = path.deploy_lib_dir(tree)
316end 232end
317 233
318--- Apply a given function to the active rocks trees based on chosen dependency mode.
319-- @param deps_mode string: Dependency mode: "one" for the current default tree,
320-- "all" for all trees, "order" for all trees with priority >= the current default,
321-- "none" for no trees (this function becomes a nop).
322-- @param fn function: function to be applied, with the tree dir (string) as the first
323-- argument and the remaining varargs of map_trees as the following arguments.
324-- @return a table with all results of invocations of fn collected.
325function path.map_trees(deps_mode, fn, ...)
326 local result = {}
327 if deps_mode == "one" then
328 table.insert(result, (fn(cfg.root_dir, ...)) or 0)
329 elseif deps_mode == "all" or deps_mode == "order" then
330 local use = false
331 if deps_mode == "all" then
332 use = true
333 end
334 for _, tree in ipairs(cfg.rocks_trees) do
335 if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then
336 use = true
337 end
338 if use then
339 table.insert(result, (fn(tree, ...)) or 0)
340 end
341 end
342 end
343 return result
344end
345
346local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
347
348--- Return the pathname of the file that would be loaded for a module, indexed.
349-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
350-- @param name string: name of the package (eg. "luasocket")
351-- @param version string: version number (eg. "2.0.2-1")
352-- @param tree string: repository path (eg. "/usr/local")
353-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
354-- This is done this way for use by select_module in luarocks.loader.
355-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
356function path.which_i(file_name, name, version, tree, i)
357 local deploy_dir
358 local extension = file_name:match("%.[a-z]+$")
359 if is_src_extension[extension] then
360 deploy_dir = path.deploy_lua_dir(tree)
361 file_name = dir.path(deploy_dir, file_name)
362 else
363 deploy_dir = path.deploy_lib_dir(tree)
364 file_name = dir.path(deploy_dir, file_name)
365 end
366 if i > 1 then
367 file_name = path.versioned_name(file_name, deploy_dir, name, version)
368 end
369 return file_name
370end
371
372--- Return the pathname of the file that would be loaded for a module, 234--- Return the pathname of the file that would be loaded for a module,
373-- returning the versioned pathname if given version is not the default version 235-- returning the versioned pathname if given version is not the default version
374-- in the given manifest. 236-- in the given manifest.