Skip to content

Commit 1ec33e6

Browse files
committed
convert config.lua to config.json
1 parent 748f8b9 commit 1ec33e6

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

locale/en-us/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY =
547547
'Do you need to configure your work environment as `{}`?'
548548
WINDOW_SEARCHING_IN_FILES =
549549
'Searching in files...'
550+
WINDOW_CONFIG_LUA_DEPRECATED =
551+
'`config.lua` is deprecated, please use `config.json` instead.'
552+
WINDOW_CONVERT_CONFIG_LUA =
553+
'Convert to `config.json`'
550554

551555
CONFIG_LOAD_FAILED =
552556
'Unable to read the settings file: {}'

locale/pt-br/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY =
547547
'Você precisa configurar seu ambiente de trabalho como `{}`?'
548548
WINDOW_SEARCHING_IN_FILES = -- TODO: need translate!
549549
'Procurando nos arquivos...'
550+
WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate!
551+
'`config.lua` is deprecated, please use `config.json` instead.'
552+
WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate!
553+
'Convert to `config.json`'
550554

551555
CONFIG_LOAD_FAILED =
552556
'Não é possível ler o arquivo de configurações: {}'

locale/zh-cn/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY =
547547
'是否需要将你的工作环境配置为 `{}` ?'
548548
WINDOW_SEARCHING_IN_FILES =
549549
'正在文件中搜索...'
550+
WINDOW_CONFIG_LUA_DEPRECATED =
551+
'`config.lua` 已废弃,请改用 `config.json` 。'
552+
WINDOW_CONVERT_CONFIG_LUA =
553+
'转换为 `config.json`'
550554

551555
CONFIG_LOAD_FAILED =
552556
'无法读取设置文件:{}'

locale/zh-tw/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY =
547547
'是否需要將你的工作環境配置為 `{}` ?'
548548
WINDOW_SEARCHING_IN_FILES =
549549
'正在檔案中搜尋...'
550+
WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate!
551+
'`config.lua` is deprecated, please use `config.json` instead.'
552+
WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate!
553+
'Convert to `config.json`'
550554

551555
CONFIG_LOAD_FAILED =
552556
'無法讀取設定檔案:{}'

script/library.lua

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local ws = require 'workspace.workspace'
1515
local scope = require 'workspace.scope'
1616
local inspect = require 'inspect'
1717
local jsonc = require 'jsonc'
18+
local json = require 'json'
1819

1920
local m = {}
2021

@@ -275,7 +276,8 @@ local function initBuiltIn(uri)
275276
end
276277

277278
---@param libraryDir fs.path
278-
local function loadSingle3rdConfig(libraryDir)
279+
---@return table?
280+
local function loadSingle3rdConfigFromJson(libraryDir)
279281
local path = libraryDir / 'config.json'
280282
local configText = fsu.loadFile(path)
281283
if not configText then
@@ -289,6 +291,65 @@ local function loadSingle3rdConfig(libraryDir)
289291
return nil
290292
end
291293

294+
if type(cfg) ~= 'table' then
295+
log.error('config.json must be an object:', libraryDir:string())
296+
return nil
297+
end
298+
299+
return cfg
300+
end
301+
302+
---@param libraryDir fs.path
303+
---@return table?
304+
local function loadSingle3rdConfigFromLua(libraryDir)
305+
local path = libraryDir / 'config.lua'
306+
local configText = fsu.loadFile(path)
307+
if not configText then
308+
return nil
309+
end
310+
311+
local env = setmetatable({}, { __index = _G })
312+
local f, err = load(configText, '@' .. libraryDir:string(), 't', env)
313+
if not f then
314+
log.error('Load config.lua failed at:', libraryDir:string(), err)
315+
return nil
316+
end
317+
318+
local suc = xpcall(f, function (err)
319+
log.error('Load config.lua failed at:', libraryDir:string(), err)
320+
end)
321+
322+
if not suc then
323+
return nil
324+
end
325+
326+
local cfg = {}
327+
for k, v in pairs(env) do
328+
cfg[k] = v
329+
end
330+
331+
return cfg
332+
end
333+
334+
---@param libraryDir fs.path
335+
local function loadSingle3rdConfig(libraryDir)
336+
local cfg = loadSingle3rdConfigFromJson(libraryDir)
337+
if not cfg then
338+
cfg = loadSingle3rdConfigFromLua(libraryDir)
339+
if not cfg then
340+
return
341+
end
342+
local jsonbuf = json.beautify(cfg)
343+
client.requestMessage('Info', lang.script.WINDOW_CONFIG_LUA_DEPRECATED, {
344+
lang.script.WINDOW_CONVERT_CONFIG_LUA,
345+
}, function (action, index)
346+
if index == 1 and jsonbuf then
347+
fsu.saveFile(libraryDir / 'config.json', jsonbuf)
348+
fsu.fileRemove(libraryDir / 'config.lua')
349+
end
350+
end)
351+
end
352+
292353
cfg.path = libraryDir:filename():string()
293354
cfg.name = cfg.name or cfg.path
294355

0 commit comments

Comments
 (0)