Annotate

util/pluginloader.lua @ 4181:2f4496f46da2

util.pluginloader: Return file path on success in pluginloader.load_code().
author Waqas Hussain <waqas20@gmail.com>
date Sun, 20 Feb 2011 20:03:43 +0500
parent 4180:cd3fb3393e13
child 4152:4d52812d018d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
8
4171
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
9 local dir_sep, path_sep = package.config:match("^(%S+)%s(%S+)");
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
10 local plugin_dir = {};
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
11 for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^"..path_sep.."]+") do
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
12 path = path..dir_sep; -- add path separator to path end
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
13 path = path:gsub(dir_sep..dir_sep.."+", dir_sep); -- coalesce multiple separaters
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
14 plugin_dir[#plugin_dir + 1] = path;
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
15 end
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
17 local io_open, os_time = io.open, os.time;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
18 local loadstring, pairs = loadstring, pairs;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
19
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 module "pluginloader"
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function load_file(name)
4180
cd3fb3393e13 util.pluginloader: Return full file path from internal file loader on success, not just the name.
Waqas Hussain <waqas20@gmail.com>
parents: 4171
diff changeset
23 local file, err, path;
4171
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
24 for i=1,#plugin_dir do
4180
cd3fb3393e13 util.pluginloader: Return full file path from internal file loader on success, not just the name.
Waqas Hussain <waqas20@gmail.com>
parents: 4171
diff changeset
25 path = plugin_dir[i]..name;
cd3fb3393e13 util.pluginloader: Return full file path from internal file loader on success, not just the name.
Waqas Hussain <waqas20@gmail.com>
parents: 4171
diff changeset
26 file, err = io_open(path);
4171
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
27 if file then break; end
b32e6bb12379 util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents: 3411
diff changeset
28 end
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 if not file then return file, err; end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local content = file:read("*a");
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 file:close();
4180
cd3fb3393e13 util.pluginloader: Return full file path from internal file loader on success, not just the name.
Waqas Hussain <waqas20@gmail.com>
parents: 4171
diff changeset
32 return content, path;
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
35 function load_resource(plugin, resource, loader)
3410
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
36 local path, name = plugin:match("([^/]*)/?(.*)");
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
37 if name == "" then
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
38 if not resource then
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
39 resource = "mod_"..plugin..".lua";
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
40 end
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
41 loader = loader or load_file;
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
42
3410
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
43 local content, err = loader(plugin.."/"..resource);
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
44 if not content then content, err = loader(resource); end
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
45 -- TODO add support for packed plugins
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
46
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
47 return content, err;
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
48 else
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
49 if not resource then
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
50 resource = "mod_"..name..".lua";
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
51 end
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
52 loader = loader or load_file;
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
53
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
54 local content, err = loader(plugin.."/"..resource);
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
55 if not content then content, err = loader(path.."/"..resource); end
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
56 -- TODO add support for packed plugins
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
57
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
58 return content, err;
32b018eeeb3b util.pluginloader: Fix loading of plugins, plugin libraries and resources in subfolders (e.g., when loading 'a/b', load 'a/mod_b.lua', and not 'mod_a/b.lua').
Waqas Hussain <waqas20@gmail.com>
parents: 3233
diff changeset
59 end
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function load_code(plugin, resource)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local content, err = load_resource(plugin, resource);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 if not content then return content, err; end
4181
2f4496f46da2 util.pluginloader: Return file path on success in pluginloader.load_code().
Waqas Hussain <waqas20@gmail.com>
parents: 4180
diff changeset
65 local path = err;
2f4496f46da2 util.pluginloader: Return file path on success in pluginloader.load_code().
Waqas Hussain <waqas20@gmail.com>
parents: 4180
diff changeset
66 local f, err = loadstring(content, "@"..path);
2f4496f46da2 util.pluginloader: Return file path on success in pluginloader.load_code().
Waqas Hussain <waqas20@gmail.com>
parents: 4180
diff changeset
67 if not f then return f, err; end
2f4496f46da2 util.pluginloader: Return file path on success in pluginloader.load_code().
Waqas Hussain <waqas20@gmail.com>
parents: 4180
diff changeset
68 return f, path;
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 return _M;