Comparison

util/pluginloader.lua @ 4154:3785a9bb7f11

util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 22 Feb 2011 21:47:38 +0500
parent 4152:4d52812d018d
child 5021:85b2689dbcfe
comparison
equal deleted inserted replaced
4153:676588aed2d7 4154:3785a9bb7f11
17 local io_open, os_time = io.open, os.time; 17 local io_open, os_time = io.open, os.time;
18 local loadstring, pairs = loadstring, pairs; 18 local loadstring, pairs = loadstring, pairs;
19 19
20 module "pluginloader" 20 module "pluginloader"
21 21
22 local function load_file(name) 22 local function load_file(names)
23 local file, err, path; 23 local file, err, path;
24 for i=1,#plugin_dir do 24 for i=1,#plugin_dir do
25 path = plugin_dir[i]..name; 25 for j=1,#names do
26 file, err = io_open(path); 26 path = plugin_dir[i]..names[j];
27 if file then break; end 27 file, err = io_open(path);
28 if file then
29 local content = file:read("*a");
30 file:close();
31 return content, path;
32 end
33 end
28 end 34 end
29 if not file then return file, err; end 35 return file, err;
30 local content = file:read("*a");
31 file:close();
32 return content, path;
33 end 36 end
34 37
35 function load_resource(plugin, resource) 38 function load_resource(plugin, resource)
36 local path, name = plugin:match("([^/]*)/?(.*)"); 39 resource = resource or "mod_"..plugin..".lua";
37 if name == "" then
38 if not resource then
39 resource = "mod_"..plugin..".lua";
40 end
41 40
42 local content, err = load_file(plugin.."/"..resource); 41 local names = {
43 if not content then content, err = load_file(resource); end 42 "mod_"..plugin.."/"..plugin.."/"..resource; -- mod_hello/hello/mod_hello.lua
44 43 "mod_"..plugin.."/"..resource; -- mod_hello/mod_hello.lua
45 return content, err; 44 plugin.."/"..resource; -- hello/mod_hello.lua
46 else 45 resource; -- mod_hello.lua
47 if not resource then 46 };
48 resource = "mod_"..name..".lua";
49 end
50 47
51 local content, err = load_file(plugin.."/"..resource); 48 return load_file(names);
52 if not content then content, err = load_file(path.."/"..resource); end
53
54 return content, err;
55 end
56 end 49 end
57 50
58 function load_code(plugin, resource) 51 function load_code(plugin, resource)
59 local content, err = load_resource(plugin, resource); 52 local content, err = load_resource(plugin, resource);
60 if not content then return content, err; end 53 if not content then return content, err; end