Software /
code /
prosody
Annotate
util/pluginloader.lua @ 4185:3ff25f9adde7
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 | 4183:d899a847867e |
child | 5021:85b2689dbcfe |
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 |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
22 local function load_file(names) |
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 |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
25 for j=1,#names do |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
26 path = plugin_dir[i]..names[j]; |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
27 file, err = io_open(path); |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
28 if file then |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
29 local content = file:read("*a"); |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
30 file:close(); |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
31 return content, path; |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
32 end |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
33 end |
4171
b32e6bb12379
util.pluginloader: Add support for multiple plugin directories.
Waqas Hussain <waqas20@gmail.com>
parents:
3411
diff
changeset
|
34 end |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
35 return file, err; |
1359
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 end |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 |
4183
d899a847867e
util.pluginloader: Remove unused support for custom loaders, to simplify further refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
4181
diff
changeset
|
38 function load_resource(plugin, resource) |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
39 resource = resource or "mod_"..plugin..".lua"; |
2276
d9302be05f86
util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
40 |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
41 local names = { |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
42 "mod_"..plugin.."/"..plugin.."/"..resource; -- mod_hello/hello/mod_hello.lua |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
43 "mod_"..plugin.."/"..resource; -- mod_hello/mod_hello.lua |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
44 plugin.."/"..resource; -- hello/mod_hello.lua |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
45 resource; -- mod_hello.lua |
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
46 }; |
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
|
47 |
4185
3ff25f9adde7
util.pluginloader: Rewritten resource loading to be cleaner, and added support for prosody-modules directory layout. "/" in plugin names is no longer supported.
Waqas Hussain <waqas20@gmail.com>
parents:
4183
diff
changeset
|
48 return load_file(names); |
1359
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 end |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 function load_code(plugin, resource) |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 local content, err = load_resource(plugin, resource); |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 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
|
54 local path = err; |
2f4496f46da2
util.pluginloader: Return file path on success in pluginloader.load_code().
Waqas Hussain <waqas20@gmail.com>
parents:
4180
diff
changeset
|
55 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
|
56 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
|
57 return f, path; |
1359
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 |
015d624a2a71
util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 return _M; |