aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-07-15 21:28:26 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commitd6a17f7add82faf97d6a00c5b0f523f5b4401ba6 (patch)
treec35cdee43503c8dee3b67c33c44ead2e5cbd956a
parent949c0e66a12d4f9d6ad9df560647b41329d9a5f7 (diff)
downloadluarocks-d6a17f7add82faf97d6a00c5b0f523f5b4401ba6.tar.gz
luarocks-d6a17f7add82faf97d6a00c5b0f523f5b4401ba6.tar.bz2
luarocks-d6a17f7add82faf97d6a00c5b0f523f5b4401ba6.zip
finished util with some questions
-rw-r--r--src/luarocks/core/cfg.d.tl10
-rw-r--r--src/luarocks/fs.d.tl3
-rw-r--r--src/luarocks/util.tl140
3 files changed, 93 insertions, 60 deletions
diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl
index 68d110c3..5c34f29e 100644
--- a/src/luarocks/core/cfg.d.tl
+++ b/src/luarocks/core/cfg.d.tl
@@ -21,6 +21,16 @@ local record cfg
21 lua_dir: string 21 lua_dir: string
22 lib_dir: string 22 lib_dir: string
23 end 23 end
24 -- util
25 record cache
26 luajit_version_checked: boolean
27 luajit_version: string
28 rocks_provided: {Rockspec} --! import? or move?
29 end
30 record variables
31 LUA: string
32 end
33 rocks_provided: {Rockspec}
24end 34end
25 35
26return cfg \ No newline at end of file 36return cfg \ No newline at end of file
diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl
index 6a8d7520..0334329d 100644
--- a/src/luarocks/fs.d.tl
+++ b/src/luarocks/fs.d.tl
@@ -10,6 +10,9 @@ local record fs
10 is_dir: function(dir: string): boolean 10 is_dir: function(dir: string): boolean
11 dir: function(dir: string): function(): string --? right iterator 11 dir: function(dir: string): function(): string --? right iterator
12 is_file: function(file: string): boolean 12 is_file: function(file: string): boolean
13 current_dir: function(): string
14 list_dir: function(string): {string}
15 delete: function(string)
13end 16end
14 17
15return fs 18return fs
diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl
index a9fc8394..77d6f5c4 100644
--- a/src/luarocks/util.tl
+++ b/src/luarocks/util.tl
@@ -51,6 +51,7 @@ local record util
51 description: Description 51 description: Description
52 test_dependencies: {string} 52 test_dependencies: {string}
53 test: Test 53 test: Test
54 format_is_at_least: function(Rockspec, string): boolean
54 end 55 end
55 56
56 record Parser --? 57 record Parser --?
@@ -62,16 +63,16 @@ local record util
62 end 63 end
63end 64end
64 65
65util.cleanup_path = core.cleanup_path 66-- util.cleanup_path = core.cleanup_path --tlcheck acting funny
66util.split_string = core.split_string 67-- util.split_string = core.split_string
67util.sortedpairs = core.sortedpairs 68-- util.sortedpairs = core.sortedpairs
68util.deep_merge = core.deep_merge 69-- util.deep_merge = core.deep_merge
69util.deep_merge_under = core.deep_merge_under 70-- util.deep_merge_under = core.deep_merge_under
70util.popen_read = core.popen_read 71-- util.popen_read = core.popen_read
71util.show_table = core.show_table 72-- util.show_table = core.show_table
72util.printerr = core.printerr 73-- util.printerr = core.printerr
73util.warning = core.warning 74-- util.warning = core.warning
74util.keys = core.keys 75-- util.keys = core.keys
75 76
76local type Fn = util.Fn 77local type Fn = util.Fn
77local type Rockspec = util.Rockspec 78local type Rockspec = util.Rockspec
@@ -349,7 +350,7 @@ end
349 350
350--- Get default rockspec name for commands that take optional rockspec name. 351--- Get default rockspec name for commands that take optional rockspec name.
351-- @return string or (nil, string): path to the rockspec or nil and error message. 352-- @return string or (nil, string): path to the rockspec or nil and error message.
352function util.get_default_rockspec() 353function util.get_default_rockspec(): string, string
353 local versions, paths, unnamed_paths = {}, {}, {} 354 local versions, paths, unnamed_paths = {}, {}, {}
354 -- Look for rockspecs in some common locations. 355 -- Look for rockspecs in some common locations.
355 collect_rockspecs(versions, paths, unnamed_paths, ".") 356 collect_rockspecs(versions, paths, unnamed_paths, ".")
@@ -391,14 +392,14 @@ end
391-- Quote Lua string, analogous to fs.Q. 392-- Quote Lua string, analogous to fs.Q.
392-- @param s A string, such as "hello" 393-- @param s A string, such as "hello"
393-- @return string: A quoted string, such as '"hello"' 394-- @return string: A quoted string, such as '"hello"'
394function util.LQ(s) 395function util.LQ(s: string): string
395 return ("%q"):format(s) 396 return ("%q"):format(s)
396end 397end
397 398
398-- Split name and namespace of a package name. 399-- Split name and namespace of a package name.
399-- @param ns_name a name that may be in "namespace/name" format 400-- @param ns_name a name that may be in "namespace/name" format
400-- @return string, string? - name and optionally a namespace 401-- @return string, string? - name and optionally a namespace
401function util.split_namespace(ns_name) 402function util.split_namespace(ns_name: string): string, string
402 local p1, p2 = ns_name:match("^([^/]+)/([^/]+)$") 403 local p1, p2 = ns_name:match("^([^/]+)/([^/]+)$")
403 if p1 then 404 if p1 then
404 return p2, p1 405 return p2, p1
@@ -407,10 +408,7 @@ function util.split_namespace(ns_name)
407end 408end
408 409
409--- Argparse action callback for namespaced rock arguments. 410--- Argparse action callback for namespaced rock arguments.
410function util.namespaced_name_action(args, target, ns_name) 411function util.namespaced_name_action(args: {any: any}, target: string, ns_name: string) --? Fn.args
411 assert(type(args) == "table")
412 assert(type(target) == "string")
413 assert(type(ns_name) == "string" or not ns_name)
414 412
415 if not ns_name then 413 if not ns_name then
416 return 414 return
@@ -427,10 +425,10 @@ function util.namespaced_name_action(args, target, ns_name)
427 end 425 end
428end 426end
429 427
430function util.deep_copy(tbl) 428function util.deep_copy(tbl: {any: any}): {any: any}
431 local copy = {} 429 local copy: {any: any} = {}
432 for k, v in pairs(tbl) do 430 for k, v in pairs(tbl) do
433 if type(v) == "table" then 431 if v is {any: any} then
434 copy[k] = util.deep_copy(v) 432 copy[k] = util.deep_copy(v)
435 else 433 else
436 copy[k] = v 434 copy[k] = v
@@ -442,8 +440,8 @@ end
442-- A portable version of fs.exists that can be used at early startup, 440-- A portable version of fs.exists that can be used at early startup,
443-- before the platform has been determined and luarocks.fs has been 441-- before the platform has been determined and luarocks.fs has been
444-- initialized. 442-- initialized.
445function util.exists(file) 443function util.exists(file: string): boolean
446 local fd, _, code = io.open(file, "r") 444 local fd, _, code = io.open(file, "r") --! both tl.tl and the lua docs say that the method gives 2 returns: (FILE and string)
447 if code == 13 then 445 if code == 13 then
448 -- code 13 means "Permission denied" on both Unix and Windows 446 -- code 13 means "Permission denied" on both Unix and Windows
449 -- io.open on folders always fails with code 13 on Windows 447 -- io.open on folders always fails with code 13 on Windows
@@ -456,19 +454,33 @@ function util.exists(file)
456 return false 454 return false
457end 455end
458 456
457function util.lua_is_wrapper(interp: string): boolean, string
458 local fd, err = io.open(interp, "r")
459 if not fd then
460 return nil, err
461 end
462 local data: string
463 data, err = fd:read(1000)
464 fd:close()
465 if not data then
466 return nil, err
467 end
468 return not not data:match("LUAROCKS_SYSCONFDIR") --! not not?!?! string to true?
469end
470
459do 471do
460 local function Q(pathname) 472 local function Q(pathname: string): string
461 if pathname:match("^.:") then 473 if pathname:match("^.:") then
462 return pathname:sub(1, 2) .. '"' .. pathname:sub(3) .. '"' 474 return pathname:sub(1, 2) .. '"' .. pathname:sub(3) .. '"'
463 end 475 end
464 return '"' .. pathname .. '"' 476 return '"' .. pathname .. '"'
465 end 477 end
466 478
467 function util.check_lua_version(lua, luaver) 479 function util.check_lua_version(lua:string, luaver: string): string
468 if not util.exists(lua) then 480 if not util.exists(lua) then
469 return nil 481 return nil
470 end 482 end
471 local lv, err = util.popen_read(Q(lua) .. ' -e "io.write(_VERSION:sub(5))"') 483 local lv = util.popen_read(Q(lua) .. ' -e "io.write(_VERSION:sub(5))"')
472 if lv == "" then 484 if lv == "" then
473 return nil 485 return nil
474 end 486 end
@@ -478,7 +490,7 @@ do
478 return lv 490 return lv
479 end 491 end
480 492
481 function util.get_luajit_version() 493 function util.get_luajit_version(): string --? cfg.cache.luajit_version no context
482 local cfg = require("luarocks.core.cfg") 494 local cfg = require("luarocks.core.cfg")
483 if cfg.cache.luajit_version_checked then 495 if cfg.cache.luajit_version_checked then
484 return cfg.cache.luajit_version 496 return cfg.cache.luajit_version
@@ -489,7 +501,7 @@ do
489 return nil 501 return nil
490 end 502 end
491 503
492 local ljv 504 local ljv: string --? above a string?
493 if cfg.lua_version == "5.1" then 505 if cfg.lua_version == "5.1" then
494 -- Ignores extra version info for custom builds, e.g. "LuaJIT 2.1.0-beta3 some-other-version-info" 506 -- Ignores extra version info for custom builds, e.g. "LuaJIT 2.1.0-beta3 some-other-version-info"
495 ljv = util.popen_read(Q(cfg.variables.LUA) .. ' -e "io.write(tostring(jit and jit.version:gsub([[^%S+ (%S+).*]], [[%1]])))"') 507 ljv = util.popen_read(Q(cfg.variables.LUA) .. ' -e "io.write(tostring(jit and jit.version:gsub([[^%S+ (%S+).*]], [[%1]])))"')
@@ -501,11 +513,18 @@ do
501 return ljv 513 return ljv
502 end 514 end
503 515
504 local find_lua_bindir 516 local find_lua_bindir: function(prefix: string, luaver: string, verbose: string): string, string, string
505 do 517 do
506 local exe_suffix = (package.config:sub(1, 1) == "\\" and ".exe" or "") 518 local exe_suffix = (package.config:sub(1, 1) == "\\" and ".exe" or "")
507 519
508 local function insert_lua_variants(names, luaver) 520 local record names_record
521 names_hash: {string: string}
522 names_array: {string}
523 end
524
525 local type HashArrayRecord = names_record
526
527 local function insert_lua_variants(names: HashArrayRecord, luaver: string) --! atempt 1 bellow comented, this is atempt 2, original in the util-original file
509 local variants = { 528 local variants = {
510 "lua" .. luaver .. exe_suffix, 529 "lua" .. luaver .. exe_suffix,
511 "lua" .. luaver:gsub("%.", "") .. exe_suffix, 530 "lua" .. luaver:gsub("%.", "") .. exe_suffix,
@@ -513,13 +532,27 @@ do
513 "lua-" .. luaver:gsub("%.", "") .. exe_suffix, 532 "lua-" .. luaver:gsub("%.", "") .. exe_suffix,
514 } 533 }
515 for _, name in ipairs(variants) do 534 for _, name in ipairs(variants) do
516 names[name] = luaver 535 names.names_hash[name] = luaver --! this wants indexing by string --! names is never used as a hesh
517 table.insert(names, name) 536 table.insert(names.names_array, name) --! this wants indexing by integer
518 end 537 end
519 end 538 end
520 539
521 find_lua_bindir = function(prefix, luaver, verbose) 540 -- local function insert_lua_variants(names: {string}, luaver: string)
522 local names = {} 541 -- local variants = {
542 -- "lua" .. luaver .. exe_suffix,
543 -- "lua" .. luaver:gsub("%.", "") .. exe_suffix,
544 -- "lua-" .. luaver .. exe_suffix,
545 -- "lua-" .. luaver:gsub("%.", "") .. exe_suffix,
546 -- }
547 -- local names_length = #names
548 -- for i, name in ipairs(variants) do
549 -- names[name] = luaver --! this wants indexing by string
550 -- names[i + names_length] = name
551 -- end
552 -- end
553
554 find_lua_bindir = function(prefix: string, luaver: string, verbose: string): string, string, string
555 local names: HashArrayRecord = {}
523 if luaver then 556 if luaver then
524 insert_lua_variants(names, luaver) 557 insert_lua_variants(names, luaver)
525 else 558 else
@@ -528,14 +561,14 @@ do
528 end 561 end
529 end 562 end
530 if luaver == "5.1" or not luaver then 563 if luaver == "5.1" or not luaver then
531 table.insert(names, "luajit" .. exe_suffix) 564 table.insert(names.names_array, "luajit" .. exe_suffix)
532 end 565 end
533 table.insert(names, "lua" .. exe_suffix) 566 table.insert(names.names_array, "lua" .. exe_suffix)
534 567
535 local tried = {} 568 local tried = {}
536 local dir_sep = package.config:sub(1, 1) 569 local dir_sep = package.config:sub(1, 1)
537 for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do 570 for _, d in ipairs({ prefix .. dir_sep .. "bin", prefix }) do
538 for _, name in ipairs(names) do 571 for _, name in ipairs(names.names_array) do
539 local lua = d .. dir_sep .. name 572 local lua = d .. dir_sep .. name
540 local is_wrapper, err = util.lua_is_wrapper(lua) 573 local is_wrapper, err = util.lua_is_wrapper(lua)
541 if is_wrapper == false then 574 if is_wrapper == false then
@@ -558,14 +591,14 @@ do
558 end 591 end
559 end 592 end
560 593
561 function util.find_lua(prefix, luaver, verbose) 594 function util.find_lua(prefix: string, luaver: string, verbose: string): {string: string}, string
562 local lua, bindir 595 local lua, bindir: string, string
563 lua, bindir, luaver = find_lua_bindir(prefix, luaver, verbose) 596 lua, bindir, luaver = find_lua_bindir(prefix, luaver, verbose)
564 if not lua then 597 if not lua then
565 return nil, bindir 598 return nil, bindir
566 end 599 end
567 600
568 return { 601 return { --? {string: string}
569 lua_version = luaver, 602 lua_version = luaver,
570 lua = lua, 603 lua = lua,
571 lua_dir = prefix, 604 lua_dir = prefix,
@@ -574,29 +607,16 @@ do
574 end 607 end
575end 608end
576 609
577function util.lua_is_wrapper(interp) 610function util.opts_table(type_name: string, valid_opts: {string: string})
578 local fd, err = io.open(interp, "r") 611 local opts_mt: metatable<string> = {} --Option??
579 if not fd then
580 return nil, err
581 end
582 local data, err = fd:read(1000)
583 fd:close()
584 if not data then
585 return nil, err
586 end
587 return not not data:match("LUAROCKS_SYSCONFDIR")
588end
589
590function util.opts_table(type_name, valid_opts)
591 local opts_mt = {}
592 612
593 opts_mt.__index = opts_mt 613 opts_mt.__index = opts_mt
594 614
595 function opts_mt.type() 615 function opts_mt.type(): string
596 return type_name 616 return type_name
597 end 617 end
598 618
599 return function(opts) 619 return function(opts: {string: string})
600 for k, v in pairs(opts) do 620 for k, v in pairs(opts) do
601 local tv = type(v) 621 local tv = type(v)
602 if not valid_opts[k] then 622 if not valid_opts[k] then
@@ -624,10 +644,10 @@ end
624-- @return a table with rock names as keys and versions and values, 644-- @return a table with rock names as keys and versions and values,
625-- specifying modules that are already provided by the VM (including 645-- specifying modules that are already provided by the VM (including
626-- "lua" for the Lua version and, for format 3.0+, "luajit" if detected). 646-- "lua" for the Lua version and, for format 3.0+, "luajit" if detected).
627function util.get_rocks_provided(rockspec) 647function util.get_rocks_provided(rockspec: Rockspec): {Rockspec}
628 local cfg = require("luarocks.core.cfg") 648 local cfg = require("luarocks.core.cfg")
629 649
630 if not rockspec and cfg.cache.rocks_provided then 650 if not rockspec and cfg.cache.rocks_provided then --rocks_provided: Rockspec
631 return cfg.cache.rocks_provided 651 return cfg.cache.rocks_provided
632 end 652 end
633 653
@@ -660,13 +680,13 @@ function util.get_rocks_provided(rockspec)
660 end 680 end
661 681
662 if not rockspec then 682 if not rockspec then
663 cfg.cache.rocks_provided = rocks_provided 683 cfg.cache.rocks_provided = rocks_provided --!
664 end 684 end
665 685
666 return rocks_provided 686 return rocks_provided
667end 687end
668 688
669function util.remove_doc_dir(name, version) 689function util.remove_doc_dir(name: string, version: string)
670 local path = require("luarocks.path") 690 local path = require("luarocks.path")
671 local fs = require("luarocks.fs") 691 local fs = require("luarocks.fs")
672 local dir = require("luarocks.dir") 692 local dir = require("luarocks.dir")