2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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
;
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
25 path
= plugin_dir
[i
]..names
[j
];
26 file
, err
= io_open(path
);
28 local content
= file
:read("*a");
37 local function load_resource(plugin
, resource
)
38 resource
= resource
or "mod_"..plugin
..".lua";
39 local lua_version
= _VERSION
:match(" (.+)$");
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
);
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
55 local f
, err
= envload(content
, "@"..path
, env
);
56 if not f
then return f
, err
; end
60 local function load_code_ext(plugin
, resource
, extension
, env
)
61 local content
, err
= load_resource(plugin
, resource
.."."..extension
);
63 content
, err
= load_resource(resource
, resource
.."."..extension
);
69 local f
, err
= envload(content
, "@"..path
, env
);
70 if not f
then return f
, err
; end
75 load_file
= load_file
;
76 load_resource
= load_resource
;
77 load_code
= load_code
;
78 load_code_ext
= load_code_ext
;