util.encodings: Spell out all IDNA 2008 options ICU has
[prosody.git] / util / paths.lua
blobc225108a88c58a9f18bea1dad5ded55f615a6d83
1 local t_concat = table.concat;
3 local path_sep = package.config:sub(1,1);
5 local path_util = {}
7 -- Helper function to resolve relative paths (needed by config)
8 function path_util.resolve_relative_path(parent_path, path)
9 if path then
10 -- Some normalization
11 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
12 path = path:gsub("^%.%"..path_sep.."+", "");
14 local is_relative;
15 if path_sep == "/" and path:sub(1,1) ~= "/" then
16 is_relative = true;
17 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
18 is_relative = true;
19 end
20 if is_relative then
21 return parent_path..path_sep..path;
22 end
23 end
24 return path;
25 end
27 -- Helper function to convert a glob to a Lua pattern
28 function path_util.glob_to_pattern(glob)
29 return "^"..glob:gsub("[%p*?]", function (c)
30 if c == "*" then
31 return ".*";
32 elseif c == "?" then
33 return ".";
34 else
35 return "%"..c;
36 end
37 end).."$";
38 end
40 function path_util.join(...)
41 return t_concat({...}, path_sep);
42 end
44 function path_util.complement_lua_path(installer_plugin_path)
45 -- Checking for duplicates
46 -- The commands using luarocks need the path to the directory that has the /share and /lib folders.
47 local lua_version = _VERSION:match(" (.+)$");
48 local lua_path_sep = package.config:sub(3,3);
49 local dir_sep = package.config:sub(1,1);
50 local sub_path = dir_sep.."lua"..dir_sep..lua_version..dir_sep;
51 if not string.match(package.path, installer_plugin_path) then
52 package.path = package.path..lua_path_sep..installer_plugin_path..dir_sep.."share"..sub_path.."?.lua";
53 package.path = package.path..lua_path_sep..installer_plugin_path..dir_sep.."share"..sub_path.."?"..dir_sep.."init.lua";
54 end
55 if not string.match(package.path, installer_plugin_path) then
56 package.cpath = package.cpath..lua_path_sep..installer_plugin_path..dir_sep.."lib"..sub_path.."?.so";
57 end
58 end
60 return path_util;