diff options
Diffstat (limited to 'src/luarocks/path.lua')
| -rw-r--r-- | src/luarocks/path.lua | 137 |
1 files changed, 2 insertions, 135 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index dafc64e7..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. |
| 5 | local path = {} | 5 | local path = {} |
| 6 | setmetatable(path, { __index = require("luarocks.core.path") }) | ||
| 6 | 7 | ||
| 7 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
| 8 | local cfg = require("luarocks.cfg") | 9 | local cfg = require("luarocks.core.cfg") |
| 9 | local util = require("luarocks.util") | 10 | local 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" |
| 18 | end | 19 | end |
| 19 | 20 | ||
| 20 | function 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 | ||
| 27 | end | ||
| 28 | |||
| 29 | function path.root_dir(rocks_dir) | 21 | function 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) .. ".*$") |
| 32 | end | 24 | end |
| 33 | 25 | ||
| 34 | function 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 | ||
| 41 | end | ||
| 42 | |||
| 43 | function path.deploy_bin_dir(tree) | 26 | function 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 |
| 50 | end | 33 | end |
| 51 | 34 | ||
| 52 | function 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 | ||
| 59 | end | ||
| 60 | |||
| 61 | function 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 | ||
| 68 | end | ||
| 69 | |||
| 70 | function path.manifest_file(tree) | 35 | function 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) |
| 230 | end | 195 | end |
| 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). | ||
| 239 | function 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 | ||
| 258 | end | ||
| 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 |
| 287 | end | 224 | end |
| 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. | ||
| 295 | function 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) | ||
| 303 | end | ||
| 304 | |||
| 305 | function path.use_tree(tree) | 226 | function 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,60 +231,6 @@ 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) |
| 311 | end | 232 | end |
| 312 | 233 | ||
| 313 | --- 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, | ||
| 315 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
| 316 | -- "none" for no trees (this function becomes a nop). | ||
| 317 | -- @param fn function: function to be applied, with the tree dir (string) as the first | ||
| 318 | -- argument and the remaining varargs of map_trees as the following arguments. | ||
| 319 | -- @return a table with all results of invocations of fn collected. | ||
| 320 | function path.map_trees(deps_mode, fn, ...) | ||
| 321 | local result = {} | ||
| 322 | if deps_mode == "one" then | ||
| 323 | table.insert(result, (fn(cfg.root_dir, ...)) or 0) | ||
| 324 | elseif deps_mode == "all" or deps_mode == "order" then | ||
| 325 | local use = false | ||
| 326 | if deps_mode == "all" then | ||
| 327 | use = true | ||
| 328 | end | ||
| 329 | for _, tree in ipairs(cfg.rocks_trees) do | ||
| 330 | if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then | ||
| 331 | use = true | ||
| 332 | end | ||
| 333 | if use then | ||
| 334 | table.insert(result, (fn(tree, ...)) or 0) | ||
| 335 | end | ||
| 336 | end | ||
| 337 | end | ||
| 338 | return result | ||
| 339 | end | ||
| 340 | |||
| 341 | local 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") | ||
| 351 | function 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 | ||
| 365 | end | ||
| 366 | |||
| 367 | --- 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, |
| 368 | -- 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 |
| 369 | -- in the given manifest. | 236 | -- in the given manifest. |
