-
Notifications
You must be signed in to change notification settings - Fork 49
/
io.lua
469 lines (413 loc) · 9.1 KB
/
io.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
local uv = vim.loop
local log = require("codeium.log")
local Path = require("plenary.path")
local Job = require("plenary.job")
local curl = require("plenary.curl")
local config = require("codeium.config")
local default_mod = 438 -- 666
local has_http, http = pcall(require, "http")
if has_http then
has_http = http.supported()
end
local M = {}
local function check_job(job, status)
if status == 0 then
return job, nil
else
if job.enable_recording then
return job,
{
code = status,
out = table.concat(job:result(), "\n"),
err = table.concat(job:stderr_result(), "\n"),
}
else
return job, { code = status }
end
end
end
local function check_job_wrap(fn)
return function(job, status)
fn(check_job(job, status))
end
end
function M.executable(path)
local override = config.options.tools[path]
if override then
return vim.fn.executable(override)
end
return vim.fn.executable(path)
end
function M.touch(path)
local fd, err = uv.fs_open(path, "w+", default_mod)
if err or not fd then
log.error("failed to create file ", path, ": ", err)
return nil
end
local stat
stat, err = uv.fs_fstat(fd)
uv.fs_close(fd)
if err or not stat then
log.error("could not stat ", path, ": ", err)
return nil
end
return stat.mtime.sec
end
function M.stat_mtime(path)
local stat, err = uv.fs_stat(path)
if err or not stat then
log.error("could not stat ", path, ": ", err)
return nil
end
return stat.mtime.sec
end
function M.exists(path)
local stat, err = uv.fs_stat(path)
if err or not stat then
return false
end
return stat.type == "file"
end
function M.readdir(path)
local fd, err = uv.fs_opendir(path, nil, 1000)
if err then
log.error("could not open dir ", path, ": ", err)
return {}
end
local entries, err = uv.fs_readdir(fd)
uv.fs_closedir(fd)
if err then
log.error("could not read dir ", path, ":", err)
return {}
end
return entries
end
function M.tempdir(suffix)
local dir = vim.fn.tempname() .. (suffix or "")
vim.fn.mkdir(dir, "p")
return dir
end
function M.timer(delay, rep, fn)
local timer = uv.new_timer()
fn = vim.schedule_wrap(fn)
local function cancel()
if timer then
pcall(timer.stop, timer)
pcall(timer.close, timer)
end
timer = nil
end
local function callback()
if timer then
fn(cancel)
end
end
timer:start(delay, rep, callback)
return cancel
end
function M.read_json(path)
local fd, err, errcode = uv.fs_open(path, "r", default_mod)
if err or not fd then
if errcode == "ENOENT" then
return nil, errcode
end
log.error("could not open ", path, ": ", err)
return nil, errcode
end
local stat, err, errcode = uv.fs_fstat(fd)
if err or not stat then
uv.fs_close(fd)
log.error("could not stat ", path, ": ", err)
return nil, errcode
end
local contents, err, errcode = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd)
if err then
log.error("could not read ", path, ": ", err)
return nil, errcode
end
local ok, json = pcall(vim.fn.json_decode, contents)
if not ok then
log.error("could not parse json in ", path, ": ", err)
return nil, json
end
return json, nil
end
function M.write_json(path, json)
local ok, text = pcall(vim.fn.json_encode, json)
if not ok then
log.error("could not encode JSON ", path, ": ", text)
return nil, text
end
local parent = Path:new(path):parent().filename
local ok, err = pcall(vim.fn.mkdir, parent, "p")
if not ok then
log.error("could not create directory ", parent, ": ", err)
return nil, err
end
local fd, err, errcode = uv.fs_open(path, "w+", default_mod)
if err or not fd then
log.error("could not open ", path, ": ", err)
return nil, errcode
end
local size, err, errcode = uv.fs_write(fd, text, 0)
uv.fs_close(fd)
if err then
log.error("could not write ", path, ": ", err)
return nil, errcode
end
return size, nil
end
function M.get_command_output(...)
local job = M.job({ ... })
local output, err = job:sync(1000)
if err and err ~= 0 then
log.debug("job failed ", err)
return nil, err
end
local result, err = check_job(job, err)
if err then
log.debug("job failed: ", err)
return nil, err
else
return output[1], nil
end
end
local system_info_cache = nil
function M.get_system_info()
if system_info_cache then
return system_info_cache
end
local uname = vim.loop.os_uname()
local os = uname.sysname
if os == "Linux" then
os = "linux"
elseif os == "Darwin" then
os = "macos"
elseif os == "Windows_NT" then
os = "windows"
else
require("codeium.notify").warn("Unknown sysname: ", os)
end
local arch = uname.machine
if os == "Darwin" and arch == "arm64" then
arch = "aarch64"
end
system_info_cache = {
os = os,
arch = arch,
is_arm = arch == "arm",
is_aarch = arch == "aarch64",
is_x86 = arch == "x86_64",
is_unix = os == "linux" or os == "macos",
is_windows = os == "windows",
}
return system_info_cache
end
-- @return plenary.Job
function M.job(cmd)
local o = config.options
local tool_name = cmd.tool_name or cmd[1]
local tool = o.tools[tool_name]
local wrapper
if tool then
wrapper = tool
else
wrapper = o.wrapper
end
if wrapper then
if type(wrapper) == "string" then
wrapper = { wrapper }
end
local wrap = #wrapper
local num = #cmd
local offset
if tool then
-- tools directly replace the binary
offset = -1
else
offset = 0
end
for i = num, 1, -1 do
cmd[i + wrap + offset] = cmd[i]
end
for i = 1, wrap do
cmd[i] = wrapper[i]
end
end
-- Remove custom properties
cmd.tool_name = nil
local result = {}
result.args = {}
for k, v in pairs(cmd) do
if type(k) == "number" then
if k == 1 then
result.command = v
else
result.args[k - 1] = v
end
elseif type(v) == "function" then
if k == "on_exit" then
v = check_job_wrap(v)
end
result[k] = vim.schedule_wrap(v)
else
result[k] = v
end
end
return Job:new(result)
end
function M.generate_uuid()
local uuid
local os = M.get_system_info()
if os.is_windows then
uuid = string.gsub("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx", "[xy]", function(c)
return string.format("%x", (c == "x") and (math.random(16) - 1) or (((math.random(16) - 1) % 4) + 8))
end)
end
if not M.executable("uuidgen") then
error("uuiden could not be found")
else
local err
uuid, err = M.get_command_output("uuidgen")
if err then
error("failed to generate UUID: " .. vim.inspect(err))
end
end
return uuid
end
function M.gunzip(path, callback)
local os = M.get_system_info()
if os.is_windows then
M.job({
"powershell.exe",
"-noprofile",
"-command",
"\"Expand-Archive -Path '",
path,
"' -DestinationPath '",
".",
"'\"",
on_exit = callback,
}):start()
else
if not M.executable("gzip") then
callback(nil, "gzip could not be found")
else
M.job({
"gzip",
"-d",
path,
on_exit = callback,
}):start()
end
end
end
function M.set_executable(path, callback)
if M.get_system_info().os == "windows" then
-- determined by the filename
-- improvement: potentially unblock the file
callback(nil, nil)
else
M.job({
"chmod",
"+x",
path,
on_exit = callback,
}):start()
end
end
function M.download(url, path, callback)
if has_http then
http.request({
http.methods.GET,
url,
nil,
path,
callback = vim.schedule_wrap(function(err, resp)
if err then
callback(nil, "failed to download file " .. err)
elseif resp.code < 200 or resp.code > 399 then
callback(resp, "http response " .. resp.status)
else
callback(resp, nil)
end
end),
})
else
curl.get(url, {
output = path,
callback = vim.schedule_wrap(function(out)
if out.exit ~= 0 then
callback(out, "curl exited with status code " .. out)
elseif out.status < 200 or out.status > 399 then
callback(out, "http response " .. out.status)
else
callback(out, nil)
end
end),
})
end
end
function M.post(url, params)
if type(params.body) == "table" then
params.headers = params.headers or {}
params.headers["content-type"] = params.headers["content-type"] or "application/json"
params.body = vim.fn.json_encode(params.body)
end
local cb = vim.schedule_wrap(params.callback)
if has_http then
params[1] = http.methods.POST
params[2] = url
params[3] = params.body
params.body = nil
params.callback = function(err, resp)
if err then
cb(nil, {
code = 1,
err = err,
})
elseif resp.code > 299 then
cb(resp.body, {
code = 0,
status = resp.code,
response = resp,
out = resp.body,
})
else
cb(resp.body, nil)
end
end
http.request(params)
else
params.callback = function(out, _)
if out.exit ~= 0 then
cb(nil, {
code = out.exit,
err = "curl failed",
})
elseif out.status > 299 then
cb(out.body, {
code = 0,
status = out.status,
response = out,
out = out.body,
})
else
cb(out.body, nil)
end
end
curl.post(url, params)
end
end
function M.shell_open(...)
local info = M.get_system_info()
if info.os == "linux" then
return M.get_command_output("xdg-open", ...)
elseif info.os == "macos" then
return M.get_command_output("/usr/bin/open", ...)
else
return M.get_command_output("start", ...)
end
end
return M