Skip to content

Commit

Permalink
feat(install): add breaking change check for Rocks install {rock}
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 5, 2024
1 parent 46f154a commit 91431b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
56 changes: 39 additions & 17 deletions lua/rocks/operations/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ helpers.is_installed = nio.create(function(rock_name)
return future.wait()
end, 1)

---@param rock OutdatedRock
---@return RockUpdate | nil
local function get_breaking_change(rock)
local _, version = pcall(vim.version.parse, rock.version)
local _, target_version = pcall(vim.version.parse, rock.target_version)
if type(version) == "table" and type(target_version) == "table" and target_version.major > version.major then
return setmetatable({
name = rock.name,
version = version,
target_version = target_version,
}, {
__tostring = function()
return ("%s %s -> %s"):format(rock.name, tostring(version), tostring(target_version))
end,
})
end
end

---@type fun(rock_name: rock_name): RockUpdate | nil
helpers.get_breaking_change = nio.create(function(rock_name)
local rock = state.outdated_rocks()[rock_name]
return rock and get_breaking_change(rock)
end, 2)

---@class RockUpdate
---@field name rock_name
---@field version vim.Version
Expand All @@ -187,32 +211,30 @@ end, 1)
function helpers.get_breaking_changes(outdated_rocks)
return vim.iter(outdated_rocks):fold(
{},
---@param acc table<rock_name, OutdatedRock>
---@param acc table<rock_name, RockUpdate>
---@param key rock_name
---@param rock OutdatedRock
function(acc, key, rock)
local _, version = pcall(vim.version.parse, rock.version)
local _, target_version = pcall(vim.version.parse, rock.target_version)
if
type(version) == "table"
and type(target_version) == "table"
and target_version.major > version.major
then
acc[key] = setmetatable({
name = rock.name,
version = version,
target_version = target_version,
}, {
__tostring = function()
return ("%s %s -> %s"):format(rock.name, tostring(version), tostring(target_version))
end,
})
local breaking_change = get_breaking_change(rock)
if breaking_change then
acc[key] = breaking_change
end
return acc
end
)
end

---@type async fun(rock: RockUpdate): boolean
helpers.prompt_for_breaking_intall = nio.create(function(rock)
local prompt = ([[
%s may be a breaking change! Update anyway?
To skip this prompt, run 'Rocks! install {rock}'
]]):format(tostring(rock))
nio.scheduler()
local choice = vim.fn.confirm(prompt, "&Yes\n&No", 2, "Question")
return choice == 1
end, 1)

---@type async fun(breaking_changes: table<rock_name, RockUpdate>, outdated_rocks: table<rock_name, OutdatedRock>): table<rock_name, OutdatedRock>
helpers.prompt_for_breaking_update = nio.create(function(breaking_changes, outdated_rocks)
local pretty_changes = vim.iter(breaking_changes)
Expand Down
7 changes: 7 additions & 0 deletions lua/rocks/operations/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ Use 'Rocks install {rock_name}' or install rocks-git.nvim.
end
local install_spec = parse_result.spec
local version = install_spec.version
local breaking_change = not version and helpers.get_breaking_change(rock_name)
if breaking_change and not helpers.prompt_for_breaking_intall(breaking_change) then
progress_handle:report({
title = "Installation aborted",
})
progress_handle:cancel()
end
nio.scheduler()
progress_handle:report({
message = version and ("%s -> %s"):format(rock_name, version) or rock_name,
Expand Down
2 changes: 1 addition & 1 deletion lua/rocks/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local config = require("rocks.config.internal")
local log = require("rocks.log")
local nio = require("nio")

---@type async fun(): {[string]: Rock}
---@type async fun(): table<rock_name, Rock>
state.installed_rocks = nio.create(function()
local rocks = vim.empty_dict()
---@cast rocks {[string]: Rock}
Expand Down
4 changes: 3 additions & 1 deletion spec/operations/install_update_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ describe("install/update", function()
future = nio.control.future()
operations.update(function()
future.set(true)
end)
end, {
skip_prompts = true,
})
future.wait()
installed_rocks = state.installed_rocks()
local updated_version = vim.version.parse(installed_rocks.neorg.version)
Expand Down

0 comments on commit 91431b4

Please sign in to comment.