Files
dep/lua/dep/modules/module.lua

59 lines
1.4 KiB
Lua

local spec_man = require("dep.spec")
local packager = require("dep.package")
---@class module
---@field name string name of the module
---@field desc string description of the module
---@field path string path to the module
---@field mod table the module
local module = {}
--- Initialize a module
---@param self table?
---@param modpath string path to the module
---@param prefix string? the prefix to all modules
---@param overrides spec? a module override
---@return module|false module false on failure to load module
---@nodiscard
function module:new(modpath, prefix, overrides)
local ok, err
local o = {}
self = {}
self.__index = self
setmetatable(o, self)
o.name = "<unnamed module>"
o.desc = "<undescribed module>"
if type(modpath) == "string" then
if prefix ~= nil then
if prefix:sub(#prefix) ~= "." and modpath:sub(1, 2) ~= "." then
modpath = "."..modpath
end
o.path = prefix..modpath
else
o.path = modpath
end
o.name = modpath
ok, o.mod = pcall(require, o.path)
if not ok then
return false
end
end
o.name = o.mod.name or o.name
o.desc = o.mod.desc or o.desc
-- allow a module to be a spec
if spec_man.check(o.mod, true) ~= false then
o.mod = { o.mod }
end
ok, err = pcall(packager.register_speclist, o.mod, overrides)
if not ok then
error(string.format("%s <- %s", err, o.name))
end
return self
end
return module