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
|
local Data = require "nvim-lsp-installer.data"
local log = require "nvim-lsp-installer.log"
local process = require "nvim-lsp-installer.process"
local installers = require "nvim-lsp-installer.installers"
local platform = require "nvim-lsp-installer.platform"
local fs = require "nvim-lsp-installer.fs"
local path = require "nvim-lsp-installer.path"
local M = {}
---@param url string @The url to fetch.
---@param callback fun(err: string|nil, raw_data: string)
local function fetch(url, callback)
local stdio = process.in_memory_sink()
log.fmt_debug("Fetching URL %s", url)
local on_exit = function(success)
if success then
log.fmt_debug("Successfully fetched URL %s", url)
callback(nil, table.concat(stdio.buffers.stdout, ""))
else
local stderr = table.concat(stdio.buffers.stderr, "")
log.fmt_warn("Failed to fetch URL %s. stderr=%s", url, stderr)
callback(("Failed to fetch url %q.\n%s"):format(url, stderr), nil)
end
end
local job_variants = {
process.lazy_spawn("wget", {
args = { "-nv", "-O", "-", url },
stdio_sink = stdio.sink,
}),
process.lazy_spawn("curl", {
args = { "-fsSL", url },
stdio_sink = stdio.sink,
}),
}
if platform.is_win then
local ps_script = {
"$ProgressPreference = 'SilentlyContinue'",
("Write-Output (iwr -UseBasicParsing -Uri %q).Content"):format(url),
}
table.insert(
job_variants,
1,
process.lazy_spawn("powershell.exe", {
args = { "-NoProfile", "-Command", table.concat(ps_script, ";") },
stdio_sink = stdio.sink,
env = process.graft_env({}, { "PSMODULEPATH" }),
})
)
end
process.attempt {
jobs = job_variants,
on_iterate = function()
log.debug "Flushing stdout/stderr buffers."
stdio.buffers.stdout = {}
stdio.buffers.stderr = {}
end,
on_finish = on_exit,
}
end
---@param repo string @The GitHub repo ("username/repo").
function M.use_github_latest_tag(repo)
---@type ServerInstallerFunction
return function(_, callback, context)
if context.requested_server_version then
log.fmt_debug(
"Requested server version already provided (%s), skipping fetching tags from GitHub.",
context.requested_server_version
)
-- User has already provided a version - don't fetch the tags from GitHub
return callback(true)
end
context.stdio_sink.stdout "Fetching tags from GitHub API...\n"
fetch(
("https://api.github.com/repos/%s/tags"):format(repo),
vim.schedule_wrap(function(err, raw_data)
if err then
context.stdio_sink.stderr(tostring(err) .. "\n")
callback(false)
return
end
local data = Data.json_decode(raw_data)
if vim.tbl_count(data) == 0 then
context.stdio_sink.stderr("No tags found for GitHub repo %s.\n", repo)
callback(false)
return
end
context.requested_server_version = data[1].name
callback(true)
end)
)
end
end
---@param repo string @The GitHub repo ("username/repo").
function M.use_github_release(repo)
---@type ServerInstallerFunction
return function(server, callback, context)
if context.requested_server_version then
log.fmt_debug(
"Requested server version already provided (%s), skipping fetching latest release from GitHub.",
context.requested_server_version
)
-- User has already provided a version - don't fetch the latest version from GitHub
return callback(true)
end
context.stdio_sink.stdout "Fetching latest release version from GitHub API...\n"
fetch(
("https://api.github.com/repos/%s/releases/latest"):format(repo),
vim.schedule_wrap(function(err, response)
if err then
context.stdio_sink.stderr(tostring(err) .. "\n")
return callback(false)
end
local version = Data.json_decode(response).tag_name
log.debug("Resolved latest version", server.name, repo, version)
context.requested_server_version = version
callback(true)
end)
)
end
end
---@param repo string @The GitHub report ("username/repo").
---@param file string|fun(resolved_version: string): string @The name of a file available in the provided repo's GitHub releases.
function M.use_github_release_file(repo, file)
return installers.pipe {
M.use_github_release(repo),
function(server, callback, context)
local function get_download_url(version)
local target_file
if type(file) == "function" then
target_file = file(version)
else
target_file = file
end
if not target_file then
log.fmt_error(
"Unable to find which release file to download. server_name=%s, repo=%s",
server.name,
repo
)
context.stdio_sink.stderr(
(
"Could not find which release file to download. Most likely, the current operating system or architecture (%s) is not supported.\n"
):format(platform.arch)
)
return nil
end
return ("https://github.com/%s/releases/download/%s/%s"):format(repo, version, target_file)
end
local download_url = get_download_url(context.requested_server_version)
if not download_url then
return callback(false)
end
context.github_release_file = download_url
callback(true)
end,
}
end
---Creates an installer that moves the current installation directory to the server's root directory.
function M.promote_install_dir()
---@type ServerInstallerFunction
return vim.schedule_wrap(function(server, callback, context)
if server:promote_install_dir(context.install_dir) then
context.install_dir = server.root_dir
callback(true)
else
context.stdio_sink.stderr(
("Failed to promote temporary install directory to %s.\n"):format(server.root_dir)
)
callback(false)
end
end)
end
---Access the context ojbect to create a new installer.
---@param fn fun(context: ServerInstallContext): ServerInstallerFunction
function M.capture(fn)
---@type ServerInstallerFunction
return function(server, callback, context)
local installer = fn(context)
installer(server, callback, context)
end
end
---Update the context object.
---@param fn fun(context: ServerInstallContext): ServerInstallerFunction
function M.set(fn)
---@type ServerInstallerFunction
return function(_, callback, context)
fn(context)
callback(true)
end
end
---@param rel_path string @The relative path from the current installation working directory.
function M.set_working_dir(rel_path)
---@type ServerInstallerFunction
return vim.schedule_wrap(function(server, callback, context)
local new_dir = path.concat { context.install_dir, rel_path }
log.fmt_debug(
"Changing installation working directory for %s from %s to %s",
server.name,
context.install_dir,
new_dir
)
if not fs.dir_exists(new_dir) then
local ok = pcall(fs.mkdirp, new_dir)
if not ok then
context.stdio_sink.stderr(("Failed to create directory %s.\n"):format(new_dir))
return callback(false)
end
end
context.install_dir = new_dir
callback(true)
end)
end
function M.use_os_distribution()
---Parses the provided contents of an /etc/\*-release file and identifies the Linux distribution.
---@param contents string @The contents of a /etc/\*-release file.
---@return table<string, any>
local function parse_linux_dist(contents)
local lines = vim.split(contents, "\n")
local entries = {}
for i = 1, #lines do
local line = lines[i]
local index = line:find "="
if index then
local key = line:sub(1, index - 1)
local value = line:sub(index + 1)
entries[key] = value
end
end
if entries.ID == "ubuntu" then
-- Parses the Ubuntu OS VERSION_ID into their version components, e.g. "18.04" -> {major=18, minor=04}
local version_id = entries.VERSION_ID:gsub([["]], "")
local version_parts = vim.split(version_id, "%.")
local major = tonumber(version_parts[1])
local minor = tonumber(version_parts[2])
return {
id = "ubuntu",
version_id = version_id,
version = { major = major, minor = minor },
}
else
return {
id = "linux-generic",
}
end
end
return installers.when {
---@type ServerInstallerFunction
linux = function(_, callback, ctx)
local stdio = process.in_memory_sink()
process.spawn("bash", {
args = { "-c", "cat /etc/*-release" },
stdio_sink = stdio.sink,
}, function(success)
if success then
ctx.os_distribution = parse_linux_dist(table.concat(stdio.buffers.stdout, ""))
callback(true)
else
ctx.os_distribution = {
id = "linux-generic",
}
callback(true)
end
end)
end,
mac = function(_, callback, ctx)
ctx.os_distribution = {
id = "macOS",
}
callback(true)
end,
win = function(_, callback, ctx)
ctx.os_distribution = {
id = "windows",
}
callback(true)
end,
}
end
return M
|