Comparison

util/pluginloader.lua @ 12251:21ed12cfe300

util.pluginloader: Support for a per-file load filter Load filters can choose to block the loading of certain files, and optionally return some metadata about the loaded file.
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Feb 2022 12:56:52 +0000
parent 12250:e157e5c79daa
child 12255:a3ad9cf740d6
comparison
equal deleted inserted replaced
12250:e157e5c79daa 12251:21ed12cfe300
22 local pluginloader_methods = {}; 22 local pluginloader_methods = {};
23 local pluginloader_mt = { __index = pluginloader_methods }; 23 local pluginloader_mt = { __index = pluginloader_methods };
24 24
25 function pluginloader_methods:load_file(names) 25 function pluginloader_methods:load_file(names)
26 local file, err, path; 26 local file, err, path;
27 local load_filter_cb = self._options.load_filter_cb;
27 for i=1,#plugin_dir do 28 for i=1,#plugin_dir do
28 for j=1,#names do 29 for j=1,#names do
29 path = plugin_dir[i]..names[j]; 30 path = plugin_dir[i]..names[j];
30 file, err = io_open(path); 31 file, err = io_open(path);
31 if file then 32 if file then
32 local content = file:read("*a"); 33 local content = file:read("*a");
33 file:close(); 34 file:close();
34 return content, path; 35 local metadata;
36 if load_filter_cb then
37 path, content, metadata = load_filter_cb(path, content);
38 end
39 if content and path then
40 return content, path, metadata;
41 end
35 end 42 end
36 end 43 end
37 end 44 end
38 return file, err; 45 return file, err;
39 end 46 end
40 47
41 function pluginloader_methods:load_resource(plugin, resource) 48 function pluginloader_methods:load_resource(plugin, resource)
49 resource = resource or "mod_"..plugin..".lua";
42 local names = { 50 local names = {
43 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua 51 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua
44 "mod_"..plugin..dir_sep..resource; -- mod_hello/mod_hello.lua 52 "mod_"..plugin..dir_sep..resource; -- mod_hello/mod_hello.lua
45 plugin..dir_sep..resource; -- hello/mod_hello.lua 53 plugin..dir_sep..resource; -- hello/mod_hello.lua
46 resource; -- mod_hello.lua 54 resource; -- mod_hello.lua
50 58
51 return self:load_file(names); 59 return self:load_file(names);
52 end 60 end
53 61
54 function pluginloader_methods:load_code(plugin, resource, env) 62 function pluginloader_methods:load_code(plugin, resource, env)
55 local content, err = load_resource(plugin, resource); 63 local content, err, metadata = self:load_resource(plugin, resource);
56 if not content then return content, err; end 64 if not content then return content, err; end
57 local path = err; 65 local path = err;
58 local f, err = envload(content, "@"..path, env); 66 local f, err = envload(content, "@"..path, env);
59 if not f then return f, err; end 67 if not f then return f, err; end
60 return f, path; 68 return f, path, metadata;
61 end 69 end
62 70
63 function pluginloader_methods:load_code_ext(plugin, resource, extension, env) 71 function pluginloader_methods:load_code_ext(plugin, resource, extension, env)
64 local content, err = load_resource(plugin, resource.."."..extension); 72 local content, err, metadata = self:load_resource(plugin, resource.."."..extension);
65 if not content and extension == "lib.lua" then 73 if not content and extension == "lib.lua" then
66 content, err = load_resource(plugin, resource..".lua"); 74 content, err, metadata = self:load_resource(plugin, resource..".lua");
67 end 75 end
68 if not content then 76 if not content then
69 content, err = load_resource(resource, resource.."."..extension); 77 content, err, metadata = self:load_resource(resource, resource.."."..extension);
70 if not content then 78 if not content then
71 return content, err; 79 return content, err;
72 end 80 end
73 end 81 end
74 local path = err; 82 local path = err;