-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
build-settings.lua
108 lines (99 loc) · 3.18 KB
/
build-settings.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
local fs = require 'bee.filesystem'
local currentPath = debug.getinfo(1, 'S').source:sub(2)
local rootPath = currentPath:gsub('[^/\\]-$', '')
package.path = package.path
.. ';' .. rootPath .. '?.lua'
.. ';' .. rootPath .. 'server/script/?.lua'
local json = require 'json-beautify'
local configuration = require 'server.tools.configuration'
local fsu = require 'fs-utility'
local lloader = require 'locale-loader'
local diagd = require 'proto.diagnostic'
local util = require 'utility'
local function addSplited(t, key, value)
t[key] = value
for pos in key:gmatch '()%.' do
local left = key:sub(1, pos - 1)
local right = key:sub(pos + 1)
local nt = t[left] or {
properties = {}
}
t[left] = nt
addSplited(nt.properties, right, value)
end
end
local function copyWithNLS(t, callback)
local nt = {}
local mt = getmetatable(t)
if mt then
setmetatable(nt, mt)
end
for k, v in pairs(t) do
if type(v) == 'string' then
v = callback(v) or v
elseif type(v) == 'table' then
v = copyWithNLS(v, callback)
end
nt[k] = v
if type(k) == 'string' and k:sub(1, #'Lua.') == 'Lua.' then
local shortKey = k:sub(#'Lua.' + 1)
local ref = {
['$ref'] = '#/properties/' .. shortKey
}
addSplited(nt, shortKey, ref)
nt[k] = nil
nt[shortKey] = v
end
end
return nt
end
local encodeOption = {
newline = '\r\n',
indent = ' ',
}
local function mergeDiagnosticGroupLocale(locale)
for groupName, names in pairs(diagd.diagnosticGroups) do
local key = ('config.diagnostics.%s'):format(groupName)
local list = {}
for name in util.sortPairs(names) do
list[#list+1] = ('* %s'):format(name)
end
local desc = table.concat(list, '\n')
locale[key] = desc
end
end
for dirPath in fs.pairs(fs.path 'server/locale') do
local lang = dirPath:filename():string()
local nlsPath = dirPath / 'setting.lua'
local text = fsu.loadFile(nlsPath)
if not text then
goto CONTINUE
end
local nls = lloader(text, nlsPath:string())
-- add `config.diagnostics.XXX`
mergeDiagnosticGroupLocale(nls)
local setting = {
title = 'setting',
description = 'Setting of sumneko.lua',
type = 'object',
properties = copyWithNLS(configuration, function (str)
return str:gsub('^%%(.+)%%$', function (key)
if not nls[key] then
nls[key] = "TODO: Needs documentation"
end
return nls[key]
end)
end),
}
local schemaName, nlsName
if lang == 'en-us' then
schemaName = 'setting/schema.json'
nlsName = 'package.nls.json'
else
schemaName = 'setting/schema-' .. lang .. '.json'
nlsName = 'package.nls.' .. lang .. '.json'
end
fsu.saveFile(fs.path(schemaName), json.beautify(setting, encodeOption))
fsu.saveFile(fs.path(nlsName), json.beautify(nls, encodeOption))
::CONTINUE::
end