From 72d80a35bee56bd8f8002b199db37f89c1441c29 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 13 Jul 2019 17:12:58 -0300 Subject: path: keep order of existing entries in PATH Implements suggestion by @FSMaxB: > Add an additional flag to util.cleanup_path that specifies if the cleanup > happens from the right or from the left. If append is true, clean up from the > left, otherwise clean up from the right. Fixes #763. --- src/luarocks/cmd/path.lua | 6 +++--- src/luarocks/core/util.lua | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd/path.lua b/src/luarocks/cmd/path.lua index 9046cd2e..bb383ad9 100644 --- a/src/luarocks/cmd/path.lua +++ b/src/luarocks/cmd/path.lua @@ -60,10 +60,10 @@ function path_cmd.command(flags) local lpath_var, lcpath_var = util.lua_path_variables() - util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) - util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) + util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version, flags["append"]))) + util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version, flags["append"]))) if not flags["no-bin"] then - util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep))) + util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep, nil, flags["append"]))) end return true end diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 1499dc62..6a306026 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua @@ -165,13 +165,23 @@ end -- @param list string: A path string (from $PATH or package.path) -- @param sep string: The separator -- @param lua_version (optional) string: The Lua version to use. -function util.cleanup_path(list, sep, lua_version) +-- @param keep_last (optional) if true, keep last occurrence in case +-- of duplicates; otherwise keep first occurrence. The default is false. +function util.cleanup_path(list, sep, lua_version, keep_last) assert(type(list) == "string") assert(type(sep) == "string") local parts = util.split_string(list, sep) local final, entries = {}, {} - for _, part in ipairs(parts) do - part = part:gsub("//", "/") + local start, stop, step + + if keep_last then + start, stop, step = 1, #parts, 1 + else + start, stop, step = #parts, 1, -1 + end + + for i = start, stop, step do + local part = parts[i]:gsub("//", "/") if lua_version then part = part:gsub("/lua/([%d.]+)/", function(part_version) if part_version:sub(1, #lua_version) ~= lua_version then @@ -180,7 +190,8 @@ function util.cleanup_path(list, sep, lua_version) end) end if not entries[part] then - table.insert(final, part) + local at = keep_last and #final+1 or 1 + table.insert(final, at, part) entries[part] = true end end -- cgit v1.2.3-55-g6feb