Comparison

util/startup.lua @ 10175:4dac771ddf9f

util.startup: Improved how .set_plugindir updates prosody.paths.plugins, package.path and package.cpath
author João Duarte <jvsDuarte08@gmail.com>
date Fri, 26 Jul 2019 17:54:37 -0700
parent 10174:55dc8eb1e7d3
child 10192:761ff113c741
comparison
equal deleted inserted replaced
10174:55dc8eb1e7d3 10175:4dac771ddf9f
224 function startup.setup_datadir() 224 function startup.setup_datadir()
225 prosody.paths.data = config.get("*", "data_path") or CFG_DATADIR or "data"; 225 prosody.paths.data = config.get("*", "data_path") or CFG_DATADIR or "data";
226 end 226 end
227 227
228 function startup.setup_plugindir() 228 function startup.setup_plugindir()
229 --local lfs_currentdir = require "lfs".currentdir()
230 --local current_directory = lfs_currentdir
231 local custom_plugin_paths = config.get("*", "plugin_paths"); 229 local custom_plugin_paths = config.get("*", "plugin_paths");
232 local installer_plugin_path = config.get("*", "installer_plugin_path") or "custom_plugins"; 230 local installer_plugin_path = config.get("*", "installer_plugin_path") or "custom_plugins";
231 -- This variable separates different paths, like this "," here -> /usr;/home
233 local path_sep = package.config:sub(3,3); 232 local path_sep = package.config:sub(3,3);
233 -- This variable is the separator between directories, in a path, like the "/" here -> /home/path/to/somewhere
234 local dir_sep = package.config:sub(1,1);
234 if custom_plugin_paths then 235 if custom_plugin_paths then
235 -- path1;path2;path3;defaultpath... 236 -- path1;path2;path3;defaultpath...
236 -- luacheck: ignore 111 237 -- luacheck: ignore 111
237 CFG_PLUGINDIR = table.concat(custom_plugin_paths, path_sep)..path_sep..(CFG_PLUGINDIR or "plugins"); 238 CFG_PLUGINDIR = table.concat(custom_plugin_paths, path_sep)..path_sep..(CFG_PLUGINDIR or "plugins");
238 prosody.paths.plugins = CFG_PLUGINDIR; 239 prosody.paths.plugins = CFG_PLUGINDIR;
239 end 240 end
240 -- Checking if the folder exists. If it doesn't, we create it 241 -- Checks if installer_plugin_path is a relative paths and makes it an absolute path
242 if installer_plugin_path:sub(1,1) ~= "/" then
243 -- Works fine when executing prosody from source (configure and make only)
244 -- Probably wont be the best install directory, when using a package installation
245 local lfs_currentdir = require "lfs".currentdir();
246 local current_directory = lfs_currentdir;
247 -- Some normalization
248 installer_plugin_path = installer_plugin_path:gsub("^%.%"..dir_sep.."+", "");
249 installer_plugin_path = current_directory..dir_sep..installer_plugin_path;
250 end
251 -- Checking if the folder exists. If it doesn't, we create it, but we need permissions to do so
241 if os.execute('[ -d "'..installer_plugin_path..'" ]') ~= 0 then 252 if os.execute('[ -d "'..installer_plugin_path..'" ]') ~= 0 then
242 os.execute("mkdir "..installer_plugin_path) 253 os.execute("mkdir "..installer_plugin_path);
243 end 254 end
244 --[[if not string.find(package.path, current_directory..installer_plugin_path[path]) then 255 -- Developers may have add these custom paths to their LUA_PATH/LUA_CPATH variables, before running prosody
245 --os.execute("ls -la "..current_directory..path_sep..installer_plugin_paths[path]) 256 -- Therefore, I'll just check if the paths we are about to add aren't already at package.(path/cpath)
246 package.path = package.path..path_sep..current_directory..installer_plugin_path.."/?.lua"..path_sep..path_sep 257 if not string.match(package.path, installer_plugin_path) then
247 package.path = package.path..current_directory..installer_plugin_path.."/?/init.lua"..path_sep..path_sep 258 local lua_version = _VERSION:match(" (.+)$")
248 package.cpath = package.cpath..path_sep..current_directory..installer_plugin_path.."/?.lua" 259 -- I'm assuming there's good reason not to hard code any separator
249 package.cpath = package.cpath..path_sep..current_directory..installer_plugin_path.."/?/init.lua" 260 -- This next line is unnecessary, but I think it makes the code more readable and neat
250 end]] 261 local sub_path = dir_sep.."lua"..dir_sep..lua_version..dir_sep
262 package.path = package.path..path_sep..installer_plugin_path..dir_sep.."share"..sub_path.."?.lua";
263 package.path = package.path..path_sep..installer_plugin_path..dir_sep.."share"..sub_path.."?"..dir_sep.."init.lua";
264 package.cpath = package.cpath..path_sep..installer_plugin_path..dir_sep.."lib"..sub_path.."?.lua";
265 end
266 -- The commands using luarocks need the path to the directory that has the /share and /lib folders.
251 CFG_PLUGINDIR = installer_plugin_path..path_sep..(CFG_PLUGINDIR or "plugins"); 267 CFG_PLUGINDIR = installer_plugin_path..path_sep..(CFG_PLUGINDIR or "plugins");
252 prosody.paths.plugins = CFG_PLUGINDIR; 268 prosody.paths.plugins = CFG_PLUGINDIR;
253 end 269 end
254 270
255 function startup.chdir() 271 function startup.chdir()