diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:57 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | 66459e17a7f356827d1ee2592e847d0e939acf0f (patch) | |
tree | 76b705ac5d7790a8cb7f4abed65b10a88169ccf7 /src | |
parent | 091c6a54952f1d67400a5c3b4da5ee2f469e0484 (diff) | |
download | luarocks-66459e17a7f356827d1ee2592e847d0e939acf0f.tar.gz luarocks-66459e17a7f356827d1ee2592e847d0e939acf0f.tar.bz2 luarocks-66459e17a7f356827d1ee2592e847d0e939acf0f.zip |
Teal: convert luarocks.search
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/search.tl (renamed from src/luarocks/search.lua) | 103 |
1 files changed, 48 insertions, 55 deletions
diff --git a/src/luarocks/search.lua b/src/luarocks/search.tl index 180f8f45..31c42ee9 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.tl | |||
@@ -9,14 +9,18 @@ local util = require("luarocks.util") | |||
9 | local queries = require("luarocks.queries") | 9 | local queries = require("luarocks.queries") |
10 | local results = require("luarocks.results") | 10 | local results = require("luarocks.results") |
11 | 11 | ||
12 | local type Result = require("luarocks.core.types.result").Result | ||
13 | |||
14 | local type Query = require("luarocks.core.types.query").Query | ||
15 | |||
16 | local type Tree = require("luarocks.core.types.tree").Tree | ||
17 | |||
12 | --- Store a search result (a rock or rockspec) in the result tree. | 18 | --- Store a search result (a rock or rockspec) in the result tree. |
13 | -- @param result_tree table: The result tree, where keys are package names and | 19 | -- @param result_tree table: The result tree, where keys are package names and |
14 | -- values are tables matching version strings to arrays of | 20 | -- values are tables matching version strings to arrays of |
15 | -- tables with fields "arch" and "repo". | 21 | -- tables with fields "arch" and "repo". |
16 | -- @param result table: A result. | 22 | -- @param result table: A result. |
17 | function search.store_result(result_tree, result) | 23 | function search.store_result(result_tree: {string: {string: {Result}}}, result: Result) |
18 | assert(type(result_tree) == "table") | ||
19 | assert(result:type() == "result") | ||
20 | 24 | ||
21 | local name = result.name | 25 | local name = result.name |
22 | local version = result.version | 26 | local version = result.version |
@@ -27,7 +31,7 @@ function search.store_result(result_tree, result) | |||
27 | arch = result.arch, | 31 | arch = result.arch, |
28 | repo = result.repo, | 32 | repo = result.repo, |
29 | namespace = result.namespace, | 33 | namespace = result.namespace, |
30 | }) | 34 | } as Result) |
31 | end | 35 | end |
32 | 36 | ||
33 | --- Store a match in a result tree if version matches query. | 37 | --- Store a match in a result tree if version matches query. |
@@ -39,9 +43,7 @@ end | |||
39 | -- tables with fields "arch" and "repo". | 43 | -- tables with fields "arch" and "repo". |
40 | -- @param result table: a result object. | 44 | -- @param result table: a result object. |
41 | -- @param query table: a query object. | 45 | -- @param query table: a query object. |
42 | local function store_if_match(result_tree, result, query) | 46 | local function store_if_match(result_tree: {string: {string: {Result}}}, result: Result, query: Query) |
43 | assert(result:type() == "result") | ||
44 | assert(query:type() == "query") | ||
45 | 47 | ||
46 | if result:satisfies(query) then | 48 | if result:satisfies(query) then |
47 | search.store_result(result_tree, result) | 49 | search.store_result(result_tree, result) |
@@ -57,10 +59,7 @@ end | |||
57 | -- values are tables matching version strings to arrays of | 59 | -- values are tables matching version strings to arrays of |
58 | -- tables with fields "arch" and "repo". | 60 | -- tables with fields "arch" and "repo". |
59 | -- If a table was given in the "result_tree" parameter, that is the result value. | 61 | -- If a table was given in the "result_tree" parameter, that is the result value. |
60 | function search.disk_search(repo, query, result_tree) | 62 | function search.disk_search(repo: string, query: Query, result_tree?: {string: {string: {Result}}}): {string: {string: {Result}}} |
61 | assert(type(repo) == "string") | ||
62 | assert(query:type() == "query") | ||
63 | assert(type(result_tree) == "table" or not result_tree) | ||
64 | 63 | ||
65 | local fs = require("luarocks.fs") | 64 | local fs = require("luarocks.fs") |
66 | 65 | ||
@@ -98,10 +97,7 @@ end | |||
98 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. | 97 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. |
99 | -- @param is_local boolean | 98 | -- @param is_local boolean |
100 | -- @return true or, in case of errors, nil, an error message and an optional error code. | 99 | -- @return true or, in case of errors, nil, an error message and an optional error code. |
101 | local function manifest_search(result_tree, repo, query, lua_version, is_local) | 100 | local function manifest_search(result_tree: {string: {string: {Result}}}, repo: string, query: Query, lua_version: string, is_local: boolean): boolean, string, string |
102 | assert(type(result_tree) == "table") | ||
103 | assert(type(repo) == "string") | ||
104 | assert(query:type() == "query") | ||
105 | 101 | ||
106 | -- FIXME do not add this in local repos | 102 | -- FIXME do not add this in local repos |
107 | if (not is_local) and query.namespace then | 103 | if (not is_local) and query.namespace then |
@@ -124,11 +120,11 @@ local function manifest_search(result_tree, repo, query, lua_version, is_local) | |||
124 | return true | 120 | return true |
125 | end | 121 | end |
126 | 122 | ||
127 | local function remote_manifest_search(result_tree, repo, query, lua_version) | 123 | local function remote_manifest_search(result_tree: {string: {string: {Result}}}, repo: string, query: Query, lua_version?: string): boolean, string, string |
128 | return manifest_search(result_tree, repo, query, lua_version, false) | 124 | return manifest_search(result_tree, repo, query, lua_version, false) |
129 | end | 125 | end |
130 | 126 | ||
131 | function search.local_manifest_search(result_tree, repo, query, lua_version) | 127 | function search.local_manifest_search(result_tree: {string: {string: {Result}}}, repo: string, query: Query, lua_version?: string): boolean, string, string |
132 | return manifest_search(result_tree, repo, query, lua_version, true) | 128 | return manifest_search(result_tree, repo, query, lua_version, true) |
133 | end | 129 | end |
134 | 130 | ||
@@ -138,13 +134,15 @@ end | |||
138 | -- @return table: A table where keys are package names | 134 | -- @return table: A table where keys are package names |
139 | -- and values are tables matching version strings to arrays of | 135 | -- and values are tables matching version strings to arrays of |
140 | -- tables with fields "arch" and "repo". | 136 | -- tables with fields "arch" and "repo". |
141 | function search.search_repos(query, lua_version) | 137 | function search.search_repos(query: Query, lua_version?: string): {string : {string : {Result}}} |
142 | assert(query:type() == "query") | ||
143 | 138 | ||
144 | local result_tree = {} | 139 | local result_tree = {} |
145 | for _, repo in ipairs(cfg.rocks_servers) do | 140 | local repo = {} |
146 | if type(repo) == "string" then | 141 | for _, repostr in ipairs(cfg.rocks_servers) do |
147 | repo = { repo } | 142 | if repostr is string then |
143 | repo = { repostr } | ||
144 | else | ||
145 | repo = repostr | ||
148 | end | 146 | end |
149 | for _, mirror in ipairs(repo) do | 147 | for _, mirror in ipairs(repo) do |
150 | if not cfg.disabled_servers[mirror] then | 148 | if not cfg.disabled_servers[mirror] then |
@@ -182,9 +180,8 @@ end | |||
182 | -- in search result trees. | 180 | -- in search result trees. |
183 | -- @return string or nil: the URL for the latest version if one could | 181 | -- @return string or nil: the URL for the latest version if one could |
184 | -- be picked, or nil. | 182 | -- be picked, or nil. |
185 | local function pick_latest_version(name, versions) | 183 | local function pick_latest_version(name: string, versions: {string: {Result}}): string |
186 | assert(type(name) == "string" and not name:match("/")) | 184 | assert(not name:match("/")) |
187 | assert(type(versions) == "table") | ||
188 | 185 | ||
189 | local vtables = {} | 186 | local vtables = {} |
190 | for v, _ in pairs(versions) do | 187 | for v, _ in pairs(versions) do |
@@ -209,8 +206,7 @@ end | |||
209 | -- Find out which other Lua versions provide rock versions matching a query, | 206 | -- Find out which other Lua versions provide rock versions matching a query, |
210 | -- @param query table: a query object. | 207 | -- @param query table: a query object. |
211 | -- @return table: array of Lua versions supported, in "5.x" format. | 208 | -- @return table: array of Lua versions supported, in "5.x" format. |
212 | local function supported_lua_versions(query) | 209 | local function supported_lua_versions(query: Query): {string} |
213 | assert(query:type() == "query") | ||
214 | local result_tree = {} | 210 | local result_tree = {} |
215 | 211 | ||
216 | for lua_version in util.lua_versions() do | 212 | for lua_version in util.lua_versions() do |
@@ -230,12 +226,11 @@ end | |||
230 | -- @return string or (nil, string, string): URL for latest matching version | 226 | -- @return string or (nil, string, string): URL for latest matching version |
231 | -- of the rock if it was found, or nil followed by an error message | 227 | -- of the rock if it was found, or nil followed by an error message |
232 | -- and an error code. | 228 | -- and an error code. |
233 | function search.find_suitable_rock(query) | 229 | function search.find_suitable_rock(query: Query): string, string, string |
234 | assert(query:type() == "query") | ||
235 | 230 | ||
236 | local rocks_provided = util.get_rocks_provided() | 231 | local rocks_provided = util.get_rocks_provided() |
237 | 232 | ||
238 | if rocks_provided[query.name] ~= nil then | 233 | if rocks_provided[query.name] then |
239 | -- Do not install versions listed in rocks_provided. | 234 | -- Do not install versions listed in rocks_provided. |
240 | return nil, "Rock "..query.name.." "..rocks_provided[query.name].. | 235 | return nil, "Rock "..query.name.." "..rocks_provided[query.name].. |
241 | " is already provided by VM or via 'rocks_provided' in the config file.", "provided" | 236 | " is already provided by VM or via 'rocks_provided' in the config file.", "provided" |
@@ -253,23 +248,14 @@ function search.find_suitable_rock(query) | |||
253 | end | 248 | end |
254 | end | 249 | end |
255 | 250 | ||
256 | function search.find_src_or_rockspec(name, namespace, version, check_lua_versions) | 251 | function search.find_rock_checking_lua_versions(query: Query, check_lua_versions: boolean): string, string |
257 | local query = queries.new(name, namespace, version, false, "src|rockspec") | ||
258 | local url, err = search.find_rock_checking_lua_versions(query, check_lua_versions) | ||
259 | if not url then | ||
260 | return nil, "Could not find a result named "..tostring(query)..": "..err | ||
261 | end | ||
262 | return url | ||
263 | end | ||
264 | |||
265 | function search.find_rock_checking_lua_versions(query, check_lua_versions) | ||
266 | local url, err, errcode = search.find_suitable_rock(query) | 252 | local url, err, errcode = search.find_suitable_rock(query) |
267 | if url then | 253 | if url then |
268 | return url | 254 | return url |
269 | end | 255 | end |
270 | 256 | ||
271 | if errcode == "notfound" then | 257 | if errcode == "notfound" then |
272 | local add | 258 | local add: string |
273 | if check_lua_versions then | 259 | if check_lua_versions then |
274 | util.printout(query.name .. " not found for Lua " .. cfg.lua_version .. ".") | 260 | util.printout(query.name .. " not found for Lua " .. cfg.lua_version .. ".") |
275 | util.printout("Checking if available for other Lua versions...") | 261 | util.printout("Checking if available for other Lua versions...") |
@@ -289,7 +275,8 @@ function search.find_rock_checking_lua_versions(query, check_lua_versions) | |||
289 | if #query.constraints == 0 then | 275 | if #query.constraints == 0 then |
290 | add = query.name.." supports "..versions_message | 276 | add = query.name.." supports "..versions_message |
291 | elseif #query.constraints == 1 and query.constraints[1].op == "==" then | 277 | elseif #query.constraints == 1 and query.constraints[1].op == "==" then |
292 | add = query.name.." "..query.constraints[1].version.string.." supports "..versions_message | 278 | local queryversion = tostring(query.constraints[1].version) |
279 | add = query.name.." "..queryversion.." supports "..versions_message | ||
293 | else | 280 | else |
294 | add = "Matching "..query.name.." versions support "..versions_message | 281 | add = "Matching "..query.name.." versions support "..versions_message |
295 | end | 282 | end |
@@ -305,27 +292,34 @@ function search.find_rock_checking_lua_versions(query, check_lua_versions) | |||
305 | return nil, err | 292 | return nil, err |
306 | end | 293 | end |
307 | 294 | ||
295 | function search.find_src_or_rockspec(name: string, namespace: string, version: string, check_lua_versions: boolean): string, string | ||
296 | local query = queries.new(name, namespace, version, false, "src|rockspec") | ||
297 | local url, err = search.find_rock_checking_lua_versions(query, check_lua_versions) | ||
298 | if not url then | ||
299 | return nil, "Could not find a result named "..tostring(query)..": "..err | ||
300 | end | ||
301 | return url | ||
302 | end | ||
303 | |||
308 | --- Print a list of rocks/rockspecs on standard output. | 304 | --- Print a list of rocks/rockspecs on standard output. |
309 | -- @param result_tree table: A result tree. | 305 | -- @param result_tree table: A result tree. |
310 | -- @param porcelain boolean or nil: A flag to force machine-friendly output. | 306 | -- @param porcelain boolean or nil: A flag to force machine-friendly output. |
311 | function search.print_result_tree(result_tree, porcelain) | 307 | function search.print_result_tree(result_tree: {string: {string: {Result}}}, porcelain?: boolean) |
312 | assert(type(result_tree) == "table") | ||
313 | assert(type(porcelain) == "boolean" or not porcelain) | ||
314 | 308 | ||
315 | if porcelain then | 309 | if porcelain then |
316 | for package, versions in util.sortedpairs(result_tree) do | 310 | for packagestr, versions in util.sortedpairs(result_tree) do |
317 | for version, repos in util.sortedpairs(versions, vers.compare_versions) do | 311 | for version, repos in util.sortedpairs(versions, vers.compare_versions) do |
318 | for _, repo in ipairs(repos) do | 312 | for _, repo in ipairs(repos) do |
319 | local nrepo = dir.normalize(repo.repo) | 313 | local nrepo = dir.normalize(repo.repo) |
320 | util.printout(package, version, repo.arch, nrepo, repo.namespace) | 314 | util.printout(packagestr, version, repo.arch, nrepo, repo.namespace) |
321 | end | 315 | end |
322 | end | 316 | end |
323 | end | 317 | end |
324 | return | 318 | return |
325 | end | 319 | end |
326 | 320 | ||
327 | for package, versions in util.sortedpairs(result_tree) do | 321 | for packagestr, versions in util.sortedpairs(result_tree) do |
328 | local namespaces = {} | 322 | local namespaces: {string: {string}} = {} |
329 | for version, repos in util.sortedpairs(versions, vers.compare_versions) do | 323 | for version, repos in util.sortedpairs(versions, vers.compare_versions) do |
330 | for _, repo in ipairs(repos) do | 324 | for _, repo in ipairs(repos) do |
331 | local key = repo.namespace or "" | 325 | local key = repo.namespace or "" |
@@ -337,7 +331,7 @@ function search.print_result_tree(result_tree, porcelain) | |||
337 | end | 331 | end |
338 | end | 332 | end |
339 | for key, list in util.sortedpairs(namespaces) do | 333 | for key, list in util.sortedpairs(namespaces) do |
340 | util.printout(key == "" and package or key .. "/" .. package) | 334 | util.printout(key == "" and packagestr or key .. "/" .. packagestr) |
341 | for _, line in ipairs(list) do | 335 | for _, line in ipairs(list) do |
342 | util.printout(line) | 336 | util.printout(line) |
343 | end | 337 | end |
@@ -346,8 +340,7 @@ function search.print_result_tree(result_tree, porcelain) | |||
346 | end | 340 | end |
347 | end | 341 | end |
348 | 342 | ||
349 | function search.pick_installed_rock(query, given_tree) | 343 | function search.pick_installed_rock(query: Query, given_tree: string | Tree): string, string, string | Tree, string |
350 | assert(query:type() == "query") | ||
351 | 344 | ||
352 | local result_tree = {} | 345 | local result_tree = {} |
353 | local tree_map = {} | 346 | local tree_map = {} |
@@ -364,7 +357,7 @@ function search.pick_installed_rock(query, given_tree) | |||
364 | return nil, "cannot find package "..tostring(query).."\nUse 'list' to find installed rocks." | 357 | return nil, "cannot find package "..tostring(query).."\nUse 'list' to find installed rocks." |
365 | end | 358 | end |
366 | 359 | ||
367 | if not result_tree[query.name] and next(result_tree, next(result_tree)) then | 360 | if not result_tree[query.name] and next(result_tree, next(result_tree) as string) then |
368 | local out = { "multiple installed packages match the name '"..tostring(query).."':\n\n" } | 361 | local out = { "multiple installed packages match the name '"..tostring(query).."':\n\n" } |
369 | for name, _ in util.sortedpairs(result_tree) do | 362 | for name, _ in util.sortedpairs(result_tree) do |
370 | table.insert(out, " " .. name .. "\n") | 363 | table.insert(out, " " .. name .. "\n") |
@@ -373,9 +366,9 @@ function search.pick_installed_rock(query, given_tree) | |||
373 | return nil, table.concat(out) | 366 | return nil, table.concat(out) |
374 | end | 367 | end |
375 | 368 | ||
376 | local repo_url | 369 | local repo_url: string |
377 | 370 | ||
378 | local name, versions | 371 | local name, versions: string, {string : {Result}} |
379 | if result_tree[query.name] then | 372 | if result_tree[query.name] then |
380 | name, versions = query.name, result_tree[query.name] | 373 | name, versions = query.name, result_tree[query.name] |
381 | else | 374 | else |