Skip to content

Commit bec5271

Browse files
authored
feat(experimental): install rockspec dependencies (#38)
1 parent 9efef1c commit bec5271

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

lua/rocks-dev/local-rock-handler.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
local api = require("rocks.api")
2+
local rockspec = require("rocks-dev.rockspec")
23
local nio = require("nio")
34

45
local rock_handler = {}
56

6-
---@class DevRockSpec: RockSpec
7+
---@class rocks-dev.DevRockSpec: RockSpec
78
---@field name string Name of the plugin.
89
---@field dir string
910

@@ -14,7 +15,7 @@ local ROCKS_DEV_VERSION = "rocksdev"
1415
function rock_handler.get_sync_callback(rock)
1516
local user_configuration = api.get_rocks_toml()
1617
if rock.dir or (rock.dev and user_configuration.dev.path) then
17-
---@cast rock DevRockSpec
18+
---@cast rock rocks-dev.DevRockSpec
1819
---@param on_progress fun(message: string)
1920
---@param _ fun(message: string) on_error
2021
---@param on_success? fun(opts: rock_handler.on_success.Opts)
@@ -38,6 +39,7 @@ function rock_handler.get_sync_callback(rock)
3839
name = rock.name,
3940
version = ROCKS_DEV_VERSION,
4041
},
42+
dependencies = rockspec.get_dependencies(rock),
4143
})
4244
end
4345

lua/rocks-dev/rockspec.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---@mod rocks-dev.rockspec
2+
---
3+
---@brief [[
4+
---
5+
---Get dependencies from a rockspec in the repository root
6+
---
7+
---@brief ]]
8+
9+
-- Copyright (C) 2024 Neorocks Org.
10+
--
11+
-- License: GPLv3
12+
-- Created: 03 Sep 2024
13+
-- Updated: 03 Sep 2024
14+
-- Homepage: https://github.com/nvim-neorocks/rocks-git.nvim
15+
-- Maintainer: mrcjkb <[email protected]>
16+
17+
local log = require("rocks.log")
18+
19+
-- NOTE: This module shares code with rocks-git.nvim
20+
-- It's too soon to abstract it out, but it could potentially become part
21+
-- of the rocks.nvim API.
22+
local rockspec = {}
23+
24+
---@param rock_spec rocks-dev.DevRockSpec
25+
---@return string[]
26+
function rockspec.get_dependencies(rock_spec)
27+
local rockspec_paths = vim.fs.find(function(name, path)
28+
return rock_spec.dir == path
29+
and vim
30+
.iter({ "scm", "dev", "git" })
31+
---@param specrev string
32+
:map(function(specrev)
33+
return rock_spec.name .. "%-" .. specrev .. "%-%d.rockspec"
34+
end)
35+
---@param pattern string
36+
:any(function(pattern)
37+
return name:match(pattern) ~= nil
38+
end)
39+
end, {
40+
path = rock_spec.dir,
41+
upward = false,
42+
})
43+
if vim.tbl_isempty(rockspec_paths) then
44+
return {}
45+
end
46+
local rockspec_path = rockspec_paths[1]
47+
local rockspec_tbl = {}
48+
xpcall(function()
49+
loadfile(rockspec_path, "t", rockspec_tbl)()
50+
end, function(err)
51+
log.error("rocks-git: Could not load rockspec: " .. err)
52+
end)
53+
return rockspec_tbl.dependencies or {}
54+
end
55+
56+
return rockspec

spec/rockspec_spec.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local tempdir = vim.fn.tempname()
2+
local plugin_dir = vim.fs.joinpath(tempdir, "foo.nvim")
3+
vim.env.HOME = tempdir
4+
vim.system({ "rm", "-r", tempdir }):wait()
5+
vim.system({ "mkdir", "-p", plugin_dir }):wait()
6+
7+
local rockspec = require("rocks-dev.rockspec")
8+
describe("rockspec", function()
9+
describe("get_dependencies", function()
10+
local rockspec_content = [[
11+
package = "foo.nvim"
12+
version = "scm-1"
13+
14+
dependencies = {
15+
"bar >= 1.0.0",
16+
}
17+
]]
18+
local fh = io.open(vim.fs.joinpath(plugin_dir, "foo.nvim-scm-1.rockspec"), "w+")
19+
assert(fh, "Cound not open rockspec for writing")
20+
fh:write(rockspec_content)
21+
fh:close()
22+
---@diagnostic disable-next-line: missing-fields
23+
local dependencies = rockspec.get_dependencies({
24+
name = "foo.nvim",
25+
dir = plugin_dir,
26+
})
27+
assert.same({ "bar >= 1.0.0" }, dependencies)
28+
end)
29+
end)

0 commit comments

Comments
 (0)