Module:Loader

require('Module:MultiRequire') local table, string, dpl, _ = multiRequire('Module:Table', 'Module:String', 'Module:DPL', 'Module:LibraryUtil') local require, pcall = require, pcall

local p = {} _G.loader = p p.registry = _G.loader.registry or {} local loadable = table.Set{ 'bit32', 'libraryUtil', 'ustring', 'luabit.hex', 'luabit.bit', } local stack = table.slice(string.split(debug.traceback(''), '\n\t'), 1, 3) local moduleName = (not stack[2]:match('mw.lua:487:') and stack[2] or stack[1]):match('^(.+):%d+:.+')

local function formatError(e, module, path) if type(e) ~= 'string' then return nil end if e:match('not found') then return ('Module %q was not found (in path %q)'):format(module, path) elseif e:match('loop') then return ('Loop or previous error loading module %q'):format(module) elseif e:match('^[Mm]odule:') then return ('Exception in loading module %q at line %s: %s'):format(module, e:match('^Module:%w+:(%d+)'), e:gsub('^Module:%w+:%d+:%s*', '')) end end

p.loadData = loadDataMulti

local function createRequireFunc(basePath) return function(...) local args = { ... }		local ret = {} local options local doUnpack = true if type(args[1]) == 'table' then args = args[1] doUnpack = false end if type(args[2]) == 'table' then options = args[2] end options = options or {} for i, path in forEachArgs({ 'string', 'table', required=1 }, ...) do			local oldPath = path assertTrue(path ~= '', 'Path may not be empty (in argument #%d)', 2, i)			path = p.resolvePath(path, basePath) if p.registry[path] then args[i] = p.registry[path] else local success, res = pcall(require, path) assertTrue(success, formatError(res, path, oldPath), 2) args[i] = res p.registry[path] = res end end if doUnpack then return unpack(args) else return args end end end

- -- function: .require(...modules: table |string[]) -- -- Loads the listed modules. Accepts relative paths - p.require = createRequireFunc(moduleName)

- -- function: .resolveRelativePath(relativePath: string, basePath: string) -- -- Resolves a relative path based on `basePath`. - function p.resolvePath(relativePath, basePath) checkType(1, relativePath, 'string') checkType(2, basePath, 'string', true) basePath = basePath or moduleName:gsub('^[Mm]odule:', '') assertTrue(relativePath ~= '', 'Path may not be empty', 2) if not loadable[relativePath] and not loadable[basePath] then relativePath = relativePath:gsub('^[Mm]odule:', '') basePath = basePath:gsub('^[Mm]odule:', '') end local stack = table.tableUtil(string.split(basePath, "/")) local parts = table.tableUtil(string.split(relativePath, "/")) local base = stack if parts[1]:match"~" or (not parts:some(function(_, v) return v:match('%.') end) and parts[1] ~= '') then parts[1] = parts[1]:gsub("~", '') return 'Module:'..parts:concat('/') end for i = 1, #parts, 1 do		local v = parts[i]:gsub('^%$$', base[1]) if v ~= "." then assertFalse((i > 1 and i < #parts) and v == "", "Invalid path %q: Path level/name must not be empty", 2, relativePath) if v == ".." then stack:pop elseif v ~= "" then stack:push(v) end else stack:pop end end return 'Module:'..table.concat(stack:map(function(v, i)		return v:gsub('^#$', stack[i-1] or stack[1]) end), '/'); end

- -- function: getModuleNames -- -- Lists all the availble modules to load. - function p.getModuleNames return dpl.list{ namespace='Module', } end

- -- function: getRegistry -- -- Gets the loader's registry - function p.getRegistry return p.registry end

- -- function: register(path: string, payload: function) -- -- Registers a module to the registry. - function p.register(path, payload) checkType(1, path, 'string') checkType(2, payload, 'function') assertTrue(path ~= '', 'path may not be empty', 2) local exports = {} path = path:gsub('^[Mm]odule:', '') local oldPath = path path = p.resolvePath(path) local success, res = pcall(payload, p.require, exports, {		path=path,	}) if res == nil then res = 'unknown error' else res = tostring(res) end

assertTrue(success, 'Exception in registring module %q%s: %s', 2, path, res:match('^.-:%d+:') and (' in %s at line %s'):format(res:match('^(.-):%d+:'), res:match('^.-:(%d+):')) or , res:gsub('^(.-):%d+:%s*', )) p.registry[path] = exports return exports end

return p