From 74b0087d28c735dc5440c4587be3085abbd05257 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 25 Sep 2012 17:17:27 -0300 Subject: factor out tree map operation and use it in "luarocks path" command, providing proper bin paths. --- src/luarocks/cfg.lua | 4 ++-- src/luarocks/dir.lua | 4 ++++ src/luarocks/fs/lua.lua | 40 ++++++++++++++++++---------------------- src/luarocks/manif_core.lua | 29 +++++++---------------------- src/luarocks/path.lua | 28 +++++++++++++++++++++++++++- src/luarocks/util.lua | 2 ++ 6 files changed, 60 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index b2ed2a96..330fabca 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -277,7 +277,7 @@ if detected.windows then lib = { "?.dll", "lib?.dll" }, include = { "?.h" } } - defaults.export_path = "SET PATH=%s;%s" + defaults.export_path = "SET PATH=%s" defaults.export_path_separator = ";" defaults.export_lua_path = "SET LUA_PATH=%s" defaults.export_lua_cpath = "SET LUA_CPATH=%s" @@ -322,7 +322,7 @@ if detected.unix then lib = { "lib?.so", "lib?.so.*" }, include = { "?.h" } } - defaults.export_path = "export PATH='%s:%s'" + defaults.export_path = "export PATH='%s'" defaults.export_path_separator = ":" defaults.export_lua_path = "export LUA_PATH='%s'" defaults.export_lua_cpath = "export LUA_CPATH='%s'" diff --git a/src/luarocks/dir.lua b/src/luarocks/dir.lua index 3de9e241..c2309ff1 100644 --- a/src/luarocks/dir.lua +++ b/src/luarocks/dir.lua @@ -68,3 +68,7 @@ function split_url(url) end return protocol, pathname end + +function normalize(name) + return name:gsub("\\", "/"):gsub("(.)/*$", "%1") +end diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index c413ccb3..eba61214 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -43,10 +43,6 @@ function Q(arg) return "'" .. arg:gsub("\\", "\\\\"):gsub("'", "'\\''") .. "'" end -local function normalize(name) - return name:gsub("\\", "/"):gsub("(.)/*$", "%1") -end - --- Test is file/dir is writable. -- Warning: testing if a file/dir is writable does not guarantee -- that it will remain writable and therefore it is no replacement @@ -55,7 +51,7 @@ end -- @return boolean: true if file exists, false otherwise. function is_writable(file) assert(file) - file = normalize(file) + file = dir.normalize(file) local result if fs.is_dir(file) then local file2 = dir.path(file, '.tmpluarockstestwritable') @@ -77,7 +73,7 @@ end -- @return string or nil: name of temporary directory or nil on failure. function make_temp_dir(name) assert(type(name) == "string") - name = normalize(name) + name = dir.normalize(name) local temp_dir = (os.getenv("TMP") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) if fs.make_dir(temp_dir) then @@ -110,7 +106,7 @@ end -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not -- or if it could not perform the check for any reason. function check_md5(file, md5sum) - file = normalize(file) + file = dir.normalize(file) local computed = fs.get_md5(file) if not computed then return false @@ -156,7 +152,7 @@ end -- @param d string: The directory to switch to. function change_dir(d) table.insert(dir_stack, lfs.currentdir()) - d = normalize(d) + d = dir.normalize(d) lfs.chdir(d) end @@ -187,7 +183,7 @@ end -- @return boolean: true on success, false on failure. function make_dir(directory) assert(type(directory) == "string") - directory = normalize(directory) + directory = dir.normalize(directory) local path = nil if directory:sub(2, 2) == ":" then path = directory:sub(1, 2) @@ -217,7 +213,7 @@ end -- @param d string: pathname of directory to remove. function remove_dir_if_empty(d) assert(d) - d = normalize(d) + d = dir.normalize(d) lfs.rmdir(d) end @@ -227,7 +223,7 @@ end -- @param d string: pathname of directory to remove. function remove_dir_tree_if_empty(d) assert(d) - d = normalize(d) + d = dir.normalize(d) for i=1,10 do lfs.rmdir(d) d = dir.dir_name(d) @@ -243,8 +239,8 @@ end -- plus an error message. function copy(src, dest, perms) assert(src and dest) - src = normalize(src) - dest = normalize(dest) + src = dir.normalize(src) + dest = dir.normalize(dest) local destmode = lfs.attributes(dest, "mode") if destmode == "directory" then dest = dir.path(dest, dir.base_name(src)) @@ -296,8 +292,8 @@ end -- plus an error message. function copy_contents(src, dest) assert(src and dest) - src = normalize(src) - dest = normalize(dest) + src = dir.normalize(src) + dest = dir.normalize(dest) assert(lfs.attributes(src, "mode") == "directory") for file in lfs.dir(src) do @@ -338,7 +334,7 @@ end -- @param name string: Pathname of source -- @return boolean: true on success, false on failure. function delete(name) - name = normalize(name) + name = dir.normalize(name) return recursive_delete(name) or false end @@ -352,7 +348,7 @@ function list_dir(at) if not at then at = fs.current_dir() end - at = normalize(at) + at = dir.normalize(at) if not fs.is_dir(at) then return {} end @@ -393,7 +389,7 @@ function find(at) if not at then at = fs.current_dir() end - at = normalize(at) + at = dir.normalize(at) if not fs.is_dir(at) then return {} end @@ -407,7 +403,7 @@ end -- @return boolean: true if file exists, false otherwise. function exists(file) assert(file) - file = normalize(file) + file = dir.normalize(file) return type(lfs.attributes(file)) == "table" end @@ -416,7 +412,7 @@ end -- @return boolean: true if it is a directory, false otherwise. function is_dir(file) assert(file) - file = normalize(file) + file = dir.normalize(file) return lfs.attributes(file, "mode") == "directory" end @@ -425,12 +421,12 @@ end -- @return boolean: true if it is a file, false otherwise. function is_file(file) assert(file) - file = normalize(file) + file = dir.normalize(file) return lfs.attributes(file, "mode") == "file" end function set_time(file, time) - file = normalize(file) + file = dir.normalize(file) return lfs.touch(file, time) end diff --git a/src/luarocks/manif_core.lua b/src/luarocks/manif_core.lua index ed6cac07..6af99fe7 100644 --- a/src/luarocks/manif_core.lua +++ b/src/luarocks/manif_core.lua @@ -63,30 +63,15 @@ function get_versions(name, use_trees) assert(type(name) == "string") assert(type(use_trees) == "string") - local manifest - - if use_trees == "one" then - manifest = load_local_manifest(cfg.rocks_dir) - elseif use_trees == "all" or use_trees == "order" then - manifest = {} - local use = false - if use_trees == "all" then - use = true - end - for _, tree in ipairs(cfg.rocks_trees) do - if tree == cfg.rocks_dir then - use = true - end - if use then - local loaded = load_local_manifest(path.rocks_dir(tree)) - if loaded then - util.deep_merge(manifest, loaded) - end - end + local manifest = {} + path.map_trees(use_trees, function(tree) + local loaded = load_local_manifest(path.rocks_dir(tree)) + if loaded then + util.deep_merge(manifest, loaded) end - end + end) - local item = manifest and manifest.repository[name] + local item = next(manifest) and manifest.repository[name] if item then return util.keys(item) end diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index bbd928a0..17d9d52b 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -7,6 +7,7 @@ module("luarocks.path", package.seeall) 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 = "" @@ -305,6 +306,27 @@ function use_tree(tree) cfg.deploy_lib_dir = deploy_lib_dir(tree) end +function map_trees(use_trees, fn, ...) + local result = {} + if use_trees == "one" then + table.insert(result, fn(cfg.root_dir, ...)) + elseif use_trees == "all" or use_trees == "order" then + local use = false + if use_trees == "all" then + use = true + end + for _, tree in ipairs(cfg.rocks_trees) do + if dir.normalize(tree) == dir.normalize(cfg.root_dir) then + use = true + end + if use then + table.insert(result, fn(tree, ...)) + end + end + end + return result +end + --- Return the pathname of the file that would be loaded for a module, indexed. -- @param module_name string: module name (eg. "socket.core") -- @param name string: name of the package (eg. "luasocket") @@ -352,10 +374,14 @@ end -- @return boolean This function always succeeds. function run(...) local flags = util.parse_flags(...) + local deps_mode = deps.flags_to_deps_mode(flags) + util.printout(cfg.export_lua_path:format(util.remove_path_dupes(package.path, ';'))) util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(package.cpath, ';'))) if flags["bin"] then - util.printout(cfg.export_path:format(util.remove_path_dupes(os.getenv("PATH"), cfg.export_path_separator), cfg.deploy_bin_dir)) + local bin_dirs = map_trees(deps_mode, deploy_bin_dir) + table.insert(bin_dirs, 1, os.getenv("PATH")) + util.printout(cfg.export_path:format(util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator))) end return true end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 1a1cccf9..46802255 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -331,6 +331,8 @@ end -- @param list string: A path string (from $PATH or package.path) -- @param sep string: The separator function remove_path_dupes(list, sep) + assert(type(list) == "string") + assert(type(sep) == "string") local parts = split_string(list, sep) local final, entries = {}, {} for _, part in ipairs(parts) do -- cgit v1.2.3-55-g6feb