Skip to content

Commit

Permalink
feat: trigger RocksInstallPost user event after install/update
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Aug 6, 2024
1 parent 00a8e78 commit 277fe74
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ You can also pin/unpin installed plugins with:
:Rocks [pin|unpin] {rock}
```

## :calendar: User events

For `:h User` events that rocks.nvim will trigger, see `:h rocks.user-event`.

## :package: Extending `rocks.nvim`

This plugin provides a Lua API for extensibility.
Expand Down
29 changes: 29 additions & 0 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rocks.nvim ··································
·································································· |rocks-toml|
rocks.nvim commands ··········································· |rocks-commands|
rocks.nvim configuration ········································ |rocks-config|
rocks.nvim |User| |event|s ·································· |rocks.user-event|
Lua API for rocks.nvim extensions ·································· |rocks-api|
rocks.nvim API hooks ········································· |rocks-api-hooks|
rocks.nvim logging API ············································· |rocks-log|
Expand Down Expand Up @@ -131,6 +132,34 @@ RocksOpts *RocksOpts*
rocks.nvim will create a default luarocks config in `rocks_path` and merge it with this table (if set).


==============================================================================
rocks.nvim |User| |event|s *rocks.user-event*

rocks.user-events.data.RocksInstallPost*rocks.user-events.data.RocksInstallPost*
The following |User| |event|s are available:

RocksInstallPost Invoked after installing or updating a rock
The `data` is of type
|rocks.user-events.data.RocksInstallPost|.


To create an autocommand for an event:
>lua
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
---@type rocks.user-events.data.RocksInstallPost
local data = ev.data
-- ...
end,
})
<

Fields: ~
{spec} (RockSpec)
{installed} (Rock)


==============================================================================
Lua API for rocks.nvim extensions *rocks-api*

Expand Down
37 changes: 37 additions & 0 deletions lua/rocks/meta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Copyright (C) 2024 Neorocks Org.
--
-- License: GPLv3
-- Created: 03 Aug 2024
-- Updated: 03 Aug 2024
-- Homepage: https://github.com/nvim-neorocks/rocks.nvim
-- Maintainers: NTBBloodbath <[email protected]>, Vhyrro <[email protected]>, mrcjkb <[email protected]>

---@mod rocks.user-event rocks.nvim |User| |event|s
---
---@brief [[
---The following |User| |event|s are available:
---
---RocksInstallPost Invoked after installing or updating a rock
--- The `data` is of type
--- |rocks.user-events.data.RocksInstallPost|.
---
---
---To create an autocommand for an event:
--->lua
--- vim.api.nvim_create_autocmd("User", {
--- pattern = "RocksInstallPost",
--- callback = function(ev)
--- ---@type rocks.user-events.data.RocksInstallPost
--- local data = ev.data
--- -- ...
--- end,
--- })
---<

---@class rocks.user-events.data.RocksInstallPost
---@field spec RockSpec
---@field installed Rock

error("can't require a meta module")
local meta = {}
return meta
11 changes: 11 additions & 0 deletions lua/rocks/operations/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ helpers.install = nio.create(function(rock_spec, progress_handle)
-- We also exclude `-<specrev>` from the version match.
version = sc.stdout:match(name:gsub("%p", "%%%1") .. "%s+([^-%s]+)"),
}
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksInstallPost",
modeline = false,
---@type rocks.user-events.data.RocksInstallPost
data = {
spec = rock_spec,
installed = installed_rock,
},
})
end)
message = ("Installed: %s -> %s"):format(installed_rock.name, installed_rock.version)
log.info(message)
if progress_handle then
Expand Down
2 changes: 1 addition & 1 deletion nix/test-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
];
text = ''
mkdir -p doc
lemmy-help lua/rocks/{init,commands,config/init,api/{init,hooks},log}.lua > doc/rocks.txt
lemmy-help lua/rocks/{init,commands,config/init,meta,api/{init,hooks},log}.lua > doc/rocks.txt
'';
};
in {
Expand Down
19 changes: 16 additions & 3 deletions spec/operations/install_update_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ describe("install/update", function()
local operations = require("rocks.operations")
local state = require("rocks.state")
nio.tests.it("install and update rocks", function()
local autocmd_future = nio.control.future()
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
if not autocmd_future.is_set() then
autocmd_future.set(ev.data)
end
end,
})
local future = nio.control.future()
operations.add({ "Neorg", "7.0.0" }, function() -- ensure lower case
future.set(true)
end)
future.wait()
local installed_rocks = state.installed_rocks()
assert.same({
local neorg_expected = {
name = "neorg",
version = "7.0.0",
}, installed_rocks.neorg)
}
local installed_rocks = state.installed_rocks()
assert.same(neorg_expected, installed_rocks.neorg)
local data = autocmd_future.wait()
assert.same(neorg_expected, data.installed)
assert.same(neorg_expected, data.spec)
local user_rocks = require("rocks.config.internal").get_user_rocks()
assert.same({
name = "neorg",
Expand Down

0 comments on commit 277fe74

Please sign in to comment.