74 lines
1.9 KiB
Lua
74 lines
1.9 KiB
Lua
local logger = require('dep.log')
|
|
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 disable boolean weather to disable all the packages inside the module
|
|
---@field path string path to the module
|
|
---@field mod table the module
|
|
---@field packages package[] all packages registed from 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)
|
|
overrides = overrides or {}
|
|
|
|
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
|
|
logger:log("error", "failed to load module: %s", vim.inspect(o.mod))
|
|
return false
|
|
end
|
|
end
|
|
o.name = o.mod.name or o.name
|
|
o.desc = o.mod.desc or o.desc
|
|
|
|
-- ensure the overrides are properly set
|
|
overrides = vim.tbl_extend("force", overrides, {
|
|
disable = o.mod.disable or overrides.disable
|
|
})
|
|
|
|
-- 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
|
|
logger:log("error", "%s <- %s", err, o.name)
|
|
return false
|
|
end
|
|
|
|
-- ensure that the module contains the packages that it's created
|
|
self.packages = err
|
|
|
|
return self
|
|
end
|
|
|
|
return module
|