summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2019-07-13 17:12:58 -0300
committerHisham Muhammad <hisham@gobolinux.org>2019-07-17 20:30:25 -0300
commit72d80a35bee56bd8f8002b199db37f89c1441c29 (patch)
treeb3da27263aa3c9a20ca860753791d2ae15496cd6 /src
parent526664e6e54da0f129621ea6df355767259a103a (diff)
downloadluarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.tar.gz
luarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.tar.bz2
luarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/cmd/path.lua6
-rw-r--r--src/luarocks/core/util.lua19
2 files changed, 18 insertions, 7 deletions
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)
60 60
61 local lpath_var, lcpath_var = util.lua_path_variables() 61 local lpath_var, lcpath_var = util.lua_path_variables()
62 62
63 util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) 63 util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version, flags["append"])))
64 util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) 64 util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version, flags["append"])))
65 if not flags["no-bin"] then 65 if not flags["no-bin"] then
66 util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep))) 66 util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep, nil, flags["append"])))
67 end 67 end
68 return true 68 return true
69end 69end
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
165-- @param list string: A path string (from $PATH or package.path) 165-- @param list string: A path string (from $PATH or package.path)
166-- @param sep string: The separator 166-- @param sep string: The separator
167-- @param lua_version (optional) string: The Lua version to use. 167-- @param lua_version (optional) string: The Lua version to use.
168function util.cleanup_path(list, sep, lua_version) 168-- @param keep_last (optional) if true, keep last occurrence in case
169-- of duplicates; otherwise keep first occurrence. The default is false.
170function util.cleanup_path(list, sep, lua_version, keep_last)
169 assert(type(list) == "string") 171 assert(type(list) == "string")
170 assert(type(sep) == "string") 172 assert(type(sep) == "string")
171 local parts = util.split_string(list, sep) 173 local parts = util.split_string(list, sep)
172 local final, entries = {}, {} 174 local final, entries = {}, {}
173 for _, part in ipairs(parts) do 175 local start, stop, step
174 part = part:gsub("//", "/") 176
177 if keep_last then
178 start, stop, step = 1, #parts, 1
179 else
180 start, stop, step = #parts, 1, -1
181 end
182
183 for i = start, stop, step do
184 local part = parts[i]:gsub("//", "/")
175 if lua_version then 185 if lua_version then
176 part = part:gsub("/lua/([%d.]+)/", function(part_version) 186 part = part:gsub("/lua/([%d.]+)/", function(part_version)
177 if part_version:sub(1, #lua_version) ~= lua_version then 187 if part_version:sub(1, #lua_version) ~= lua_version then
@@ -180,7 +190,8 @@ function util.cleanup_path(list, sep, lua_version)
180 end) 190 end)
181 end 191 end
182 if not entries[part] then 192 if not entries[part] then
183 table.insert(final, part) 193 local at = keep_last and #final+1 or 1
194 table.insert(final, at, part)
184 entries[part] = true 195 entries[part] = true
185 end 196 end
186 end 197 end