-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpackspec.lua
executable file
·57 lines (47 loc) · 1.25 KB
/
packspec.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
local function fatal(s, ...)
print(string.format(s, ...))
os.exit(1)
end
if not arg[1] then
fatal('usage: %s [files...]', arg[0])
end
local jsonschema = require('jsonschema')
local schema = require('packspec.schema')
local validator = jsonschema.generate_validator(schema)
for _, path in ipairs(arg) do
local spec
if path:match('%.lua$') then
local chunk, err = assert(loadfile(path))
if err then
fatal('%s: loading failed\n%s', path, err)
end
spec = {}
setfenv(chunk, spec)
local ok
ok, err = pcall(chunk)
if not ok then
fatal('%s: evaluation failed\n%s', path, err)
end
elseif path:match('%.json$') then
local file, err = assert(io.open(path, 'rb'))
if err then
fatal('%s: failed to open file\n%s', path, err)
end
local json = (file:read('*a'))
file:close()
if not json then
fatal('%s: could not read the file', path)
end
spec, err = vim.json.decode(json)
if err then
fatal('%s: decoding json failed\n%s', path, err)
end
else
fatal('invalid filename, expected lua or json file: %s', path)
end
local ok, err = validator(spec)
if not ok then
fatal('%s: validation failed\n%s', path, err)
end
print(string.format('%s: OK', path))
end