Skip to content

Commit

Permalink
feat: initial bootstrap.lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Apr 18, 2024
1 parent e25027e commit cd213f4
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,99 @@
-- The rocks.nvim plugin is already loaded via the vim.opt.runtimepath:append()
-- call in the `init.lua` bootstrapping script.

print("Hello")
local config_data = vim.g.rocks_config
local install_path = config_data.rocks_path or vim.fs.joinpath(vim.fn.stdpath("data"), "rocks")
local luarocks_binary = config_data.luarocks_binary or vim.fs.joinpath(install_location, "bin", "luarocks")

---@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

--- Notify command output.
---@param msg string
---@param sc vim.SystemCompleted
---@param level integer|nil
local function notify_output(msg, sc, level)
local function remove_shell_color(s)
return tostring(s):gsub("\x1B%[[0-9;]+m", "")
end
vim.notify(
table.concat({
msg,
sc and "stderr: " .. remove_shell_color(sc.stderr),
sc and "stdout: " .. remove_shell_color(sc.stdout),
}, "\n"),
level
)
end

--- Sets up luarocks for use with rocks.nvim
---@param install_path string
---@return boolean success
local function set_up_luarocks(install_path)
if guard_set_up_luarocks_dependency_missing("git") then
return false
end
if guard_set_up_luarocks_dependency_missing("make") then
return false
end

---@diagnostic disable-next-line: param-type-mismatch
local tempdir = vim.fs.joinpath(vim.fn.stdpath("run"), ("luarocks-%X"):format(math.random(256 ^ 7)))

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

local sc = vim.system({
"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)
return false
end

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

sc = vim.system({
"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)
return false
end

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

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

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

return true
end

assert(set_up_luarocks(install_path), "failed to install luarocks! Please try again :)")

0 comments on commit cd213f4

Please sign in to comment.