-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathio.lua
More file actions
460 lines (402 loc) · 9.71 KB
/
io.lua
File metadata and controls
460 lines (402 loc) · 9.71 KB
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
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 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.file_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 or not entries 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 = uv.os_uname()
local os = uname.sysname
if os == "Linux" then
os = "linux"
elseif os == "Darwin" then
os = "macos"
elseif os == "Windows_NT" or string.find(os, "MINGW32_NT") then
os = "windows"
else
require("codeium.notify").warn("Unknown sysname: ", os)
end
local arch = uname.machine
if os == "macos" 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[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
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()
return 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
function M.gunzip(path, callback)
if M.executable("gzip") then
M.job({
"gzip",
"-d",
path,
on_exit = callback,
}):start()
return
end
local function expandFile(infile)
local scriptDirectory = debug.getinfo(1, "S").source:match("^@(.*/)[^/]+$")
local command = "& { . "
.. vim.fn.shellescape(scriptDirectory .. "../powershell/gzip.ps1")
.. "; Expand-File "
.. vim.fn.shellescape(infile)
.. "}"
local output = vim.fn.system(command)
if vim.v.shell_error ~= 0 then
error("Failed to expand file: " .. output)
end
end
local shell = vim.o.shell
local shellcmdflag = vim.o.shellcmdflag
local shellredir = vim.o.shellredir
local shellpipe = vim.o.shellpipe
local shellquote = vim.o.shellquote
local shellxquote = vim.o.shellxquote
local pwshCoreAvailable = vim.fn.executable("pwsh")
local isPowershell = vim.o.shell == "pwsh"
or vim.o.shell == "pwsh.exe"
or vim.o.shell == "powershell"
or vim.o.shell == "powershell.exe"
if not isPowershell then
if pwshCoreAvailable then
vim.o.shell = "pwsh"
else
vim.o.shell = "powershell"
end
vim.o.shellcmdflag =
"-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;"
vim.o.shellredir = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode"
vim.o.shellpipe = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode"
vim.o.shellquote = ""
vim.o.shellxquote = ""
end
isPowershell = vim.o.shell == "pwsh"
or vim.o.shell == "pwsh.exe"
or vim.o.shell == "powershell"
or vim.o.shell == "powershell.exe"
if isPowershell then
expandFile(path)
callback()
else
callback(nil, "gzip could not be found, powershell was unable to be run")
end
vim.o.shell = shell
vim.o.shellcmdflag = shellcmdflag
vim.o.shellredir = shellredir
vim.o.shellpipe = shellpipe
vim.o.shellquote = shellquote
vim.o.shellxquote = shellxquote
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)
return
end
M.job({
"chmod",
"+x",
path,
on_exit = callback,
}):start()
end
function M.download(url, path, callback)
curl.get(url, {
output = path,
compressed = false,
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
function M.post(url, params)
local tmpfname = os.tmpname()
if type(params.body) == "table" then
local ok, json = pcall(vim.fn.json_encode, params.body)
if not ok then
log.error("Failed to encode Codeium payload (invalid UTF-8?)")
return
end
local f = io.open(tmpfname, "w+")
if f == nil then
log.error("Cannot open temporary message file: " .. tmpfname)
return
end
f:write(json)
f:close()
params.headers = params.headers or {}
params.headers["content-type"] = params.headers["content-type"] or "application/json"
params.compressed = false
params.body = tmpfname
end
local cb = vim.schedule_wrap(params.callback)
params.callback = function(out, _)
os.remove(tmpfname)
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
function M.shell_open(url)
local info = M.get_system_info()
if info.os == "linux" then
return M.get_command_output("xdg-open", url)
elseif info.os == "macos" then
return M.get_command_output("/usr/bin/open", url)
else
return M.get_command_output("cmd", "/C start " .. url:gsub("&", "^&"))
end
end
return M