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.lua139
1 files changed, 11 insertions, 128 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua
index dafc64e7..37898435 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,34 +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 end
254 end
255 if not name then name = file end
256 name = name:gsub("^%.+", ""):gsub("%.+$", "")
257 return name
258end
259
260--- Obtain the directory name where a module should be stored. 197--- Obtain the directory name where a module should be stored.
261-- For example, on Unix, "foo.bar.baz" will return "foo/bar". 198-- For example, on Unix, "foo.bar.baz" will return "foo/bar".
262-- @param mod string: A module name in Lua dot-separated format. 199-- @param mod string: A module name in Lua dot-separated format.
@@ -286,22 +223,6 @@ function path.configure_paths(rockspec)
286 rockspec.variables = vars 223 rockspec.variables = vars
287end 224end
288 225
289--- Produce a versioned version of a filename.
290-- @param file string: filename (must start with prefix)
291-- @param prefix string: Path prefix for file
292-- @param name string: Rock name
293-- @param version string: Rock version
294-- @return string: a pathname with the same directory parts and a versioned basename.
295function path.versioned_name(file, prefix, name, version)
296 assert(type(file) == "string")
297 assert(type(name) == "string")
298 assert(type(version) == "string")
299
300 local rest = file:sub(#prefix+1):gsub("^/*", "")
301 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
302 return dir.path(prefix, name_version.."-"..rest)
303end
304
305function path.use_tree(tree) 226function path.use_tree(tree)
306 cfg.root_dir = tree 227 cfg.root_dir = tree
307 cfg.rocks_dir = path.rocks_dir(tree) 228 cfg.rocks_dir = path.rocks_dir(tree)
@@ -310,6 +231,15 @@ function path.use_tree(tree)
310 cfg.deploy_lib_dir = path.deploy_lib_dir(tree) 231 cfg.deploy_lib_dir = path.deploy_lib_dir(tree)
311end 232end
312 233
234function path.rocks_tree_to_string(tree)
235 if type(tree) == "string" then
236 return tree
237 else
238 assert(type(tree) == "table")
239 return tree.root
240 end
241end
242
313--- Apply a given function to the active rocks trees based on chosen dependency mode. 243--- Apply a given function to the active rocks trees based on chosen dependency mode.
314-- @param deps_mode string: Dependency mode: "one" for the current default tree, 244-- @param deps_mode string: Dependency mode: "one" for the current default tree,
315-- "all" for all trees, "order" for all trees with priority >= the current default, 245-- "all" for all trees, "order" for all trees with priority >= the current default,
@@ -338,51 +268,4 @@ function path.map_trees(deps_mode, fn, ...)
338 return result 268 return result
339end 269end
340 270
341local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
342
343--- Return the pathname of the file that would be loaded for a module, indexed.
344-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
345-- @param name string: name of the package (eg. "luasocket")
346-- @param version string: version number (eg. "2.0.2-1")
347-- @param tree string: repository path (eg. "/usr/local")
348-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
349-- This is done this way for use by select_module in luarocks.loader.
350-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
351function path.which_i(file_name, name, version, tree, i)
352 local deploy_dir
353 local extension = file_name:match("%.[a-z]+$")
354 if is_src_extension[extension] then
355 deploy_dir = path.deploy_lua_dir(tree)
356 file_name = dir.path(deploy_dir, file_name)
357 else
358 deploy_dir = path.deploy_lib_dir(tree)
359 file_name = dir.path(deploy_dir, file_name)
360 end
361 if i > 1 then
362 file_name = path.versioned_name(file_name, deploy_dir, name, version)
363 end
364 return file_name
365end
366
367--- Return the pathname of the file that would be loaded for a module,
368-- returning the versioned pathname if given version is not the default version
369-- in the given manifest.
370-- @param module_name string: module name (eg. "socket.core")
371-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
372-- @param name string: name of the package (eg. "luasocket")
373-- @param version string: version number (eg. "2.0.2-1")
374-- @param tree string: repository path (eg. "/usr/local")
375-- @param manifest table: the manifest table for the tree.
376-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
377function path.which(module_name, file_name, name, version, tree, manifest)
378 local versions = manifest.modules[module_name]
379 assert(versions)
380 for i, name_version in ipairs(versions) do
381 if name_version == name.."/"..version then
382 return path.which_i(file_name, name, version, tree, i):gsub("//", "/")
383 end
384 end
385 assert(false)
386end
387
388return path 271return path