-
Notifications
You must be signed in to change notification settings - Fork 15
/
installer.lua
527 lines (445 loc) · 19.1 KB
/
installer.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
---@author Vhyrro
---@license GPLv3
-- This file activates an installer window within Neovim that allows for a fully fledged `rocks.nvim` installation.
-- This file is usually sourced from an external hosting provider like Github.
-- GENERAL TODOs:
-- - Make resizing work with windows
-- - Remove some code duplication
-- - Make code work on all platforms
-- - Add proper error handling
local min_version = "0.10.0"
if vim.fn.has("nvim-" .. min_version) ~= 1 then
error(("rocks.nvim requires Neovim >= %s"):format(min_version))
end
--- The buffer ID of the main UI
---@type number
local buffer = vim.api.nvim_create_buf(false, true)
--- The window ID of the main UI
---@type number
local window = vim.api.nvim_get_current_win()
-- STEP 1: Set up appropriate variables for newly created buffer.
vim.api.nvim_buf_set_name(buffer, "rocks.nvim installer")
vim.bo[buffer].expandtab = true
vim.wo[window].conceallevel = 3
vim.wo[window].concealcursor = "nv"
vim.api.nvim_win_set_buf(window, buffer)
vim.cmd([[
syntax match Rocks /rocks\.nvim/
hi Rocks gui=bold cterm=bold
]])
vim.api.nvim_set_option_value("virtualedit", "all", {
win = window,
})
-----------------------------------------------------------------
--- Temporarily sets a buffer to modifiable before running a callback
--- and making the buffer unmodifiable again.
---@param id number #The buffer ID of the buffer to unlock
---@param callback fun() #The callback to execute
local function acquire_buffer_lock(id, callback)
vim.bo[id].modifiable = true
callback()
vim.bo[id].modifiable = false
end
--- Resizes the user interface and readjusts all text and other UI elements.
--- TODO(vhyrro): Add logic for when the screen is too small to display text.
local function resize_ui()
acquire_buffer_lock(buffer, function()
local size = vim.api.nvim_win_get_width(window)
vim.opt.textwidth = size % 2 == 0 and size or size - 1
vim.cmd("%center")
end)
end
--- Creates the main banner and introduction text for the installer.
local function create_body()
local title = [[
_ __ ___ ___| | _____ _ ____ _(_)_ __ ___
| '__/ _ \ / __| |/ / __| | '_ \ \ / / | '_ ` _ \
| | | (_) | (__| <\__ \_| | | \ V /| | | | | | |
|_| \___/ \___|_|\_\___(_)_| |_|\_/ |_|_| |_| |_|
]]
---@type string[]
local title_lines = vim.split(title, "\n", { plain = true, trimempty = true })
-- Padding logic
-- The following `do` block ensures that all lines are of equal width
-- so the title gets centered appropriately.
do
local min_len = 0
for _, line in ipairs(title_lines) do
min_len = math.max(min_len, line:len())
end
for i, line in ipairs(title_lines) do
title_lines[i] = title_lines[i] .. string.rep(" ", math.max(min_len - line:len() - 1, 0))
end
end
--- The introductory text for the `rocks.nvim` installer.
--- Segments where input should be permitted are defined by the following syntax:
---
--- [name:length:{{lua_code()}}]
---
--- The return value of the executed lua becomes the default value for that entry.
--- The default value section may be ommitted.
---@type string[]
local introduction = vim.split(
[[
Welcome to the rocks.nvim installer!
rocks.nvim is a modern approach to Neovim plugin management.
This page lists all of the most important tweakable aspects of the installation process.
To edit a value, move your cursor over it and modify the value using regular Neovim keybinds.
When you are ready, press <CR> on the OK button.
This installer supports using the mouse.
Once you start editing a value, you may exit it by pressing Enter or by clicking elsewhere.
-------------------------------------------------------------------------------------------
Rocks installation path: [install_path:50:{{vim.fs.joinpath(vim.fn.stdpath('data'), "rocks")}}]
Set up luarocks (recommended) ?: [setup_luarocks:6:{{true }}]
< OK >
]],
"\n",
{ plain = true }
)
vim.api.nvim_buf_set_lines(buffer, 0, -1, true, vim.list_extend(title_lines, introduction))
vim.bo[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
--- 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 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
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=" .. install_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
--- The main function of the installer.
local function install()
vim.api.nvim_create_autocmd("VimResized", {
buffer = buffer,
callback = vim.schedule_wrap(resize_ui),
})
create_body()
resize_ui()
-- This section goes through all input declarations and parses them, creating new windows
-- where applicable (see the `introduction` variable in the `create_body` function).
---@see create_body
---@alias position { [1]: integer, [2]: integer }
---@alias input_field {window: number, buffer: number, width: number, content: string, position: position}
--- Stores all of the input fields (window IDs, buffer IDs, content)
---@type {[string]: input_field}
local input_fields = {}
for i, line in ipairs(vim.api.nvim_buf_get_lines(buffer, 0, -1, true)) do
-- Try to find an input declaration and parse it.
---@type integer|nil, integer|nil, string, number, string
local start, end_, name, width, default_value = line:find("%[([^:]+):([0-9]+):%{%{(.+)%}%}%]")
if start and end_ then
-- Attempt to execute the code that will give us the default value
default_value = assert(loadstring("return tostring(" .. default_value .. ")"))()
-- Create necessary padding for the input window and recenter the line where we placed the new window.
acquire_buffer_lock(buffer, function()
vim.api.nvim_buf_set_text(buffer, i - 1, start - 1, i - 1, end_, { string.rep("_", width) })
vim.cmd(tostring(i) .. "center")
local difference = math.floor((width - (end_ - start)) / 2)
start = start - difference
end_ = end_ - difference
end)
-- Create a subbuffer for the input window which will contain editable text.
local subbuffer = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(subbuffer, 0, -1, true, { default_value })
local win_id = vim.api.nvim_open_win(subbuffer, false, {
width = tonumber(width),
height = 1,
row = 0,
col = 0,
border = "none",
style = "minimal",
focusable = false,
fixed = false,
relative = "win",
win = window,
bufpos = { i - 1, start - 1 },
})
vim.wo[win_id].wrap = false
input_fields[name] = {
window = win_id,
position = {
i - 1,
start - 1,
},
buffer = subbuffer,
width = width,
content = default_value,
}
vim.keymap.set({ "n", "i" }, "<CR>", function()
vim.cmd.stopinsert()
local current_cursor_pos = vim.api.nvim_win_get_cursor(window)
current_cursor_pos[1] = current_cursor_pos[1] + 1
vim.api.nvim_win_set_cursor(window, current_cursor_pos)
vim.api.nvim_set_current_win(window)
end, { buffer = subbuffer })
-- Every time the value within the input window changes also update the data
-- in the input_fields table to reflect that data.
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
buffer = subbuffer,
callback = function()
input_fields[name].content = vim.api.nvim_buf_get_lines(subbuffer, 0, -1, true)[1]
end,
})
end
end
-- If the user moves their cursor into the area of a window then move the cursor /into/ that
-- window. This allows editable parts of text in an otherwise uneditable buffer.
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = buffer,
callback = function()
if not vim.api.nvim_win_is_valid(window) then
return true
end
local current_cursor_pos = vim.api.nvim_win_get_cursor(window)
-- Go through every active input field and see if we are in its area.
for _, data in pairs(input_fields) do
if not vim.api.nvim_win_is_valid(data.window) then
return true
end
local win_pos = data.position
local width = data.width
if
current_cursor_pos[1] - 1 == win_pos[1]
and (current_cursor_pos[2] >= win_pos[2] and current_cursor_pos[2] < win_pos[2] + width)
then
vim.api.nvim_set_current_win(data.window)
end
end
end,
})
vim.keymap.set("n", "<CR>", function()
local cursor = vim.api.nvim_win_get_cursor(0)[1]
local line = vim.trim(vim.api.nvim_buf_get_lines(0, cursor - 1, cursor, true)[1])
if line == "< OK >" then
local install_path = input_fields.install_path.content
local setup_luarocks = input_fields.setup_luarocks.content == "true"
local temp_luarocks_path =
vim.fs.joinpath(vim.fn.stdpath("run") --[[@as string]], ("luarocks-%X"):format(math.random(256 ^ 7)))
local luarocks_binary = "luarocks"
if setup_luarocks then
local success = set_up_luarocks(temp_luarocks_path)
if not success then
return
end
luarocks_binary = vim.fs.joinpath(temp_luarocks_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 installer to setup luarocks automatically",
vim.log.levels.ERROR
)
return
end
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
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)
end
acquire_buffer_lock(buffer, function()
local install_path_rel = install_path:gsub(vim.env.HOME, "")
vim.api.nvim_buf_set_lines(buffer, 0, -1, true, {
"INSTALLATION COMPLETE",
"",
"You are almost ready! Please take the following code snippet and paste it in your `init.lua`.",
"The code has already been copied to your clipboard:",
">lua",
" local rocks_config = {",
' rocks_path = vim.env.HOME .. "' .. install_path_rel .. '",',
" }",
" ",
" vim.g.rocks_nvim = rocks_config",
" ",
" local luarocks_path = {",
' vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?.lua"),',
' vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?", "init.lua"),',
" }",
' package.path = package.path .. ";" .. table.concat(luarocks_path, ";")',
" ",
" local luarocks_cpath = {",
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.so"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.so"),',
" -- Remove the dylib and dll paths if you do not need macos or windows support",
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.dylib"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.dylib"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.dll"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.dll"),',
" }",
' package.cpath = package.cpath .. ";" .. table.concat(luarocks_cpath, ";")',
" ",
' vim.opt.runtimepath:append(vim.fs.joinpath(rocks_config.rocks_path, "lib", "luarocks", "rocks-5.1", "rocks.nvim", "*"))',
"<",
"Thank you for installing rocks.nvim!",
"",
"<< OPEN INIT.LUA >>",
})
local size = vim.api.nvim_win_get_width(window)
vim.opt.textwidth = size % 2 == 0 and size or size - 1
vim.cmd("1center")
vim.cmd("$-2,$center")
vim.fn.setreg('"', {
"local rocks_config = {",
' rocks_path = vim.env.HOME .. "' .. install_path_rel .. '",',
"}",
"",
"vim.g.rocks_nvim = rocks_config",
"",
"local luarocks_path = {",
' vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?.lua"),',
' vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?", "init.lua"),',
"}",
'package.path = package.path .. ";" .. table.concat(luarocks_path, ";")',
"",
"local luarocks_cpath = {",
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.so"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.so"),',
" -- Remove the dylib and dll paths if you do not need macos or windows support",
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.dylib"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.dylib"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.dll"),',
' vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.dll"),',
"}",
'package.cpath = package.cpath .. ";" .. table.concat(luarocks_cpath, ";")',
"",
'vim.opt.runtimepath:append(vim.fs.joinpath(rocks_config.rocks_path, "lib", "luarocks", "rocks-5.1", "*", "*"))',
---@diagnostic disable-next-line: param-type-mismatch
}, "l")
vim.bo[buffer].filetype = "help"
end)
elseif line == "<< OPEN INIT.LUA >>" then
vim.cmd.edit(vim.fs.joinpath(vim.fn.stdpath("config") --[[@as string]], "init.lua"))
vim.cmd("write ++p")
pcall(vim.api.nvim_buf_delete, buffer, { force = true })
end
end, { buffer = 0 })
end
vim.schedule(install)