From 418d2ab34891b130cc317df32f65f978640febcf Mon Sep 17 00:00:00 2001 From: bzp2010 Date: Tue, 3 Jun 2025 02:17:56 +0800 Subject: feat: allow loading json manifest --- src/luarocks/core/manif.lua | 11 +++++++++-- src/luarocks/core/manif.tl | 9 ++++++++- src/luarocks/core/persist.lua | 30 ++++++++++++++++++++++++++++++ src/luarocks/core/persist.tl | 30 ++++++++++++++++++++++++++++++ src/luarocks/manif.lua | 6 +++++- src/luarocks/manif.tl | 4 ++++ src/luarocks/vendor/dkjson.d.tl | 2 +- 7 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/luarocks/core/manif.lua b/src/luarocks/core/manif.lua index 4c5e0441..c5ee158e 100644 --- a/src/luarocks/core/manif.lua +++ b/src/luarocks/core/manif.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local table = _tl_compat and _tl_compat.table or table; local type = type +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type local manif = {} @@ -52,7 +52,14 @@ end function manif.manifest_loader(file, repo_url, lua_version) - local manifest, err, errcode = persist.load_into_table(file) + local manifest, err, errcode + + if file:match(".*%.json$") then + manifest, err, errcode = persist.load_json_into_table(file) + else + manifest, err, errcode = persist.load_into_table(file) + end + if not manifest and type(err) == "string" then return nil, "Failed loading manifest for " .. repo_url .. ": " .. err, errcode end diff --git a/src/luarocks/core/manif.tl b/src/luarocks/core/manif.tl index 1f3b3659..0a96d47e 100644 --- a/src/luarocks/core/manif.tl +++ b/src/luarocks/core/manif.tl @@ -52,7 +52,14 @@ end -- @return table or (nil, string, string): the manifest or nil, -- error message and error code ("open", "load", "run"). function manif.manifest_loader(file: string, repo_url: string, lua_version: string): Manifest, string | {any: any}, string - local manifest, err, errcode = persist.load_into_table(file) + local manifest, err, errcode: {string: any}, {string: boolean} | string, string + + if file:match(".*%.json$") then + manifest, err, errcode = persist.load_json_into_table(file) + else + manifest, err, errcode = persist.load_into_table(file) + end + if not manifest and err is string then return nil, "Failed loading manifest for "..repo_url..": " .. err, errcode end diff --git a/src/luarocks/core/persist.lua b/src/luarocks/core/persist.lua index 258a42c0..c9ccb4a0 100644 --- a/src/luarocks/core/persist.lua +++ b/src/luarocks/core/persist.lua @@ -2,6 +2,8 @@ local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 th local persist = {} +local json = require("luarocks.vendor.dkjson") + @@ -67,4 +69,32 @@ function persist.load_into_table(filename, tbl) return result, globals end + + + + + + + + + + +function persist.load_json_into_table(filename) + local fd, open_err = io.open(filename) + if not fd then + return nil, open_err, "open" + end + local str, read_err = fd:read("*a") + fd:close() + if not str then + return nil, read_err, "open" + end + local manifest, _, err = json.decode(str) + if not manifest then + return nil, "Failed decode manifest: " .. err, "load" + end + + return manifest, {} +end + return persist diff --git a/src/luarocks/core/persist.tl b/src/luarocks/core/persist.tl index 4694afcb..268d2bb6 100644 --- a/src/luarocks/core/persist.tl +++ b/src/luarocks/core/persist.tl @@ -2,6 +2,8 @@ local record persist end +local json = require("luarocks.vendor.dkjson") + -------------------------------------------------------------------------------- --- Load and run a Lua file in an environment. @@ -67,5 +69,33 @@ function persist.load_into_table(filename: string, tbl?: {string:any}) : {string return result, globals end +--- Load a JSON file containing assignments, storing them in a table. +-- The global environment is not propagated to the loaded file. +-- @param filename string: the name of the file. +-- @param tbl table or nil: if given, this table is used to store +-- loaded values. +-- @return (table, table) or (nil, string, string): a table with the file's assignments +-- as fields and set of undefined globals accessed in file, +-- or nil, an error message and an error code ("open"; couldn't open the file, +-- "load"; compile-time error, or "run"; run-time error) +-- in case of errors. +function persist.load_json_into_table(filename: string) : {string: any}, {string: boolean} | string, string + local fd, open_err = io.open(filename) + if not fd then + return nil, open_err, "open" + end + local str, read_err = fd:read("*a") + fd:close() + if not str then + return nil, read_err, "open" + end + local manifest, _, err = json.decode(str) + if not manifest then + return nil, "Failed decode manifest: " .. err, "load" + end + + return manifest, {} +end + return persist diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua index 809b823d..b608d363 100644 --- a/src/luarocks/manif.lua +++ b/src/luarocks/manif.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local type = type +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type @@ -103,6 +103,10 @@ function manif.load_manifest(repo_url, lua_version, versioned_only) not versioned_only and "manifest" or nil, } + if util.get_luajit_version() then + table.insert(filenames, 1, "manifest-" .. lua_version .. ".json") + end + local protocol, repodir = dir.split_url(repo_url) local pathname, from_cache if protocol == "file" then diff --git a/src/luarocks/manif.tl b/src/luarocks/manif.tl index 6a62a73f..da9b7d4c 100644 --- a/src/luarocks/manif.tl +++ b/src/luarocks/manif.tl @@ -103,6 +103,10 @@ function manif.load_manifest(repo_url: string, lua_version?: string, versioned_o not versioned_only and "manifest" or nil, } + if util.get_luajit_version() then + table.insert(filenames, 1, "manifest-" .. lua_version .. ".json") + end + local protocol, repodir = dir.split_url(repo_url) local pathname, from_cache: string, boolean if protocol == "file" then diff --git a/src/luarocks/vendor/dkjson.d.tl b/src/luarocks/vendor/dkjson.d.tl index a7c76389..4245e8aa 100644 --- a/src/luarocks/vendor/dkjson.d.tl +++ b/src/luarocks/vendor/dkjson.d.tl @@ -15,7 +15,7 @@ local record dkjson end encode: function({string:any}, ?JsonState): string - decode: function(string, ?number, ?any, ?table): {string:any} + decode: function(string, ?number, ?any, ?table): {string:any}, integer, string null: table -- cgit v1.2.3-55-g6feb