refine module support and package loading

This commit is contained in:
2025-06-24 01:48:30 -04:00
parent eba21a8021
commit 6bd6db3c02
6 changed files with 200 additions and 101 deletions

View File

@ -0,0 +1,55 @@
local spec_man = require("dep.spec")
local packager = require("dep.package")
---@class module
---@field name string name 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>"
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
-- 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