From a3ee3e91ae5147f15f521decfe4d9188afefc782 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 20 Mar 2014 21:40:47 +0400 Subject: Split luarocks.path module --- src/bin/luarocks | 2 +- src/luarocks/path.lua | 154 +++++++++++++++--------------------------- src/luarocks/path_command.lua | 58 ++++++++++++++++ 3 files changed, 113 insertions(+), 101 deletions(-) create mode 100644 src/luarocks/path_command.lua (limited to 'src') diff --git a/src/bin/luarocks b/src/bin/luarocks index e9cfc349..9c190175 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -16,7 +16,7 @@ commands = { remove = "luarocks.remove", make = "luarocks.make", download = "luarocks.download", - path = "luarocks.path", + path = "luarocks.path_command", show = "luarocks.show", new_version = "luarocks.new_version", lint = "luarocks.lint", diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index d7273095..598e51d9 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -2,31 +2,23 @@ --- LuaRocks-specific path handling functions. -- All paths are configured in this module, making it a single -- point where the layout of the local installation is defined in LuaRocks. -module("luarocks.path", package.seeall) +--module("luarocks.path", package.seeall) +local path = {} local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") local util = require("luarocks.util") -local deps = require("luarocks.deps") - -help_summary = "Return the currently configured package path." -help_arguments = "" -help = [[ -Returns the package path currently configured for this installation -of LuaRocks, formatted as shell commands to update LUA_PATH and -LUA_CPATH. (On Unix systems, you may run: eval `luarocks path`) -]] --- Infer rockspec filename from a rock filename. -- @param rock_name string: Pathname of a rock file. -- @return string: Filename of the rockspec, without path. -function rockspec_name_from_rock(rock_name) +function path.rockspec_name_from_rock(rock_name) assert(type(rock_name) == "string") local base_name = dir.base_name(rock_name) return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" end -function rocks_dir(tree) +function path.rocks_dir(tree) if type(tree) == "string" then return dir.path(tree, cfg.rocks_subdir) else @@ -35,12 +27,12 @@ function rocks_dir(tree) end end -function root_dir(rocks_dir) +function path.root_dir(rocks_dir) assert(type(rocks_dir) == "string") return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") end -function rocks_tree_to_string(tree) +function path.rocks_tree_to_string(tree) if type(tree) == "string" then return tree else @@ -49,7 +41,7 @@ function rocks_tree_to_string(tree) end end -function deploy_bin_dir(tree) +function path.deploy_bin_dir(tree) if type(tree) == "string" then return dir.path(tree, "bin") else @@ -58,7 +50,7 @@ function deploy_bin_dir(tree) end end -function deploy_lua_dir(tree) +function path.deploy_lua_dir(tree) if type(tree) == "string" then return dir.path(tree, cfg.lua_modules_path) else @@ -67,7 +59,7 @@ function deploy_lua_dir(tree) end end -function deploy_lib_dir(tree) +function path.deploy_lib_dir(tree) if type(tree) == "string" then return dir.path(tree, cfg.lib_modules_path) else @@ -76,7 +68,7 @@ function deploy_lib_dir(tree) end end -function manifest_file(tree) +function path.manifest_file(tree) if type(tree) == "string" then return dir.path(tree, cfg.rocks_subdir, "manifest") else @@ -90,10 +82,10 @@ end -- @return string: The resulting path -- does not guarantee that -- @param tree string or nil: If given, specifies the local tree to use. -- the package (and by extension, the path) exists. -function versions_dir(name, tree) +function path.versions_dir(name, tree) assert(type(name) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name) + return dir.path(path.rocks_dir(tree), name) end --- Get the local installation directory (prefix) for a package. @@ -102,11 +94,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function install_dir(name, version, tree) +function path.install_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version) + return dir.path(path.rocks_dir(tree), name, version) end --- Get the local filename of the rockspec of an installed rock. @@ -115,11 +107,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the file) exists. -function rockspec_file(name, version, tree) +function path.rockspec_file(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, name.."-"..version..".rockspec") + return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") end --- Get the local filename of the rock_manifest file of an installed rock. @@ -128,11 +120,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the file) exists. -function rock_manifest_file(name, version, tree) +function path.rock_manifest_file(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "rock_manifest") + return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") end --- Get the local installation directory for C libraries of a package. @@ -141,11 +133,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function lib_dir(name, version, tree) +function path.lib_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "lib") + return dir.path(path.rocks_dir(tree), name, version, "lib") end --- Get the local installation directory for Lua modules of a package. @@ -154,11 +146,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function lua_dir(name, version, tree) +function path.lua_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "lua") + return dir.path(path.rocks_dir(tree), name, version, "lua") end --- Get the local installation directory for documentation of a package. @@ -167,11 +159,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function doc_dir(name, version, tree) +function path.doc_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "doc") + return dir.path(path.rocks_dir(tree), name, version, "doc") end --- Get the local installation directory for configuration files of a package. @@ -180,11 +172,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function conf_dir(name, version, tree) +function path.conf_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "conf") + return dir.path(path.rocks_dir(tree), name, version, "conf") end --- Get the local installation directory for command-line scripts @@ -194,11 +186,11 @@ end -- @param tree string or nil: If given, specifies the local tree to use. -- @return string: The resulting path -- does not guarantee that -- the package (and by extension, the path) exists. -function bin_dir(name, version, tree) +function path.bin_dir(name, version, tree) assert(type(name) == "string") assert(type(version) == "string") tree = tree or cfg.root_dir - return dir.path(rocks_dir(tree), name, version, "bin") + return dir.path(path.rocks_dir(tree), name, version, "bin") end --- Extract name, version and arch of a rock filename, @@ -206,7 +198,7 @@ end -- @param file_name string: pathname of a rock or rockspec -- @return (string, string, string) or nil: name, version and arch -- or nil if name could not be parsed -function parse_name(file_name) +function path.parse_name(file_name) assert(type(file_name) == "string") if file_name:match("%.rock$") then return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") @@ -221,7 +213,7 @@ end -- @param version string: Package version. -- @param arch string: Architecture identifier, or "rockspec" or "installed". -- @return string: A URL or pathname following LuaRocks naming conventions. -function make_url(pathname, name, version, arch) +function path.make_url(pathname, name, version, arch) assert(type(pathname) == "string") assert(type(name) == "string") assert(type(version) == "string") @@ -245,7 +237,7 @@ end -- @return string: The module identifier, or nil if given path is -- not a conformant module path (the function does not check if the -- path actually exists). -function path_to_module(file) +function path.path_to_module(file) assert(type(file) == "string") local name = file:match("(.*)%."..cfg.lua_extension.."$") @@ -270,7 +262,7 @@ end -- For example, on Unix, "foo.bar.baz" will return "foo/bar". -- @param mod string: A module name in Lua dot-separated format. -- @return string: A directory name using the platform's separator. -function module_to_path(mod) +function path.module_to_path(mod) assert(type(mod) == "string") return (mod:gsub("[^.]*$", ""):gsub("%.", dir.separator)) end @@ -279,19 +271,19 @@ end -- Create a "variables" table in the rockspec table, containing -- adjusted variables according to the configuration file. -- @param rockspec table: The rockspec table. -function configure_paths(rockspec) +function path.configure_paths(rockspec) assert(type(rockspec) == "table") local vars = {} for k,v in pairs(cfg.variables) do vars[k] = v end local name, version = rockspec.name, rockspec.version - vars.PREFIX = install_dir(name, version) - vars.LUADIR = lua_dir(name, version) - vars.LIBDIR = lib_dir(name, version) - vars.CONFDIR = conf_dir(name, version) - vars.BINDIR = bin_dir(name, version) - vars.DOCDIR = doc_dir(name, version) + vars.PREFIX = path.install_dir(name, version) + vars.LUADIR = path.lua_dir(name, version) + vars.LIBDIR = path.lib_dir(name, version) + vars.CONFDIR = path.conf_dir(name, version) + vars.BINDIR = path.bin_dir(name, version) + vars.DOCDIR = path.doc_dir(name, version) rockspec.variables = vars end @@ -301,7 +293,7 @@ end -- @param name string: Rock name -- @param version string: Rock version -- @return string: a pathname with the same directory parts and a versioned basename. -function versioned_name(file, prefix, name, version) +function path.versioned_name(file, prefix, name, version) assert(type(file) == "string") assert(type(name) == "string") assert(type(version) == "string") @@ -311,12 +303,12 @@ function versioned_name(file, prefix, name, version) return dir.path(prefix, name_version.."-"..rest) end -function use_tree(tree) +function path.use_tree(tree) cfg.root_dir = tree - cfg.rocks_dir = rocks_dir(tree) - cfg.deploy_bin_dir = deploy_bin_dir(tree) - cfg.deploy_lua_dir = deploy_lua_dir(tree) - cfg.deploy_lib_dir = deploy_lib_dir(tree) + cfg.rocks_dir = path.rocks_dir(tree) + cfg.deploy_bin_dir = path.deploy_bin_dir(tree) + cfg.deploy_lua_dir = path.deploy_lua_dir(tree) + cfg.deploy_lib_dir = path.deploy_lib_dir(tree) end --- Apply a given function to the active rocks trees based on chosen dependency mode. @@ -326,7 +318,7 @@ end -- @param fn function: function to be applied, with the tree dir (string) as the first -- argument and the remaining varargs of map_trees as the following arguments. -- @return a table with all results of invocations of fn collected. -function map_trees(deps_mode, fn, ...) +function path.map_trees(deps_mode, fn, ...) local result = {} if deps_mode == "one" then table.insert(result, (fn(cfg.root_dir, ...)) or 0) @@ -336,7 +328,7 @@ function map_trees(deps_mode, fn, ...) use = true end for _, tree in ipairs(cfg.rocks_trees) do - if dir.normalize(rocks_tree_to_string(tree)) == dir.normalize(rocks_tree_to_string(cfg.root_dir)) then + if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then use = true end if use then @@ -355,17 +347,17 @@ end -- @param i number: the index, 1 if version is the current default, > 1 otherwise. -- This is done this way for use by select_module in luarocks.loader. -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") -function which_i(module_name, name, version, tree, i) +function path.which_i(module_name, name, version, tree, i) local deploy_dir if module_name:match("%.lua$") then - deploy_dir = deploy_lua_dir(tree) + deploy_dir = path.deploy_lua_dir(tree) module_name = dir.path(deploy_dir, module_name) else - deploy_dir = deploy_lib_dir(tree) + deploy_dir = path.deploy_lib_dir(tree) module_name = dir.path(deploy_dir, module_name) end if i > 1 then - module_name = versioned_name(module_name, deploy_dir, name, version) + module_name = path.versioned_name(module_name, deploy_dir, name, version) end return module_name end @@ -379,53 +371,15 @@ end -- @param tree string: repository path (eg. "/usr/local") -- @param manifest table: the manifest table for the tree. -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") -function which(module_name, filename, name, version, tree, manifest) +function path.which(module_name, filename, name, version, tree, manifest) local versions = manifest.modules[module_name] assert(versions) for i, name_version in ipairs(versions) do if name_version == name.."/"..version then - return which_i(filename, name, version, tree, i):gsub("//", "/") + return path.which_i(filename, name, version, tree, i):gsub("//", "/") end end assert(false) end ---- Driver function for "path" command. --- @return boolean This function always succeeds. -function run(...) - local flags = util.parse_flags(...) - local deps_mode = deps.get_deps_mode(flags) - - local lr_path, lr_cpath = cfg.package_paths() - local bin_dirs = map_trees(deps_mode, deploy_bin_dir) - - if flags["lr-path"] then - util.printout(util.remove_path_dupes(lr_path, ';')) - return true - elseif flags["lr-cpath"] then - util.printout(util.remove_path_dupes(lr_cpath, ';')) - return true - elseif flags["lr-bin"] then - local lr_bin = util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator) - util.printout(util.remove_path_dupes(lr_bin, ';')) - return true - end - - if flags["append"] then - lr_path = package.path .. ";" .. lr_path - lr_cpath = package.cpath .. ";" .. lr_cpath - else - lr_path = lr_path.. ";" .. package.path - lr_cpath = lr_cpath .. ";" .. package.cpath - end - - util.printout(cfg.export_lua_path:format(util.remove_path_dupes(lr_path, ';'))) - util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(lr_cpath, ';'))) - if flags["bin"] then - table.insert(bin_dirs, 1, os.getenv("PATH")) - local lr_bin = util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator) - util.printout(cfg.export_path:format(lr_bin)) - end - return true -end - +return path diff --git a/src/luarocks/path_command.lua b/src/luarocks/path_command.lua new file mode 100644 index 00000000..4e52bdc9 --- /dev/null +++ b/src/luarocks/path_command.lua @@ -0,0 +1,58 @@ + +--- Module implementing the LuaRocks "path" command. +-- Return the currently configured package path. +local path_command = {} + +local path = require("luarocks.path") +local cfg = require("luarocks.cfg") +local util = require("luarocks.util") +local deps = require("luarocks.deps") + +path_command.help_summary = "Return the currently configured package path." +path_command.help_arguments = "" +path_command.help = [[ +Returns the package path currently configured for this installation +of LuaRocks, formatted as shell commands to update LUA_PATH and +LUA_CPATH. (On Unix systems, you may run: eval `luarocks path`) +]] + +--- Driver function for "path" command. +-- @return boolean This function always succeeds. +function path_command.run(...) + local flags = util.parse_flags(...) + local deps_mode = deps.get_deps_mode(flags) + + local lr_path, lr_cpath = cfg.package_paths() + local bin_dirs = path.map_trees(deps_mode, path.deploy_bin_dir) + + if flags["lr-path"] then + util.printout(util.remove_path_dupes(lr_path, ';')) + return true + elseif flags["lr-cpath"] then + util.printout(util.remove_path_dupes(lr_cpath, ';')) + return true + elseif flags["lr-bin"] then + local lr_bin = util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator) + util.printout(util.remove_path_dupes(lr_bin, ';')) + return true + end + + if flags["append"] then + lr_path = package.path .. ";" .. lr_path + lr_cpath = package.cpath .. ";" .. lr_cpath + else + lr_path = lr_path.. ";" .. package.path + lr_cpath = lr_cpath .. ";" .. package.cpath + end + + util.printout(cfg.export_lua_path:format(util.remove_path_dupes(lr_path, ';'))) + util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(lr_cpath, ';'))) + if flags["bin"] then + table.insert(bin_dirs, 1, os.getenv("PATH")) + local lr_bin = util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator) + util.printout(cfg.export_path:format(lr_bin)) + end + return true +end + +return path_command -- cgit v1.2.3-55-g6feb