aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbzp2010 <bzp2010@apache.org>2025-06-03 02:17:56 +0800
committerHisham Muhammad <hisham@gobolinux.org>2025-06-03 12:45:57 -0300
commit418d2ab34891b130cc317df32f65f978640febcf (patch)
treeba0b354c70f5477e2a0fd7cd381d8d82c47f8346
parent5dd175b080e78c4587408696d9ec3cb77b88d0bb (diff)
downloadluarocks-418d2ab34891b130cc317df32f65f978640febcf.tar.gz
luarocks-418d2ab34891b130cc317df32f65f978640febcf.tar.bz2
luarocks-418d2ab34891b130cc317df32f65f978640febcf.zip
feat: allow loading json manifest
-rw-r--r--src/luarocks/core/manif.lua11
-rw-r--r--src/luarocks/core/manif.tl9
-rw-r--r--src/luarocks/core/persist.lua30
-rw-r--r--src/luarocks/core/persist.tl30
-rw-r--r--src/luarocks/manif.lua6
-rw-r--r--src/luarocks/manif.tl4
-rw-r--r--src/luarocks/vendor/dkjson.d.tl2
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 @@
1local _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 1local _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
2 2
3local manif = {} 3local manif = {}
4 4
@@ -52,7 +52,14 @@ end
52 52
53 53
54function manif.manifest_loader(file, repo_url, lua_version) 54function manif.manifest_loader(file, repo_url, lua_version)
55 local manifest, err, errcode = persist.load_into_table(file) 55 local manifest, err, errcode
56
57 if file:match(".*%.json$") then
58 manifest, err, errcode = persist.load_json_into_table(file)
59 else
60 manifest, err, errcode = persist.load_into_table(file)
61 end
62
56 if not manifest and type(err) == "string" then 63 if not manifest and type(err) == "string" then
57 return nil, "Failed loading manifest for " .. repo_url .. ": " .. err, errcode 64 return nil, "Failed loading manifest for " .. repo_url .. ": " .. err, errcode
58 end 65 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
52-- @return table or (nil, string, string): the manifest or nil, 52-- @return table or (nil, string, string): the manifest or nil,
53-- error message and error code ("open", "load", "run"). 53-- error message and error code ("open", "load", "run").
54function manif.manifest_loader(file: string, repo_url: string, lua_version: string): Manifest, string | {any: any}, string 54function manif.manifest_loader(file: string, repo_url: string, lua_version: string): Manifest, string | {any: any}, string
55 local manifest, err, errcode = persist.load_into_table(file) 55 local manifest, err, errcode: {string: any}, {string: boolean} | string, string
56
57 if file:match(".*%.json$") then
58 manifest, err, errcode = persist.load_json_into_table(file)
59 else
60 manifest, err, errcode = persist.load_into_table(file)
61 end
62
56 if not manifest and err is string then 63 if not manifest and err is string then
57 return nil, "Failed loading manifest for "..repo_url..": " .. err, errcode 64 return nil, "Failed loading manifest for "..repo_url..": " .. err, errcode
58 end 65 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
2local persist = {} 2local persist = {}
3 3
4 4
5local json = require("luarocks.vendor.dkjson")
6
5 7
6 8
7 9
@@ -67,4 +69,32 @@ function persist.load_into_table(filename, tbl)
67 return result, globals 69 return result, globals
68end 70end
69 71
72
73
74
75
76
77
78
79
80
81
82function persist.load_json_into_table(filename)
83 local fd, open_err = io.open(filename)
84 if not fd then
85 return nil, open_err, "open"
86 end
87 local str, read_err = fd:read("*a")
88 fd:close()
89 if not str then
90 return nil, read_err, "open"
91 end
92 local manifest, _, err = json.decode(str)
93 if not manifest then
94 return nil, "Failed decode manifest: " .. err, "load"
95 end
96
97 return manifest, {}
98end
99
70return persist 100return 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 @@
2local record persist 2local record persist
3end 3end
4 4
5local json = require("luarocks.vendor.dkjson")
6
5-------------------------------------------------------------------------------- 7--------------------------------------------------------------------------------
6 8
7--- Load and run a Lua file in an environment. 9--- Load and run a Lua file in an environment.
@@ -67,5 +69,33 @@ function persist.load_into_table(filename: string, tbl?: {string:any}) : {string
67 return result, globals 69 return result, globals
68end 70end
69 71
72--- Load a JSON file containing assignments, storing them in a table.
73-- The global environment is not propagated to the loaded file.
74-- @param filename string: the name of the file.
75-- @param tbl table or nil: if given, this table is used to store
76-- loaded values.
77-- @return (table, table) or (nil, string, string): a table with the file's assignments
78-- as fields and set of undefined globals accessed in file,
79-- or nil, an error message and an error code ("open"; couldn't open the file,
80-- "load"; compile-time error, or "run"; run-time error)
81-- in case of errors.
82function persist.load_json_into_table(filename: string) : {string: any}, {string: boolean} | string, string
83 local fd, open_err = io.open(filename)
84 if not fd then
85 return nil, open_err, "open"
86 end
87 local str, read_err = fd:read("*a")
88 fd:close()
89 if not str then
90 return nil, read_err, "open"
91 end
92 local manifest, _, err = json.decode(str)
93 if not manifest then
94 return nil, "Failed decode manifest: " .. err, "load"
95 end
96
97 return manifest, {}
98end
99
70return persist 100return persist
71 101
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 @@
1local _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 1local _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
2 2
3 3
4 4
@@ -103,6 +103,10 @@ function manif.load_manifest(repo_url, lua_version, versioned_only)
103 not versioned_only and "manifest" or nil, 103 not versioned_only and "manifest" or nil,
104 } 104 }
105 105
106 if util.get_luajit_version() then
107 table.insert(filenames, 1, "manifest-" .. lua_version .. ".json")
108 end
109
106 local protocol, repodir = dir.split_url(repo_url) 110 local protocol, repodir = dir.split_url(repo_url)
107 local pathname, from_cache 111 local pathname, from_cache
108 if protocol == "file" then 112 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
103 not versioned_only and "manifest" or nil, 103 not versioned_only and "manifest" or nil,
104 } 104 }
105 105
106 if util.get_luajit_version() then
107 table.insert(filenames, 1, "manifest-" .. lua_version .. ".json")
108 end
109
106 local protocol, repodir = dir.split_url(repo_url) 110 local protocol, repodir = dir.split_url(repo_url)
107 local pathname, from_cache: string, boolean 111 local pathname, from_cache: string, boolean
108 if protocol == "file" then 112 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
15 end 15 end
16 encode: function({string:any}, ?JsonState): string 16 encode: function({string:any}, ?JsonState): string
17 17
18 decode: function(string, ?number, ?any, ?table): {string:any} 18 decode: function(string, ?number, ?any, ?table): {string:any}, integer, string
19 19
20 null: table 20 null: table
21 21