Mercurial > prosody-hg
annotate loader.lua @ 14064:76ece4dd9782
loader: Move all path setup here from all executables
So it is done in the same way for all the executables, with less code
duplication.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 09 Feb 2026 01:57:40 +0100 |
| parents | ef586363d90f |
| children | 50b8d732c905 |
| rev | line source |
|---|---|
|
12948
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
1 -- Allow for both require"util.foo" and require"prosody.util.foo" for a |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
2 -- transition period while we update all require calls. |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
3 |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
4 if (...) == "prosody.loader" then |
|
14064
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
5 local function is_relative(path) |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
6 local path_sep = package.config:sub(1,1); |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
7 return ((path_sep == "/" and path:sub(1,1) ~= "/") |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
8 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\"))) |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
9 end |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
10 local function filter_relative_paths(path) |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
11 if is_relative(path) then return ""; end |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
12 end |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
13 local function sanitise_paths(paths) |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
14 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";")); |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
15 end |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
16 |
| 14062 | 17 -- For require"util.foo" also look in paths equivalent to "prosody.util.foo" |
|
14064
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
18 package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2")); |
|
76ece4dd9782
loader: Move all path setup here from all executables
Kim Alvefur <zash@zash.se>
parents:
14062
diff
changeset
|
19 package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2")); |
|
12948
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
20 else |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
21 -- When requiring "prosody.x", also look for "x" |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
22 for i = #package.searchers, 1, -1 do |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
23 local search = package.searchers[i]; |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
24 table.insert(package.searchers, i, function(module_name) |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
25 local lib = module_name:match("^prosody%.(.*)$"); |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
26 if lib then |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
27 return search(lib); |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
28 end |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
29 end) |
|
29983f09c913
prosody.loader: Incorporate search path rewrite patch from Debian packages
Kim Alvefur <zash@zash.se>
parents:
12947
diff
changeset
|
30 end |
|
12947
14a44b1a51d0
prosody.loader: Allow loading modules under 'prosody' namespace (#1223)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
|
12949
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
32 |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
33 -- Look for already loaded module with or without prefix |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
34 setmetatable(package.loaded, { |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
35 __index = function(loaded, module_name) |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
36 local suffix = module_name:match("^prosody%.(.*)$"); |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
37 if suffix then |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
38 return rawget(loaded, suffix); |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
39 end |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
40 local prefixed = rawget(loaded, "prosody." .. module_name); |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
41 if prefixed ~= nil then |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
42 return prefixed; |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
43 end |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
44 end; |
|
2f61ebcf37c0
prosody.loader: Ensure already loaded modules are found in old and new namespaces
Kim Alvefur <zash@zash.se>
parents:
12948
diff
changeset
|
45 }) |
