Skip to content

Commit

Permalink
feat: plugin's deps and requires allow false and nil item, they will …
Browse files Browse the repository at this point in the history
…be ignored
  • Loading branch information
adoyle-h committed Dec 4, 2022
1 parent f318308 commit 0fa4331
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lua/one/plugin-manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,17 @@ local function setupUserPlugins(optPlugins)
end

for i, p in pairs(optPlugins or {}) do
if not p then goto continue end

local ok, plugOpts = xpcall(normalizeOpts, debug.traceback, p)
if not ok then
notify(string.format('Invalid user plugin at opts.plugins[%s]. Reason: %s', i, plugOpts), 'error')
end

userPlugins[plugOpts.id] = plugOpts
userPluginList[#userPluginList + 1] = plugOpts -- For keeping plugins order

::continue::
end

return userPluginList
Expand Down
20 changes: 16 additions & 4 deletions lua/one/plugin-manager/use-plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,34 @@ local function usePlug(pm, loadPlug, repo, opts)
-- Must load dependent plugins first, then load current plugin.
-- If current plugin is disabled, no need to load dependent plugins.
if not opts.disable then
for index, dep in pairs(opts.requires or {}) do
local requires = {}
for _, dep in pairs(opts.requires or {}) do
if not dep then goto continue end

local depPlug = usePlug(pm, loadPlug, dep)
opts.requires[index] = depPlug
requires[#requires + 1] = depPlug

if depPlug.disable then
opts.disable = true
opts.reason = string.format('Its required plugin "%s" is disabled', depPlug.id)
end

::continue::
end
opts.requires = requires
end

if not opts.disable then
for index, dep in pairs(opts.deps or {}) do
local deps = {}
for _, dep in pairs(opts.deps or {}) do
if not dep then goto continue end

local depPlug = usePlug(pm, loadPlug, dep)
opts.deps[index] = depPlug
deps[#deps + 1] = depPlug

::continue::
end
opts.deps = deps
end

plugMap[id] = opts
Expand Down
23 changes: 21 additions & 2 deletions lua/one/plugin-manager/use-plug_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,34 @@ describe('usePlug()', function()
assert.are.same('g/g2', plugs[8].repo)
assert.are.same('g/g', plugs[9].repo)

assert.are.same({ id = 'g/h1', repo = 'g/h1' }, plugs[10])
assert.are.same({ _normalized = true, id = 'g/h1', repo = 'g/h1' }, plugs[10])

assert.are.same({ id = 'g/h2', repo = 'g/h2', disable = true }, plugs[11])
assert.are.same({ _normalized = true, id = 'g/h2', repo = 'g/h2', disable = true }, plugs[11])

assert.are.same({
_normalized = true,
id = 'g/h',
repo = 'g/h',
requires = { { id = 'g/h1', repo = 'g/h1' }, { id = 'g/h2', repo = 'g/h2', disable = true } },
}, plugs[12])

end)

it('requires and deps contain false', function()
local P = { count = 0, plugs = {}, plugMap = {}, userPlugins = {} }

usePlug(P, loadPlug, {
'p',
deps = { false, { 'd1' }, false, 'd2' },
requires = { false, { 'r1' }, false, { 'r2' } },
})

assert.are.same({
_normalized = true,
id = 'p',
deps = { { id = 'd1' }, { id = 'd2' } },
requires = { { id = 'r1' }, { id = 'r2' } },
}, P.plugs[1])

end)
end)

0 comments on commit 0fa4331

Please sign in to comment.