Prepare required data folder for integration tests
[prosody.git] / util / pluginloader.lua
blobaf0428c4856ab86a3ab44d4d9fed03733a5a8f81
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8 -- luacheck: ignore 113/CFG_PLUGINDIR
10 local dir_sep, path_sep = package.config:match("^(%S+)%s(%S+)");
11 local plugin_dir = {};
12 for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^"..path_sep.."]+") do
13 path = path..dir_sep; -- add path separator to path end
14 path = path:gsub(dir_sep..dir_sep.."+", dir_sep); -- coalesce multiple separaters
15 plugin_dir[#plugin_dir + 1] = path;
16 end
18 local io_open = io.open;
19 local envload = require "util.envload".envload;
21 local function load_file(names)
22 local file, err, path;
23 for i=1,#plugin_dir do
24 for j=1,#names do
25 path = plugin_dir[i]..names[j];
26 file, err = io_open(path);
27 if file then
28 local content = file:read("*a");
29 file:close();
30 return content, path;
31 end
32 end
33 end
34 return file, err;
35 end
37 local function load_resource(plugin, resource)
38 resource = resource or "mod_"..plugin..".lua";
39 local lua_version = _VERSION:match(" (.+)$");
40 local names = {
41 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua
42 "mod_"..plugin..dir_sep..resource; -- mod_hello/mod_hello.lua
43 plugin..dir_sep..resource; -- hello/mod_hello.lua
44 resource; -- mod_hello.lua
45 "share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep.."mod_"..plugin..dir_sep..resource;
48 return load_file(names);
49 end
51 local function load_code(plugin, resource, env)
52 local content, err = load_resource(plugin, resource);
53 if not content then return content, err; end
54 local path = err;
55 local f, err = envload(content, "@"..path, env);
56 if not f then return f, err; end
57 return f, path;
58 end
60 local function load_code_ext(plugin, resource, extension, env)
61 local content, err = load_resource(plugin, resource.."."..extension);
62 if not content then
63 content, err = load_resource(resource, resource.."."..extension);
64 if not content then
65 return content, err;
66 end
67 end
68 local path = err;
69 local f, err = envload(content, "@"..path, env);
70 if not f then return f, err; end
71 return f, path;
72 end
74 return {
75 load_file = load_file;
76 load_resource = load_resource;
77 load_code = load_code;
78 load_code_ext = load_code_ext;