Skip to content

Commit

Permalink
feat(installer): error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored and vhyrro committed Nov 28, 2023
1 parent 14fa236 commit 9abf1e3
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions installer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,50 @@ It is highly recommended that you allow rocks.nvim to set up luarocks for you.
vim.api.nvim_buf_set_option(buffer, "modifiable", false)
end

---@param dep string
---@return boolean is_missing
local function guard_set_up_luarocks_dependency_missing(dep)
if vim.fn.executable(dep) ~= 1 then
vim.notify(dep .. " must be installed to set up luarocks.", vim.log.levels.ERROR)
return true
end
return false
end

--- Sets up luarocks for use with rocks.nvim
---@param install_path string
---@return boolean success
local function set_up_luarocks(install_path)
-- TODO: Check running OS here
-- TODO: Error checking
if guard_set_up_luarocks_dependency_missing("git") then
return false
end
if guard_set_up_luarocks_dependency_missing("sh") then
-- TODO: Add support for Windows?
return false
end
if guard_set_up_luarocks_dependency_missing("make") then
return false
end

local tempdir = vim.fs.joinpath(vim.fn.stdpath("run"), "luarocks")

vim.system({
local sc = vim.system({
"git",
"clone",
"https://github.com/luarocks/luarocks.git",
tempdir,
"--depth=1",
}):wait()

if sc.code ~= 0 then
vim.notify("Cloning luarocks failed: " .. sc.stderr, vim.log.levels.ERROR)
return false
end

vim.notify("Configuring luarocks...")

vim.system({
sc = vim.system({
"sh",
"configure",
"--prefix=" .. install_path,
Expand All @@ -154,14 +180,26 @@ local function set_up_luarocks(install_path)
cwd = tempdir,
}):wait()

if sc.code ~= 0 then
vim.notify("Configuring luarocks failed: " .. sc.stderr, vim.log.levels.ERROR)
return false
end

vim.notify("Installing luarocks...")

vim.system({
sc = vim.system({
"make",
"install",
}, {
cwd = tempdir,
}):wait()

if sc.code ~= 0 then
vim.notify("Installing luarocks failed: " .. sc.stderr, vim.log.levels.ERROR)
return false
end

return true
end

--- The main function of the installer.
Expand Down Expand Up @@ -292,12 +330,22 @@ local function install()
local luarocks_binary = "luarocks"

if setup_luarocks then
set_up_luarocks(install_path)
local success = set_up_luarocks(install_path)
if not success then
return
end
luarocks_binary = vim.fs.joinpath(install_path, "bin", "luarocks")
elseif vim.fn.executable(luarocks_binary) ~= 1 then
vim.notify(
luarocks_binary
.. " not found. Please ensure luarocks is installed or configure the instllert to setup luarocks automatically",
vim.log.levels.ERROR
)
return
end

vim.notify("Installing rocks.nvim...")
vim.system({
local sc = vim.system({
luarocks_binary,
"--lua-version=5.1",
"--tree=" .. install_path,
Expand All @@ -306,6 +354,11 @@ local function install()
"rocks.nvim",
}):wait()

if sc.code ~= 0 then
vim.notify("Installing rocks.nvim failed: " .. sc.stderr, vim.log.levels.ERROR)
return
end

for _, data in pairs(input_fields) do
pcall(vim.api.nvim_buf_delete, data.buffer, { force = true })
pcall(vim.api.nvim_win_close, data.window, true)
Expand Down

0 comments on commit 9abf1e3

Please sign in to comment.