From f88902378e94729c67231a018ec0d9163b02b549 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 19 Sep 2012 20:46:02 -0300 Subject: Add --bin flag for `luarocks path`, and provide cleaner variable output. Closes #76. --- src/luarocks/cfg.lua | 4 ++++ src/luarocks/path.lua | 8 ++++++-- src/luarocks/util.lua | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index a8475bfe..f68380f1 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -274,6 +274,8 @@ if detected.windows then lib = { "?.dll", "lib?.dll" }, include = { "?.h" } } + defaults.export_path = "SET PATH=%s;%s" + defaults.export_path_separator = ";" defaults.export_lua_path = "SET LUA_PATH=%s" defaults.export_lua_cpath = "SET LUA_CPATH=%s" defaults.local_cache = home.."/cache/luarocks" @@ -317,6 +319,8 @@ if detected.unix then lib = { "lib?.so", "lib?.so.*" }, include = { "?.h" } } + defaults.export_path = "export PATH='%s:%s'" + defaults.export_path_separator = ":" defaults.export_lua_path = "export LUA_PATH='%s'" defaults.export_lua_cpath = "export LUA_CPATH='%s'" defaults.local_cache = home.."/.cache/luarocks" diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index 03ed1b90..1c94b639 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -308,8 +308,12 @@ end --- Driver function for "path" command. -- @return boolean This function always succeeds. function run(...) - util.printout(cfg.export_lua_path:format(package.path)) - util.printout(cfg.export_lua_cpath:format(package.cpath)) + local flags = util.parse_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)) + end return true end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 2955be50..1a1cccf9 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -326,6 +326,22 @@ function split_string(str, delim, maxNb) return result end +--- Remove repeated entries from a path-style string. +-- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". +-- @param list string: A path string (from $PATH or package.path) +-- @param sep string: The separator +function remove_path_dupes(list, sep) + local parts = split_string(list, sep) + local final, entries = {}, {} + for _, part in ipairs(parts) do + if not entries[part] then + table.insert(final, part) + entries[part] = true + end + end + return table.concat(final, sep) +end + --[[ Author: Julio Manuel Fernandez-Diaz Date: January 12, 2007 -- cgit v1.2.3-55-g6feb