-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
18 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
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
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
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,79 @@ | ||
---@mod rocks.operations.lock | ||
-- | ||
-- Copyright (C) 2024 Neorocks Org. | ||
-- | ||
-- License: GPLv3 | ||
-- Created: 04 Apr 2024 | ||
-- Updated: 04 Apr 2024 | ||
-- Homepage: https://github.com/nvim-neorocks/rocks.nvim | ||
-- Maintainers: NTBBloodbath <[email protected]>, Vhyrro <[email protected]>, mrcjkb <[email protected]> | ||
-- | ||
---@brief [[ | ||
-- | ||
-- Lockfile management. | ||
-- | ||
---@brief ]] | ||
|
||
local config = require("rocks.config.internal") | ||
local fs = require("rocks.fs") | ||
|
||
local lock = {} | ||
|
||
---@param reset boolean | ||
local function parse_rocks_lock(reset) | ||
local lockfile = reset and "" or fs.read_or_create(config.lockfile_path, "") | ||
return require("toml_edit").parse(lockfile) | ||
end | ||
|
||
---@param rock_name? rock_name | ||
function lock.update_lockfile(rock_name) | ||
local luarocks_lockfiles = vim.iter(vim.api.nvim_get_runtime_file("luarocks.lock", true)) | ||
:filter(function(path) | ||
return not rock_name or path:find(rock_name .. "/[^%/]+/luarocks.lock$") ~= nil | ||
end) | ||
:totable() | ||
local reset = rock_name == nil | ||
local rocks_lock = parse_rocks_lock(reset) | ||
for _, luarocks_lockfile in ipairs(luarocks_lockfiles) do | ||
local rock_key = rock_name or luarocks_lockfile:match("/([^%/]+)/[^%/]+/luarocks.lock$") | ||
if rock_key then | ||
local ok, loader = pcall(loadfile, luarocks_lockfile) | ||
if not ok or not loader then | ||
return | ||
end | ||
local success, luarocks_lock_tbl = pcall(loader) | ||
if not success or not luarocks_lock_tbl or not luarocks_lock_tbl.dependencies then | ||
return | ||
end | ||
rocks_lock[rock_key] = {} | ||
for dep, version in pairs(luarocks_lock_tbl.dependencies) do | ||
local is_semver = pcall(vim.version.parse, version) | ||
if is_semver and dep ~= "lua" then | ||
rocks_lock[rock_key][dep] = version | ||
end | ||
end | ||
end | ||
end | ||
fs.write_file(config.lockfile_path, "w", tostring(rocks_lock)) | ||
end | ||
|
||
---@param rock_name rock_name | ||
---@return string | nil luarocks_lock | ||
function lock.create_luarocks_lock(rock_name) | ||
local lockfile = require("toml").decode(fs.read_or_create(config.lockfile_path, "")) | ||
local dependencies = lockfile[rock_name] | ||
if not dependencies then | ||
return | ||
end | ||
local temp_dir = vim.fs.dirname(vim.fn.tempname()) | ||
local luarocks_lock = vim.fs.joinpath(temp_dir, "luarocks.lock") | ||
local content = ([[ | ||
return { | ||
dependencies = %s | ||
} | ||
]]):format(vim.inspect(dependencies)) | ||
fs.write_file(luarocks_lock, "w", content) | ||
return luarocks_lock | ||
end | ||
|
||
return lock |
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,72 @@ | ||
---@diagnostic disable: inject-field | ||
|
||
local lock = require("rocks.operations.lock") | ||
local fs = require("rocks.fs") | ||
local config = require("rocks.config.internal") | ||
|
||
local tempdir = vim.fs.dirname(vim.fn.tempname()) | ||
assert(vim.fn.mkdir(tempdir, "p") == 1, "Failed to create tempdir " .. tempdir) | ||
|
||
describe("operations.lock", function() | ||
it("Lockfile roundtrip", function() | ||
config.lockfile_path = vim.fs.joinpath(tempdir, "rocks.lock") | ||
local lockfile_content = [[ | ||
[neorg] | ||
"pathlib.nvim" = "2.2.0-1" | ||
"plenary.nvim" = "0.1.4-1" | ||
say = "1.4.1-3" | ||
"nui.nvim" = "0.3.0-1" | ||
"lua-utils.nvim" = "1.0.2-1" | ||
luassert = "1.9.0-1" | ||
nvim-nio = "1.7.0-1" | ||
[neotest] | ||
luassert = "1.9.0-1" | ||
say = "1.4.1-3" | ||
"plenary.nvim" = "0.1.4-1" | ||
]] | ||
fs.write_file(config.lockfile_path, "w", lockfile_content) | ||
assert.same(lockfile_content, fs.read_or_create(config.lockfile_path, "")) | ||
local neorg_lockfile = assert(lock.create_luarocks_lock("neorg"), "Failed to create neorg luarocks.lock") | ||
local neorg_rtp_path = vim.fs.joinpath(tempdir, "neorg", "1.0.0-1") | ||
vim.fn.mkdir(neorg_rtp_path, "p") | ||
vim.uv.fs_symlink(neorg_lockfile, vim.fs.joinpath(neorg_rtp_path, "luarocks.lock")) | ||
vim.opt.runtimepath:append(neorg_rtp_path) | ||
local neotest_lockfile = assert(lock.create_luarocks_lock("neotest"), "Failed to create neotest luarocks.lock") | ||
local neotest_rtp_path = vim.fs.joinpath(tempdir, "neotest", "1.0.0-1") | ||
vim.fn.mkdir(neotest_rtp_path, "p") | ||
vim.uv.fs_symlink(neotest_lockfile, vim.fs.joinpath(neotest_rtp_path, "luarocks.lock")) | ||
vim.opt.runtimepath:append(neotest_rtp_path) | ||
-- Reset lockfile path | ||
config.lockfile_path = vim.fs.joinpath(vim.fs.dirname(vim.fn.tempname()), "rocks.lock") | ||
lock.update_lockfile() | ||
local roundtrip_content = fs.read_or_create(config.lockfile_path, "") | ||
assert.same(lockfile_content, roundtrip_content) | ||
end) | ||
it("Excludes lua and dev dependencies", function() | ||
local luarocks_lockfile_content = [[ | ||
return { | ||
dependencies = { | ||
lua = "5.1-1", | ||
luassert = "1.9.0-1", | ||
say = "1.4.1-3", | ||
"plenary.nvim" = "scm", | ||
} | ||
} | ||
]] | ||
local neotest_rtp_path = vim.fs.joinpath(tempdir, "neotest", "1.0.0-1") | ||
local luarocks_lockfile_path = vim.fs.joinpath(neotest_rtp_path, "luarocks.lock") | ||
fs.write_file(luarocks_lockfile_path, "w", luarocks_lockfile_content) | ||
vim.opt.runtimepath:append(neotest_rtp_path) | ||
config.lockfile_path = vim.fs.joinpath(vim.fs.dirname(vim.fn.tempname()), "rocks.lock") | ||
lock.update_lockfile() | ||
local expected_content = [[ | ||
[neotest] | ||
luassert = "1.9.0-1" | ||
say = "1.4.1-3" | ||
"plenary.nvim" = "scm" | ||
]] | ||
local lockfile_content = fs.read_or_create(config.lockfile_path, "") | ||
assert.same(expected_content, lockfile_content) | ||
end) | ||
end) |