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
|
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 fetch = require "nvim-lsp-installer.core.fetch"
local Data = require "nvim-lsp-installer.data"
local list_find_first = Data.list_find_first
local M = {}
---@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 = vim.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
context.github_repo = repo
callback(true)
end)
)
end
end
---@alias UseGithubReleaseOpts {tag_name_pattern:string}
---@param repo string @The GitHub repo ("username/repo").
---@param opts UseGithubReleaseOpts|nil
function M.use_github_release(repo, opts)
opts = opts or {}
---@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"):format(repo),
vim.schedule_wrap(function(err, response)
if err then
log.fmt_error("Failed to fetch releases for repo=%s", repo)
context.stdio_sink.stderr(tostring(err) .. "\n")
return callback(false)
end
local latest_release = list_find_first(vim.json.decode(response), function(release)
local is_stable_release = not release.prerelease and not release.draft
if opts.tag_name_pattern then
return is_stable_release and release.tag_name:match(opts.tag_name_pattern)
end
return is_stable_release
end)
if not latest_release then
log.fmt_info("Failed to find latest release. repo=%s, opts=%s", repo, opts)
callback(false)
return
end
log.debug("Resolved latest version", server.name, repo, latest_release.tag_name)
context.requested_server_version = latest_release.tag_name
context.github_repo = repo
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.
---@param opts UseGithubReleaseOpts
function M.use_github_release_file(repo, file, opts)
return installers.pipe {
M.use_github_release(repo, opts),
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
---@param fn fun(receipt_builder: InstallReceiptBuilder, ctx: ServerInstallContext)
function M.receipt(fn)
return M.capture(function(ctx)
fn(ctx.receipt, ctx)
return installers.noop
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
function M.use_homebrew_prefix()
return installers.on {
mac = function(_, callback, ctx)
local stdio = process.in_memory_sink()
process.spawn("brew", {
args = { "--prefix" },
stdio_sink = stdio.sink,
}, function(success)
if success then
ctx.homebrew_prefix = vim.trim(table.concat(stdio.buffers.stdout, ""))
callback(true)
else
ctx.stdio_sink.stderr "Failed to locate Homebrew installation.\n"
callback(false)
end
end)
end,
}
end
return M
|