-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathstd.lua
267 lines (249 loc) · 7.64 KB
/
std.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
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local a = require "mason-core.async"
local fetch = require "mason-core.fetch"
local installer = require "mason-core.installer"
local log = require "mason-core.log"
local path = require "mason-core.path"
local platform = require "mason-core.platform"
local powershell = require "mason-core.managers.powershell"
local M = {}
---@async
---@param rel_path string
---@nodiscard
local function unpack_7z(rel_path)
log.fmt_debug("std: unpack_7z %s", rel_path)
local ctx = installer.context()
return ctx.spawn["7z"] { "x", "-y", "-r", rel_path }
end
---@async
---@param rel_path string
---@nodiscard
local function unpack_peazip(rel_path)
log.fmt_debug("std: unpack_peazip %s", rel_path)
local ctx = installer.context()
return ctx.spawn.peazip { "-ext2here", path.concat { ctx.cwd:get(), rel_path } } -- peazip requires absolute paths
end
---@async
---@param rel_path string
---@nodiscard
local function wzunzip(rel_path)
log.fmt_debug("std: wzunzip %s", rel_path)
local ctx = installer.context()
return ctx.spawn.wzunzip { rel_path }
end
---@async
---@param rel_path string
---@nodiscard
local function unpack_winrar(rel_path)
log.fmt_debug("std: unpack_winrar %s", rel_path)
local ctx = installer.context()
return ctx.spawn.winrar { "e", rel_path }
end
---@async
---@param rel_path string
---@nodiscard
local function gunzip_unix(rel_path)
log.fmt_debug("std: gunzip_unix %s", rel_path)
local ctx = installer.context()
return ctx.spawn.gzip { "-d", rel_path }
end
---@async
---@param rel_path string
---@nodiscard
local function unpack_arc(rel_path)
log.fmt_debug("std: unpack_arc %s", rel_path)
local ctx = installer.context()
return ctx.spawn.arc { "unarchive", rel_path }
end
---@param rel_path string
---@return Result
local function win_decompress(rel_path)
local ctx = installer.context()
return gunzip_unix(rel_path)
:or_else(function()
return unpack_7z(rel_path)
end)
:or_else(function()
return unpack_peazip(rel_path)
end)
:or_else(function()
return wzunzip(rel_path)
end)
:or_else(function()
return unpack_winrar(rel_path)
end)
:on_success(function()
pcall(function()
ctx.fs:unlink(rel_path)
end)
end)
end
---@async
---@param url string
---@param out_file string
---@nodiscard
function M.download_file(url, out_file)
log.fmt_debug("std: downloading file %s", url, out_file)
local ctx = installer.context()
ctx.stdio_sink.stdout(("Downloading file %q…\n"):format(url))
return fetch(url, {
out_file = path.concat { ctx.cwd:get(), out_file },
}):map_err(function(err)
return ("%s\nFailed to download file %q."):format(err, url)
end)
end
---@async
---@param rel_path string
---@nodiscard
local function untar(rel_path)
log.fmt_debug("std: untar %s", rel_path)
local ctx = installer.context()
a.scheduler()
local tar = vim.fn.executable "gtar" == 1 and "gtar" or "tar"
return ctx.spawn[tar]({ "--no-same-owner", "-xvf", rel_path }):on_success(function()
pcall(function()
ctx.fs:unlink(rel_path)
end)
end)
end
---@async
---@param rel_path string
---@nodiscard
local function unzip(rel_path)
log.fmt_debug("std: unzip %s", rel_path)
local ctx = installer.context()
return platform.when {
unix = function()
return ctx.spawn.unzip({ "-d", ".", rel_path }):on_success(function()
pcall(function()
ctx.fs:unlink(rel_path)
end)
end)
end,
win = function()
return Result.pcall(function()
-- Expand-Archive seems to be hard-coded to only allow .zip extensions. Bit weird but ok.
if not _.matches("%.zip$", rel_path) then
local zip_file = ("%s.zip"):format(rel_path)
ctx.fs:rename(rel_path, zip_file)
return zip_file
end
return rel_path
end):and_then(function(zip_file)
return powershell
.command(
("Microsoft.PowerShell.Archive\\Expand-Archive -Path %q -DestinationPath ."):format(zip_file),
{},
ctx.spawn
)
:on_success(function()
pcall(function()
ctx.fs:unlink(zip_file)
end)
end)
end)
end,
}
end
---@async
---@param rel_path string
---@nodiscard
local function gunzip(rel_path)
log.fmt_debug("std: gunzip %s", rel_path)
return platform.when {
unix = function()
return gunzip_unix(rel_path)
end,
win = function()
return win_decompress(rel_path)
end,
}
end
---@async
---@param rel_path string
---@return Result
---@nodiscard
local function untar_compressed(rel_path)
log.fmt_debug("std: untar_compressed %s", rel_path)
return platform.when {
unix = function()
return untar(rel_path)
end,
win = function()
return win_decompress(rel_path)
:and_then(function()
return untar(_.gsub("%.tar%..*$", ".tar", rel_path))
end)
:or_else(function()
-- arc both decompresses and unpacks tar in one go
return unpack_arc(rel_path)
end)
end,
}
end
---@async
---@param rel_path string
---@return Result
---@nodiscard
local function untar_zst(rel_path)
return platform.when {
unix = function()
return untar(rel_path)
end,
win = function()
local ctx = installer.context()
local uncompressed_tar = rel_path:gsub("%.zst$", "")
ctx.spawn.zstd { "-dfo", uncompressed_tar, rel_path }
ctx.fs:unlink(rel_path)
return untar(uncompressed_tar)
end,
}
end
-- Order is important.
local unpack_by_filename = _.cond {
{ _.matches "%.tar$", untar },
{ _.matches "%.tar%.gz$", untar },
{ _.matches "%.tar%.bz2$", untar },
{ _.matches "%.tar%.xz$", untar_compressed },
{ _.matches "%.tar%.zst$", untar_zst },
{ _.matches "%.zip$", unzip },
{ _.matches "%.vsix$", unzip },
{ _.matches "%.nupkg$", unzip },
{ _.matches "%.gz$", gunzip },
{ _.T, _.compose(Result.success, _.format "%q doesn't need unpacking.") },
}
---@async
---@param rel_path string The relative path to the file to unpack.
---@nodiscard
function M.unpack(rel_path)
log.fmt_debug("std: unpack %s", rel_path)
local ctx = installer.context()
ctx.stdio_sink.stdout((("Unpacking %q…\n"):format(rel_path)))
return unpack_by_filename(rel_path)
end
---@async
---@param git_url string
---@param opts? { rev?: string, recursive?: boolean }
---@nodiscard
function M.clone(git_url, opts)
opts = opts or {}
log.fmt_debug("std: clone %s %s", git_url, opts)
local ctx = installer.context()
ctx.stdio_sink.stdout((("Cloning git repository %q…\n"):format(git_url)))
return Result.try(function(try)
try(ctx.spawn.git {
"clone",
"--depth",
"1",
opts.recursive and "--recursive" or vim.NIL,
git_url,
".",
})
if opts.rev then
try(ctx.spawn.git { "fetch", "--depth", "1", "origin", opts.rev })
try(ctx.spawn.git { "checkout", "--quiet", "FETCH_HEAD" })
end
end)
end
return M