Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(operations): error if 'rocks' or 'plugin' key missing in rocks.toml #318

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lua/rocks/operations/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ local function add_dev_rocks_for_update(outdated_rocks)
end)
end

---@param rocks_toml MutRocksTomlRef
---@param rock_name rock_name
---@return "plugins"|"rocks"|nil rocks_key The key of the table containing the rock entry
---@return rock_config_table|nil
local function get_rock_and_key(rocks_toml, rock_name)
local rocks_key = (rocks_toml.plugins and rocks_toml.plugins[rock_name] and "plugins")
or (rocks_toml.rocks and rocks_toml.rocks[rock_name] and "rocks")
return rocks_key, rocks_key and rocks_toml[rocks_key][rock_name]
end

--- Synchronizes the user rocks with the physical state on the current machine.
--- - Installs missing rocks
--- - Ensures that the correct versions are installed
Expand Down Expand Up @@ -400,7 +410,7 @@ operations.update = function(on_complete)

local ct = 0
for name, rock in pairs(outdated_rocks) do
local user_rock = user_rocks.plugins[rock.name] or user_rocks.rocks[rock.name]
local rocks_key, user_rock = get_rock_and_key(user_rocks, rock.name)
if not user_rock or user_rock.pin then
goto skip_update
end
Expand All @@ -420,9 +430,9 @@ operations.update = function(on_complete)
local rock_name = ret.name
if user_rock and user_rock.version then
-- Rock is configured as a table -> Update version.
user_rocks.plugins[rock_name].version = ret.version
user_rocks[rocks_key][rock_name].version = ret.version
elseif user_rock then -- Only insert the version if there's an entry in rocks.toml
user_rocks.plugins[rock_name] = ret.version
user_rocks[rocks_key][rock_name] = ret.version
end
progress_handle:report({
message = rock.version == rock.target_version
Expand Down Expand Up @@ -688,15 +698,14 @@ end
operations.pin = function(rock_name)
nio.run(function()
local user_config = parse_rocks_toml()
local rocks_key = (user_config.plugins and user_config.plugins[rock_name] and "plugins")
or (user_config.rocks and user_config.rocks[rock_name] and "rocks")
local rocks_key, user_rock = get_rock_and_key(user_config, rock_name)
if not rocks_key then
vim.schedule(function()
vim.notify(rock_name .. " not found in rocks.toml", vim.log.levels.ERROR)
end)
return
end
if type(user_config[rocks_key][rock_name]) == "string" then
if type(user_rock) == "string" then
local version = user_config[rocks_key][rock_name]
user_config[rocks_key][rock_name] = {}
user_config[rocks_key][rock_name].version = version
Expand All @@ -714,17 +723,17 @@ end
operations.unpin = function(rock_name)
nio.run(function()
local user_config = parse_rocks_toml()
local rocks_key = (user_config.plugins[rock_name] and "plugins") or (user_config.rocks[rock_name] and "rocks")
if not rocks_key then
local rocks_key, user_rock = get_rock_and_key(user_config, rock_name)
if not rocks_key or not user_rock then
vim.schedule(function()
vim.notify(rock_name .. " not found in rocks.toml", vim.log.levels.ERROR)
end)
return
end
if type(user_config[rocks_key][rock_name]) == "string" then
if type(user_rock) == "string" then
return
end
if not user_config[rocks_key][rock_name].opt then
if not user_rock.opt then
user_config[rocks_key][rock_name] = user_config[rocks_key][rock_name].version
else
user_config[rocks_key][rock_name].pin = nil
Expand Down
Loading