diff options
-rw-r--r-- | locale/en-us/script.lua | 4 | ||||
-rw-r--r-- | locale/pt-br/script.lua | 4 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 4 | ||||
-rw-r--r-- | locale/zh-tw/script.lua | 4 | ||||
-rw-r--r-- | script/library.lua | 63 |
5 files changed, 78 insertions, 1 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index ba083a11..331f88d2 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY = 'Do you need to configure your work environment as `{}`?' WINDOW_SEARCHING_IN_FILES = 'Searching in files...' +WINDOW_CONFIG_LUA_DEPRECATED = +'`config.lua` is deprecated, please use `config.json` instead.' +WINDOW_CONVERT_CONFIG_LUA = +'Convert to `config.json`' CONFIG_LOAD_FAILED = 'Unable to read the settings file: {}' diff --git a/locale/pt-br/script.lua b/locale/pt-br/script.lua index e733a00f..c5c6b493 100644 --- a/locale/pt-br/script.lua +++ b/locale/pt-br/script.lua @@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY = 'Você precisa configurar seu ambiente de trabalho como `{}`?' WINDOW_SEARCHING_IN_FILES = -- TODO: need translate! 'Procurando nos arquivos...' +WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate! +'`config.lua` is deprecated, please use `config.json` instead.' +WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate! +'Convert to `config.json`' CONFIG_LOAD_FAILED = 'Não é possível ler o arquivo de configurações: {}' diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index cc77ad79..6d86297f 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY = '是否需要将你的工作环境配置为 `{}` ?' WINDOW_SEARCHING_IN_FILES = '正在文件中搜索...' +WINDOW_CONFIG_LUA_DEPRECATED = +'`config.lua` 已废弃,请改用 `config.json` 。' +WINDOW_CONVERT_CONFIG_LUA = +'转换为 `config.json`' CONFIG_LOAD_FAILED = '无法读取设置文件:{}' diff --git a/locale/zh-tw/script.lua b/locale/zh-tw/script.lua index 27686051..8652e48e 100644 --- a/locale/zh-tw/script.lua +++ b/locale/zh-tw/script.lua @@ -547,6 +547,10 @@ WINDOW_ASK_APPLY_LIBRARY = '是否需要將你的工作環境配置為 `{}` ?' WINDOW_SEARCHING_IN_FILES = '正在檔案中搜尋...' +WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate! +'`config.lua` is deprecated, please use `config.json` instead.' +WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate! +'Convert to `config.json`' CONFIG_LOAD_FAILED = '無法讀取設定檔案:{}' diff --git a/script/library.lua b/script/library.lua index 0104189d..b160f603 100644 --- a/script/library.lua +++ b/script/library.lua @@ -15,6 +15,7 @@ local ws = require 'workspace.workspace' local scope = require 'workspace.scope' local inspect = require 'inspect' local jsonc = require 'jsonc' +local json = require 'json' local m = {} @@ -275,7 +276,8 @@ local function initBuiltIn(uri) end ---@param libraryDir fs.path -local function loadSingle3rdConfig(libraryDir) +---@return table? +local function loadSingle3rdConfigFromJson(libraryDir) local path = libraryDir / 'config.json' local configText = fsu.loadFile(path) if not configText then @@ -289,6 +291,65 @@ local function loadSingle3rdConfig(libraryDir) return nil end + if type(cfg) ~= 'table' then + log.error('config.json must be an object:', libraryDir:string()) + return nil + end + + return cfg +end + +---@param libraryDir fs.path +---@return table? +local function loadSingle3rdConfigFromLua(libraryDir) + local path = libraryDir / 'config.lua' + local configText = fsu.loadFile(path) + if not configText then + return nil + end + + local env = setmetatable({}, { __index = _G }) + local f, err = load(configText, '@' .. libraryDir:string(), 't', env) + if not f then + log.error('Load config.lua failed at:', libraryDir:string(), err) + return nil + end + + local suc = xpcall(f, function (err) + log.error('Load config.lua failed at:', libraryDir:string(), err) + end) + + if not suc then + return nil + end + + local cfg = {} + for k, v in pairs(env) do + cfg[k] = v + end + + return cfg +end + +---@param libraryDir fs.path +local function loadSingle3rdConfig(libraryDir) + local cfg = loadSingle3rdConfigFromJson(libraryDir) + if not cfg then + cfg = loadSingle3rdConfigFromLua(libraryDir) + if not cfg then + return + end + local jsonbuf = json.beautify(cfg) + client.requestMessage('Info', lang.script.WINDOW_CONFIG_LUA_DEPRECATED, { + lang.script.WINDOW_CONVERT_CONFIG_LUA, + }, function (action, index) + if index == 1 and jsonbuf then + fsu.saveFile(libraryDir / 'config.json', jsonbuf) + fsu.fileRemove(libraryDir / 'config.lua') + end + end) + end + cfg.path = libraryDir:filename():string() cfg.name = cfg.name or cfg.path |