-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimental): install rockspec dependencies
- Loading branch information
Showing
3 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
---@mod rocks-dev.rockspec | ||
--- | ||
---@brief [[ | ||
--- | ||
---Get dependencies from a rockspec in the repository root | ||
--- | ||
---@brief ]] | ||
|
||
-- Copyright (C) 2024 Neorocks Org. | ||
-- | ||
-- License: GPLv3 | ||
-- Created: 03 Sep 2024 | ||
-- Updated: 03 Sep 2024 | ||
-- Homepage: https://github.com/nvim-neorocks/rocks-git.nvim | ||
-- Maintainer: mrcjkb <[email protected]> | ||
|
||
local log = require("rocks.log") | ||
|
||
-- NOTE: This module shares code with rocks-git.nvim | ||
-- It's too soon to abstract it out, but it could potentially become part | ||
-- of the rocks.nvim API. | ||
local rockspec = {} | ||
|
||
---@param rock_spec rocks-dev.DevRockSpec | ||
---@return string[] | ||
function rockspec.get_dependencies(rock_spec) | ||
local rockspec_paths = vim.fs.find(function(name, path) | ||
return rock_spec.dir == path | ||
and vim | ||
.iter({ "scm", "dev", "git" }) | ||
---@param specrev string | ||
:map(function(specrev) | ||
return rock_spec.name .. "%-" .. specrev .. "%-%d.rockspec" | ||
end) | ||
---@param pattern string | ||
:any(function(pattern) | ||
return name:match(pattern) ~= nil | ||
end) | ||
end, { | ||
path = rock_spec.dir, | ||
upward = false, | ||
}) | ||
if vim.tbl_isempty(rockspec_paths) then | ||
return {} | ||
end | ||
local rockspec_path = rockspec_paths[1] | ||
local rockspec_tbl = {} | ||
xpcall(function() | ||
loadfile(rockspec_path, "t", rockspec_tbl)() | ||
end, function(err) | ||
log.error("rocks-git: Could not load rockspec: " .. err) | ||
end) | ||
return rockspec_tbl.dependencies or {} | ||
end | ||
|
||
return rockspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
local tempdir = vim.fn.tempname() | ||
local plugin_dir = vim.fs.joinpath(tempdir, "foo.nvim") | ||
vim.env.HOME = tempdir | ||
vim.system({ "rm", "-r", tempdir }):wait() | ||
vim.system({ "mkdir", "-p", plugin_dir }):wait() | ||
|
||
local rockspec = require("rocks-dev.rockspec") | ||
describe("rockspec", function() | ||
describe("get_dependencies", function() | ||
local rockspec_content = [[ | ||
package = "foo.nvim" | ||
version = "scm-1" | ||
dependencies = { | ||
"bar >= 1.0.0", | ||
} | ||
]] | ||
local fh = io.open(vim.fs.joinpath(plugin_dir, "foo.nvim-scm-1.rockspec"), "w+") | ||
assert(fh, "Cound not open rockspec for writing") | ||
fh:write(rockspec_content) | ||
fh:close() | ||
---@diagnostic disable-next-line: missing-fields | ||
local dependencies = rockspec.get_dependencies({ | ||
name = "foo.nvim", | ||
dir = plugin_dir, | ||
}) | ||
assert.same({ "bar >= 1.0.0" }, dependencies) | ||
end) | ||
end) |