Skip to content

Commit

Permalink
chore(installer): better error messages (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Sep 28, 2024
1 parent fbcfedf commit 630d24f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
35 changes: 28 additions & 7 deletions bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ local function notify_output(msg, sc, level)
)
end

---@param cmd string[]
---@param opts? vim.SystemOpts
---@return vim.SystemCompleted
local function exec(cmd, opts)
---@type boolean, vim.SystemObj | string
local ok, so_or_err = pcall(vim.system, cmd, opts)
if not ok then
---@cast so_or_err string
return {
code = 1,
signal = 0,
stderr = ([[
Failed to execute:
%s
%s]]):format(table.concat(cmd, " "), so_or_err),
}
end
---@cast so_or_err vim.SystemObj
return so_or_err:wait()
end

--- Sets up luarocks for use with rocks.nvim
---@param path string
---@return boolean success
Expand All @@ -59,13 +80,13 @@ local function set_up_luarocks(path)

vim.notify("Downloading luarocks...")

local sc = vim.system({
local sc = exec({
"git",
"clone",
"--filter=blob:none",
"https://github.com/luarocks/luarocks.git",
tempdir,
}):wait()
})

if sc.code ~= 0 then
notify_output("Cloning luarocks failed: ", sc, vim.log.levels.ERROR)
Expand All @@ -74,15 +95,15 @@ local function set_up_luarocks(path)

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

sc = vim.system({
sc = exec({
"sh",
"configure",
"--prefix=" .. path,
"--lua-version=5.1",
"--force-config",
}, {
cwd = tempdir,
}):wait()
})

if sc.code ~= 0 then
notify_output("Configuring luarocks failed.", sc, vim.log.levels.ERROR)
Expand All @@ -91,12 +112,12 @@ local function set_up_luarocks(path)

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

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

if sc.code ~= 0 then
notify_output("Installing luarocks failed.", sc, vim.log.levels.ERROR)
Expand Down Expand Up @@ -136,7 +157,7 @@ if supported_arch then
end
vim.notify("Installing rocks.nvim...")

local sc = vim.system(install_cmd):wait()
local sc = exec(install_cmd)

if sc.code ~= 0 then
notify_output("Installing rocks.nvim failed:", sc, vim.log.levels.ERROR)
Expand Down
35 changes: 28 additions & 7 deletions installer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ local function notify_output(msg, sc, level)
)
end

---@param cmd string[]
---@param opts? vim.SystemOpts
---@return vim.SystemCompleted
local function exec(cmd, opts)
---@type boolean, vim.SystemObj | string
local ok, so_or_err = pcall(vim.system, cmd, opts)
if not ok then
---@cast so_or_err string
return {
code = 1,
signal = 0,
stderr = ([[
Failed to execute:
%s
%s]]):format(table.concat(cmd, " "), so_or_err),
}
end
---@cast so_or_err vim.SystemObj
return so_or_err:wait()
end

--- Sets up luarocks for use with rocks.nvim
---@param install_path string
---@return boolean success
Expand All @@ -174,13 +195,13 @@ local function set_up_luarocks(install_path)

vim.notify("Downloading luarocks...")

local sc = vim.system({
local sc = exec({
"git",
"clone",
"--filter=blob:none",
"https://github.com/luarocks/luarocks.git",
tempdir,
}):wait()
})

if sc.code ~= 0 then
notify_output("Cloning luarocks failed.", sc, vim.log.levels.ERROR)
Expand All @@ -189,15 +210,15 @@ local function set_up_luarocks(install_path)

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

sc = vim.system({
sc = exec({
"sh",
"configure",
"--prefix=" .. install_path,
"--lua-version=5.1",
"--force-config",
}, {
cwd = tempdir,
}):wait()
})

if sc.code ~= 0 then
notify_output("Configuring luarocks failed.", sc, vim.log.levels.ERROR)
Expand All @@ -206,12 +227,12 @@ local function set_up_luarocks(install_path)

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

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

if sc.code ~= 0 then
notify_output("Installing luarocks failed.", sc, vim.log.levels.ERROR)
Expand Down Expand Up @@ -396,7 +417,7 @@ local function install()
table.insert(install_cmd, 4, "--server='https://nvim-neorocks.github.io/rocks-binaries/'")
end
vim.notify("Installing rocks.nvim...")
local sc = vim.system(install_cmd):wait()
local sc = exec(install_cmd)

if sc.code ~= 0 then
notify_output("Installing rocks.nvim failed:", sc, vim.log.levels.ERROR)
Expand Down

0 comments on commit 630d24f

Please sign in to comment.