-
Notifications
You must be signed in to change notification settings - Fork 15
/
bootstrap.lua
179 lines (154 loc) · 4.66 KB
/
bootstrap.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- This script installs rocks.nvim through a bootstrapping process.
-- The process consists of the following:
-- - Configure luarocks to work flawlessly with Neovim
-- - Install luarocks
-- - Use the new luarocks installation to install `rocks.nvim`
-- The rocks.nvim plugin is already loaded via the vim.opt.runtimepath:append()
-- call in the `init.lua` bootstrapping script.
math.randomseed(os.time())
local config_data = vim.g.rocks_nvim or {}
local install_path = config_data.rocks_path or vim.fs.joinpath(vim.fn.stdpath("data") --[[@as string]], "rocks")
local temp_luarocks_path =
vim.fs.joinpath(vim.fn.stdpath("run") --[[@as string]], ("luarocks-%X"):format(math.random(256 ^ 7)))
local luarocks_binary = vim.fs.joinpath(temp_luarocks_path, "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
---@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
local function set_up_luarocks(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
local tempdir =
vim.fs.joinpath(vim.fn.stdpath("run") --[[@as string]], ("luarocks-%X"):format(math.random(256 ^ 7)))
vim.notify("Downloading luarocks...")
local sc = exec({
"git",
"clone",
"--filter=blob:none",
"https://github.com/luarocks/luarocks.git",
tempdir,
})
if sc.code ~= 0 then
notify_output("Cloning luarocks failed: ", sc, vim.log.levels.ERROR)
return false
end
local luarocks_version = "v3.11.1"
sc = exec({
"git",
"checkout",
luarocks_version,
}, {
cwd = tempdir,
})
if sc.code ~= 0 then
notify_output(("Checking out luarocks %s failed."):format(luarocks_version), sc, vim.log.levels.WARN)
end
vim.notify("Configuring luarocks...")
sc = exec({
"sh",
"configure",
"--prefix=" .. path,
"--lua-version=5.1",
"--force-config",
}, {
cwd = tempdir,
})
if sc.code ~= 0 then
notify_output("Configuring luarocks failed.", sc, vim.log.levels.ERROR)
return false
end
vim.notify("Installing luarocks...")
sc = exec({
"make",
"install",
}, {
cwd = tempdir,
})
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(temp_luarocks_path), "failed to install luarocks! Please try again :)")
local rocks_binaries_supported_arch_map = {
Darwin = {
arm64 = "macosx-aarch64",
aarch64 = "macosx-aarch64",
x86_64 = "macosx-x86_64",
},
Linux = {
x86_64 = "linux-x86_64",
},
Windows_NT = {
x86_64 = "win32-x86_64",
},
}
local uname = vim.uv.os_uname()
local supported_arch = rocks_binaries_supported_arch_map[uname.sysname][uname.machine]
local install_cmd = {
luarocks_binary,
"--lua-version=5.1",
"--tree=" .. install_path,
"install",
"rocks.nvim",
}
if supported_arch then
table.insert(install_cmd, 4, "--server='https://nvim-neorocks.github.io/rocks-binaries/'")
end
vim.notify("Installing rocks.nvim...")
local sc = exec(install_cmd)
if sc.code ~= 0 then
notify_output("Installing rocks.nvim failed:", sc, vim.log.levels.ERROR)
return
end
vim.print("rocks.nvim installed successfully!")