-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
76 lines (60 loc) · 1.58 KB
/
util.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
local M = {}
function M.exec(cmd, opts)
local proc = process.start(cmd, opts or {})
if proc then
while proc:running() do
coroutine.yield(0.1)
end
return (proc:read_stdout() or '<no stdout>') .. (proc:read_stderr() or '<no stderr>'), proc:returncode()
end
return nil
end
function M.isURL(url)
return url:match '^%w+://'
end
function M.slugify(url)
return url:match '[^/]+/[^/]+$'
end
--- Returns a proper plugin name based on a provided URL
--- It will remove the `.lxl` suffix or the `lite-xl-` prefix
function M.plugName(url)
local name = string.lower(url:match '[^/]+$')
return name:gsub('.lxl$', ''):gsub('^lite%-xl%-', '')
end
function M.fileExists(path)
local f = io.open(path)
if f then
f:close()
return true
end
return false
end
function M.gitCmd(args, dir)
return M.exec {'git', '-C', dir, table.unpack(args)}
end
function M.dehexify(hex)
return (hex:gsub('%x%x', function(digits) return string.char(tonumber(digits, 16)) end))
end
function M.hexify(str)
return (str:gsub('.', function(char) return string.format('%02x', char:byte()) end))
end
function M.repoURL(repo)
return repo:match('^%w+://[^:]+')
end
function M.repoTag(repo)
return repo:match('^%w+://.+:(.+)')
end
function M.repoDir(repo)
return M.hexify(M.repoURL(repo))
end
function M.join(parts)
local str = ''
local sepPattern = string.format('%s$', '%' .. PATHSEP)
for i, part in ipairs(parts) do
local sepMatch = part:match(sepPattern)
str = str .. part .. (sepMatch or i == #parts and '' or PATHSEP)
end
str = str:gsub(string.format('%s$', '%' .. PATHSEP), '')
return str
end
return M