Skip to content

Commit cf459ff

Browse files
committed
(mini.deps) Update hooks to be called with table data argument.
1 parent 22e04f6 commit cf459ff

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

doc/mini-deps.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,16 @@ Primarily, specification is a table with the following fields:
277277
Default: `nil` for no dependencies.
278278

279279
- <hooks> `(table|nil)` - table with callable hooks to call on certain events.
280-
Each hook is executed without arguments. Possible hook names:
280+
Possible hook names:
281281
- <pre_install> - before creating plugin directory.
282282
- <post_install> - after creating plugin directory.
283283
- <pre_checkout> - before making change in plugin directory.
284284
- <post_checkout> - after making change in plugin directory.
285+
Each hook is executed with the following table as an argument:
286+
- <path> (`string`) - absolute path to plugin's directory
287+
(might not yet exist on disk).
288+
- <source> (`string`) - resolved <source> from spec.
289+
- <name> (`string`) - resolved <name> from spec.
285290
Default: `nil` for no hooks.
286291

287292
------------------------------------------------------------------------------

lua/mini/deps.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,16 @@
274274
--- Default: `nil` for no dependencies.
275275
---
276276
--- - <hooks> `(table|nil)` - table with callable hooks to call on certain events.
277-
--- Each hook is executed without arguments. Possible hook names:
277+
--- Possible hook names:
278278
--- - <pre_install> - before creating plugin directory.
279279
--- - <post_install> - after creating plugin directory.
280280
--- - <pre_checkout> - before making change in plugin directory.
281281
--- - <post_checkout> - after making change in plugin directory.
282+
--- Each hook is executed with the following table as an argument:
283+
--- - <path> (`string`) - absolute path to plugin's directory
284+
--- (might not yet exist on disk).
285+
--- - <source> (`string`) - resolved <source> from spec.
286+
--- - <name> (`string`) - resolved <name> from spec.
282287
--- Default: `nil` for no hooks.
283288
---@tag MiniDeps-plugin-specification
284289

@@ -1001,7 +1006,7 @@ H.plugs_exec_hooks = function(plugs, name)
10011006
local has_error = p.job and #p.job.err > 0
10021007
local should_execute = vim.is_callable(p.hooks[name]) and not has_error
10031008
if should_execute then
1004-
local ok, err = pcall(p.hooks[name])
1009+
local ok, err = pcall(p.hooks[name], { path = p.path, source = p.source, name = p.name })
10051010
if not ok then
10061011
local msg = string.format('Error executing %s hook in `%s`:\n%s', name, p.name, err)
10071012
H.notify(msg, 'ERROR')

tests/test_deps.lua

+67-7
Original file line numberDiff line numberDiff line change
@@ -847,25 +847,55 @@ T['add()']['Install']['properly executes `*_install` hooks'] = function()
847847
_G.process_mock_data = { [2] = { duration = 10 }, [3] = { duration = 5 } }
848848
849849
-- Add plugin with dependency and hooks
850+
_G.args = {}
851+
local make_hook = function(msg)
852+
return function(...)
853+
table.insert(_G.args, { msg, { ... } })
854+
vim.notify(msg)
855+
end
856+
end
857+
850858
local dep_spec = {
851859
source = 'user/dep_plugin',
852860
hooks = {
853-
pre_install = function() vim.notify('Dependency pre_install') end,
854-
post_install = function() vim.notify('Dependency post_install') end,
861+
pre_install = make_hook('Dependency pre_install'),
862+
post_install = make_hook('Dependency post_install'),
855863
},
856864
}
857865
local spec = {
858866
source = 'user/new_plugin',
859867
depends = { dep_spec },
860868
hooks = {
861-
pre_install = function() vim.notify('Target pre_install') end,
862-
post_install = function() vim.notify('Target post_install') end,
869+
pre_install = make_hook('Target pre_install'),
870+
post_install = make_hook('Target post_install'),
863871
},
864872
}
865873
866874
MiniDeps.add(spec)
867875
]])
868876

877+
-- Should be called with proper arguments
878+
local cwd_new_plugin, cwd_dep_plugin = test_opt_dir .. '/new_plugin', test_opt_dir .. '/dep_plugin'
879+
local ref_args = {
880+
{
881+
'Dependency pre_install',
882+
{ { path = cwd_dep_plugin, source = 'https://github.com/user/dep_plugin', name = 'dep_plugin' } },
883+
},
884+
{
885+
'Target pre_install',
886+
{ { path = cwd_new_plugin, source = 'https://github.com/user/new_plugin', name = 'new_plugin' } },
887+
},
888+
{
889+
'Dependency post_install',
890+
{ { path = cwd_dep_plugin, source = 'https://github.com/user/dep_plugin', name = 'dep_plugin' } },
891+
},
892+
{
893+
'Target post_install',
894+
{ { path = cwd_new_plugin, source = 'https://github.com/user/new_plugin', name = 'new_plugin' } },
895+
},
896+
}
897+
eq(child.lua_get('_G.args'), ref_args)
898+
869899
-- Should produce notifications
870900
local ref_notify_log = {
871901
-- Hooks are executed in a session order
@@ -1378,14 +1408,22 @@ end
13781408

13791409
T['update()']['properly executes `*_checkout` hooks'] = function()
13801410
child.lua([[
1381-
-- Add plugin with dependency and hooks
1411+
-- Add plugins with hooks
1412+
_G.args = {}
1413+
local make_hook = function(msg)
1414+
return function(...)
1415+
table.insert(_G.args, { msg, { ... } })
1416+
vim.notify(msg)
1417+
end
1418+
end
1419+
13821420
for i = 1, 3 do
13831421
local name = 'plugin_' .. i
13841422
MiniDeps.add({
13851423
source = 'user/' .. name,
13861424
hooks = {
1387-
pre_checkout = function() vim.notify(name .. ' pre_checkout') end,
1388-
post_checkout = function() vim.notify(name .. ' post_checkout') end,
1425+
pre_checkout = make_hook(name .. ' pre_checkout'),
1426+
post_checkout = make_hook(name .. ' post_checkout'),
13891427
},
13901428
})
13911429
end
@@ -1420,6 +1458,28 @@ T['update()']['properly executes `*_checkout` hooks'] = function()
14201458
]])
14211459
child.lua('MiniDeps.update(nil, { force = true })')
14221460

1461+
-- Should be called with proper arguments
1462+
local cwd_plugin_1, cwd_plugin_2 = test_opt_dir .. '/plugin_1', test_opt_dir .. '/plugin_2'
1463+
local ref_args = {
1464+
{
1465+
'plugin_1 pre_checkout',
1466+
{ { path = cwd_plugin_1, source = 'https://github.com/user/plugin_1', name = 'plugin_1' } },
1467+
},
1468+
{
1469+
'plugin_2 pre_checkout',
1470+
{ { path = cwd_plugin_2, source = 'https://github.com/user/plugin_2', name = 'plugin_2' } },
1471+
},
1472+
{
1473+
'plugin_1 post_checkout',
1474+
{ { path = cwd_plugin_1, source = 'https://github.com/user/plugin_1', name = 'plugin_1' } },
1475+
},
1476+
{
1477+
'plugin_2 post_checkout',
1478+
{ { path = cwd_plugin_2, source = 'https://github.com/user/plugin_2', name = 'plugin_2' } },
1479+
},
1480+
}
1481+
eq(child.lua_get('_G.args'), ref_args)
1482+
14231483
-- Should produce notifications
14241484
local ref_notify_log = {
14251485
{ '(mini.deps) Downloading 3 updates', 'INFO' },

0 commit comments

Comments
 (0)