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
|
local InstallContextCwd = require "mason-core.installer.context.InstallContextCwd"
local InstallContextFs = require "mason-core.installer.context.InstallContextFs"
local InstallContextSpawn = require "mason-core.installer.context.InstallContextSpawn"
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local a = require "mason-core.async"
local fetch = require "mason-core.fetch"
local fs = require "mason-core.fs"
local log = require "mason-core.log"
local path = require "mason-core.path"
local platform = require "mason-core.platform"
local receipt = require "mason-core.receipt"
---@class InstallContext
---@field receipt InstallReceiptBuilder
---@field fs InstallContextFs
---@field location InstallLocation
---@field spawn InstallContextSpawn
---@field handle InstallHandle
---@field package AbstractPackage
---@field cwd InstallContextCwd
---@field opts PackageInstallOpts
---@field stdio_sink StdioSink
---@field links { bin: table<string, string>, share: table<string, string>, opt: table<string, string> }
local InstallContext = {}
InstallContext.__index = InstallContext
---@param handle InstallHandle
---@param opts PackageInstallOpts
function InstallContext:new(handle, opts)
local cwd = InstallContextCwd:new(handle)
local spawn = InstallContextSpawn:new(handle, cwd, false)
local fs = InstallContextFs:new(cwd)
return setmetatable({
cwd = cwd,
spawn = spawn,
handle = handle,
location = handle.location, -- for convenience
package = handle.package, -- for convenience
fs = fs,
receipt = receipt.InstallReceiptBuilder:new(),
stdio_sink = handle.stdio_sink,
links = {
bin = {},
share = {},
opt = {},
},
opts = opts,
}, InstallContext)
end
---@async
---@param url string
---@param opts? FetchOpts
function InstallContext:fetch(url, opts)
opts = opts or {}
if opts.out_file then
opts.out_file = path.concat { self.cwd:get(), opts.out_file }
end
return fetch(url, opts):get_or_throw()
end
---@async
function InstallContext:promote_cwd()
local cwd = self.cwd:get()
local install_path = self:get_install_path()
if install_path == cwd then
log.fmt_debug("cwd %s is already promoted", cwd)
return
end
log.fmt_debug("Promoting cwd %s to %s", cwd, install_path)
-- 1. Uninstall any existing installation
if self.handle.package:is_installed() then
a.wait(function(resolve, reject)
self.handle.package:uninstall({ bypass_permit = true }, function(success, result)
if not success then
reject(result)
else
resolve()
end
end)
end)
end
-- 2. Prepare for renaming cwd to destination
if platform.is.unix then
-- Some Unix systems will raise an error when renaming a directory to a destination that does not already exist.
fs.async.mkdir(install_path)
end
-- 3. Update cwd
self.cwd:set(install_path)
-- 4. Move the cwd to the final installation directory
local rename_success, rename_err = pcall(fs.async.rename, cwd, install_path)
if not rename_success then
-- On some file systems, we cannot create the directory before renaming. Therefore, remove it and then rename.
log.trace("Call to uv_fs_rename() while promoting cwd failed.", rename_err)
fs.async.rmdir(install_path)
assert(fs.async.dir_exists(cwd), "Current working directory no longer exists after retrying uv_fs_rename().")
fs.async.rename(cwd, install_path)
end
end
---@param rel_path string The relative path from the current working directory to change cwd to. Will only restore to the initial cwd after execution of fn (if provided).
---@param fn async (fun(): any)? The function to run in the context of the given path.
function InstallContext:chdir(rel_path, fn)
local old_cwd = self.cwd:get()
self.cwd:set(path.concat { old_cwd, rel_path })
if fn then
local ok, result = pcall(fn)
self.cwd:set(old_cwd)
if not ok then
error(result, 0)
end
return result
end
end
---@async
---@param fn fun(resolve: fun(result: any), reject: fun(error: any))
function InstallContext:await(fn)
return a.wait(fn)
end
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param script_rel_path string Relative path to the Node.js script.
function InstallContext:write_node_exec_wrapper(new_executable_rel_path, script_rel_path)
if not self.fs:file_exists(script_rel_path) then
error(("Cannot write Node exec wrapper for path %q as it doesn't exist."):format(script_rel_path), 0)
end
return self:write_shell_exec_wrapper(
new_executable_rel_path,
("node %q"):format(path.concat {
self:get_install_path(),
script_rel_path,
})
)
end
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param script_rel_path string Relative path to the Node.js script.
function InstallContext:write_ruby_exec_wrapper(new_executable_rel_path, script_rel_path)
if not self.fs:file_exists(script_rel_path) then
error(("Cannot write Ruby exec wrapper for path %q as it doesn't exist."):format(script_rel_path), 0)
end
return self:write_shell_exec_wrapper(
new_executable_rel_path,
("ruby %q"):format(path.concat {
self:get_install_path(),
script_rel_path,
})
)
end
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param script_rel_path string Relative path to the PHP script.
function InstallContext:write_php_exec_wrapper(new_executable_rel_path, script_rel_path)
if not self.fs:file_exists(script_rel_path) then
error(("Cannot write PHP exec wrapper for path %q as it doesn't exist."):format(script_rel_path), 0)
end
return self:write_shell_exec_wrapper(
new_executable_rel_path,
("php %q"):format(path.concat {
self:get_install_path(),
script_rel_path,
})
)
end
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param module string The python module to call.
function InstallContext:write_pyvenv_exec_wrapper(new_executable_rel_path, module)
local pypi = require "mason-core.installer.managers.pypi"
local module_exists, module_err = pcall(function()
local result =
self.spawn.python { "-c", ("import %s"):format(module), with_paths = { pypi.venv_path(self.cwd:get()) } }
if not self.spawn.strict_mode then
result:get_or_throw()
end
end)
if not module_exists then
log.fmt_error("Failed to find module %q for package %q. %s", module, self.package, module_err)
error(("Cannot write Python exec wrapper for module %q as it doesn't exist."):format(module), 0)
end
return self:write_shell_exec_wrapper(
new_executable_rel_path,
("%q -m %s"):format(
path.concat {
pypi.venv_path(self:get_install_path()),
"python",
},
module
)
)
end
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param target_executable_rel_path string
function InstallContext:write_exec_wrapper(new_executable_rel_path, target_executable_rel_path)
if not self.fs:file_exists(target_executable_rel_path) then
error(("Cannot write exec wrapper for path %q as it doesn't exist."):format(target_executable_rel_path), 0)
end
if platform.is.unix then
self.fs:chmod_exec(target_executable_rel_path)
end
return self:write_shell_exec_wrapper(
new_executable_rel_path,
("%q"):format(path.concat {
self:get_install_path(),
target_executable_rel_path,
})
)
end
local BASH_TEMPLATE = _.dedent [[
#!/usr/bin/env bash
%s
exec %s "$@"
]]
local BATCH_TEMPLATE = _.dedent [[
@ECHO off
%s
%s %%*
]]
---@param new_executable_rel_path string Relative path to the executable file to create.
---@param command string The shell command to run.
---@param env table<string, string>?
---@return string # The created executable filename.
function InstallContext:write_shell_exec_wrapper(new_executable_rel_path, command, env)
if self.fs:file_exists(new_executable_rel_path) or self.fs:dir_exists(new_executable_rel_path) then
error(("Cannot write exec wrapper to %q because the file already exists."):format(new_executable_rel_path), 0)
end
return platform.when {
unix = function()
local formatted_envs = _.map(function(pair)
local var, value = pair[1], pair[2]
return ("export %s=%q"):format(var, value)
end, _.to_pairs(env or {}))
self.fs:write_file(new_executable_rel_path, BASH_TEMPLATE:format(_.join("\n", formatted_envs), command))
self.fs:chmod_exec(new_executable_rel_path)
return new_executable_rel_path
end,
win = function()
local executable_file = ("%s.cmd"):format(new_executable_rel_path)
local formatted_envs = _.map(function(pair)
local var, value = pair[1], pair[2]
return ("SET %s=%s"):format(var, value)
end, _.to_pairs(env or {}))
self.fs:write_file(executable_file, BATCH_TEMPLATE:format(_.join("\n", formatted_envs), command))
return executable_file
end,
}
end
---@param executable string
---@param rel_path string
function InstallContext:link_bin(executable, rel_path)
self.links.bin[executable] = rel_path
return self
end
InstallContext.CONTEXT_REQUEST = {}
---@generic T
---@param fn fun(context: InstallContext): T
---@return T
function InstallContext:execute(fn)
local thread = coroutine.create(function(...)
-- We wrap the function to allow it to be a spy instance (in which case it's not actually a function, but a
-- callable metatable - coroutine.create strictly expects functions only)
return fn(...)
end)
local step
local ret_val
step = function(...)
local ok, result = coroutine.resume(thread, ...)
if not ok then
error(result, 0)
elseif result == InstallContext.CONTEXT_REQUEST then
step(self)
elseif coroutine.status(thread) == "suspended" then
-- yield to parent coroutine
step(coroutine.yield(result))
else
ret_val = result
end
end
step(self)
return ret_val
end
---@async
function InstallContext:build_receipt()
log.fmt_debug("Building receipt for %s", self.package)
return Result.pcall(function()
return self.receipt:with_name(self.package.name):with_completion_time(vim.loop.gettimeofday()):build()
end)
end
function InstallContext:get_install_path()
return self.location:package(self.package.name)
end
return InstallContext
|