From 3d585c3b3fba291374602750ef3201a017cb60db Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 23 Aug 2024 20:30:10 -0300 Subject: deplocks: simplify types --- src/luarocks/core/manif.tl | 6 ++-- src/luarocks/core/persist.tl | 2 +- src/luarocks/core/types/depskey.d.tl | 9 ++++++ src/luarocks/deplocks.lua | 44 ++++++++++++++------------- src/luarocks/deplocks.tl | 58 +++++++++++++++++++----------------- src/luarocks/deps.lua | 11 ++----- src/luarocks/deps.tl | 25 ++++++---------- src/luarocks/test.tl | 3 +- 8 files changed, 81 insertions(+), 77 deletions(-) create mode 100644 src/luarocks/core/types/depskey.d.tl (limited to 'src') diff --git a/src/luarocks/core/manif.tl b/src/luarocks/core/manif.tl index f75bf28e..1f3b3659 100644 --- a/src/luarocks/core/manif.tl +++ b/src/luarocks/core/manif.tl @@ -52,11 +52,11 @@ 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: {any: any}, {any: any} | string, string = persist.load_into_table(file) + local manifest, err, errcode = persist.load_into_table(file) if not manifest and err is string then return nil, "Failed loading manifest for "..repo_url..": " .. err, errcode end - + manif.cache_manifest(repo_url, lua_version, manifest as Manifest) -- No runtime check if manifest is actually a Manifest! return manifest as Manifest, err, errcode end @@ -88,7 +88,7 @@ function manif.load_rocks_tree_manifests(deps_mode?: string): {Tree_manifest} return trees end -function manif.scan_dependencies(name: string, version: string, tree_manifests: {Tree_manifest}, dest: {any: any}) +function manif.scan_dependencies(name: string, version: string, tree_manifests: {Tree_manifest}, dest: {string: string}) if dest[name] then return end diff --git a/src/luarocks/core/persist.tl b/src/luarocks/core/persist.tl index 89cac97e..4694afcb 100644 --- a/src/luarocks/core/persist.tl +++ b/src/luarocks/core/persist.tl @@ -45,7 +45,7 @@ end -- 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_into_table(filename: string, tbl?: {string:any}) : {any: any}, {any: any} | string, string +function persist.load_into_table(filename: string, tbl?: {string:any}) : {string: any}, {string: boolean} | string, string local result: {string:any} = tbl or {} local globals = {} diff --git a/src/luarocks/core/types/depskey.d.tl b/src/luarocks/core/types/depskey.d.tl new file mode 100644 index 00000000..9d15fd25 --- /dev/null +++ b/src/luarocks/core/types/depskey.d.tl @@ -0,0 +1,9 @@ +local record depskey + enum DepsKey + "dependencies" + "build_dependencies" + "test_dependencies" + end +end + +return depskey diff --git a/src/luarocks/deplocks.lua b/src/luarocks/deplocks.lua index 8a21ef1b..b07c91f7 100644 --- a/src/luarocks/deplocks.lua +++ b/src/luarocks/deplocks.lua @@ -1,5 +1,11 @@ local deplocks = {} + + + + + + local fs = require("luarocks.fs") local dir = require("luarocks.dir") local util = require("luarocks.util") @@ -7,22 +13,23 @@ local persist = require("luarocks.persist") -local deptable = {} -local deptable_mode = "start" + +local depstable = {} +local depstable_mode = "start" local deplock_abs_filename local deplock_root_rock_name function deplocks.init(root_rock_name, dirname) - if deptable_mode ~= "start" then + if depstable_mode ~= "start" then return end - deptable_mode = "create" + depstable_mode = "create" local filename = dir.path(dirname, "luarocks.lock") deplock_abs_filename = fs.absolute_name(filename) deplock_root_rock_name = root_rock_name - deptable = {} + depstable = {} end function deplocks.get_abs_filename(root_rock_name) @@ -32,10 +39,10 @@ function deplocks.get_abs_filename(root_rock_name) end function deplocks.load(root_rock_name, dirname) - if deptable_mode ~= "start" then + if depstable_mode ~= "start" then return true, nil end - deptable_mode = "locked" + depstable_mode = "locked" local filename = dir.path(dirname, "luarocks.lock") local _, result, errcode = persist.run_file(filename, {}) @@ -52,19 +59,20 @@ function deplocks.load(root_rock_name, dirname) deplock_abs_filename = fs.absolute_name(filename) deplock_root_rock_name = root_rock_name - deptable = result + + depstable = result return true, filename end function deplocks.add(depskey, name, version) - if deptable_mode == "locked" then + if depstable_mode == "locked" then return end - local dk = deptable[depskey] + local dk = depstable[depskey] if not dk then dk = {} - deptable[depskey] = dk + depstable[depskey] = dk end if type(dk) == "table" and not dk[name] then @@ -73,23 +81,19 @@ function deplocks.add(depskey, name, version) end function deplocks.get(depskey, name) - local dk = deptable[depskey] - if not dk then - return nil - end + local dk = depstable[depskey] if type(dk) == "table" then return dk[name] - else - return dk end + return nil end function deplocks.write_file() - if deptable_mode ~= "create" then + if depstable_mode ~= "create" then return true end - return persist.save_as_module(deplock_abs_filename, deptable) + return persist.save_as_module(deplock_abs_filename, depstable) end @@ -105,7 +109,7 @@ function deplocks.proxy(depskey) end function deplocks.each(depskey) - return util.sortedpairs(deptable[depskey] or {}) + return util.sortedpairs(depstable[depskey] or {}) end return deplocks diff --git a/src/luarocks/deplocks.tl b/src/luarocks/deplocks.tl index 37c0c700..2c4889bd 100644 --- a/src/luarocks/deplocks.tl +++ b/src/luarocks/deplocks.tl @@ -1,28 +1,35 @@ local deplocks = {} +local record DepsTable + dependencies: {string: string} + build_dependencies: {string: string} + test_dependencies: {string: string} +end + local fs = require("luarocks.fs") local dir = require("luarocks.dir") local util = require("luarocks.util") local persist = require("luarocks.persist") +local type DepsKey = require("luarocks.core.types.depskey").DepsKey local type PersistableTable = require("luarocks.core.types.persist").PersistableTable -local deptable: PersistableTable = {} -local deptable_mode = "start" +local depstable: DepsTable = {} +local depstable_mode = "start" local deplock_abs_filename: string local deplock_root_rock_name: string function deplocks.init(root_rock_name: string, dirname: string) - if deptable_mode ~= "start" then + if depstable_mode ~= "start" then return end - deptable_mode = "create" + depstable_mode = "create" local filename = dir.path(dirname, "luarocks.lock") deplock_abs_filename = fs.absolute_name(filename) deplock_root_rock_name = root_rock_name - deptable = {} + depstable = {} end function deplocks.get_abs_filename(root_rock_name: string): string @@ -32,10 +39,10 @@ function deplocks.get_abs_filename(root_rock_name: string): string end function deplocks.load(root_rock_name: string, dirname: string): boolean, string, string - if deptable_mode ~= "start" then + if depstable_mode ~= "start" then return true, nil end - deptable_mode = "locked" + depstable_mode = "locked" local filename = dir.path(dirname, "luarocks.lock") local _, result, errcode = persist.run_file(filename, {}) @@ -52,50 +59,47 @@ function deplocks.load(root_rock_name: string, dirname: string): boolean, string deplock_abs_filename = fs.absolute_name(filename) deplock_root_rock_name = root_rock_name - deptable = result as PersistableTable --! cast + -- FIXME we're not really checking that the table is a DepsTable + depstable = result as DepsTable return true, filename end -function deplocks.add(depskey: string, name: string, version: string) - if deptable_mode == "locked" then +function deplocks.add(depskey: DepsKey, name: string, version: string) + if depstable_mode == "locked" then return end - local dk = deptable[depskey] + local dk = depstable[depskey] if not dk then dk = {} - deptable[depskey] = dk + depstable[depskey] = dk end - if dk is PersistableTable and not dk[name] then + if dk is {string: string} and not dk[name] then dk[name] = version end end -function deplocks.get(depskey: string, name: string): string | number | boolean | PersistableTable - local dk = deptable[depskey] - if not dk then - return nil - end - if dk is PersistableTable then +function deplocks.get(depskey: DepsKey, name: string): string + local dk = depstable[depskey] + if dk is {string: string} then return dk[name] - else - return dk end + return nil end function deplocks.write_file(): boolean, string - if deptable_mode ~= "create" then + if depstable_mode ~= "create" then return true end - return persist.save_as_module(deplock_abs_filename, deptable) + return persist.save_as_module(deplock_abs_filename, depstable as PersistableTable) end -- a table-like interface to deplocks -function deplocks.proxy(depskey: string): PersistableTable +function deplocks.proxy(depskey: DepsKey): {string: string} return setmetatable({}, { - __index = function(_, k: string): string | number | boolean | PersistableTable + __index = function(_, k: string): string return deplocks.get(depskey, k) end, __newindex = function(_, k: string, v: string) @@ -104,8 +108,8 @@ function deplocks.proxy(depskey: string): PersistableTable }) end -function deplocks.each(depskey: string): function(): string | number, string | number | boolean | PersistableTable - return util.sortedpairs(deptable[depskey] as PersistableTable or {}) +function deplocks.each(depskey: DepsKey): function(): string, string + return util.sortedpairs(depstable[depskey] or {}) end return deplocks diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index c960f3b7..f02070a6 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -296,16 +296,9 @@ function deps.fulfill_dependencies(rockspec, depskey, deps_mode, verify, deplock util.printout("Using dependencies pinned in lockfile: " .. filename) local get_versions = prepare_get_versions("none", rocks_provided, depskey) - local dnsnamestr, dversionstr for dnsname, dversion in deplocks.each(depskey) do - if type(dnsname) == "string" then - dnsnamestr = dnsname - end - if type(dversion) == "string" then - dversionstr = dversion - end - local dname, dnamespace = util.split_namespace(dnsnamestr) - local depq = queries.new(dname, dnamespace, dversionstr) + local dname, dnamespace = util.split_namespace(dnsname) + local depq = queries.new(dname, dnamespace, dversion) util.printout(("%s %s is pinned to %s (%s)"):format( name, version, tostring(depq), rock_status(depq, get_versions))) diff --git a/src/luarocks/deps.tl b/src/luarocks/deps.tl index e26ba60f..17584c36 100644 --- a/src/luarocks/deps.tl +++ b/src/luarocks/deps.tl @@ -28,13 +28,13 @@ local type Query = require("luarocks.core.types.query").Query local type Version = require("luarocks.core.types.version").Version -local type PersistableTable = require("luarocks.core.types.persist").PersistableTable - local type Result = require("luarocks.core.types.result").Result local type Dir = require("luarocks.core.types.dir").Dir local type Dirs = require("luarocks.core.types.dir").Dirs +local type DepsKey = require("luarocks.core.types.depskey").DepsKey + local type Args = require("luarocks.core.types.args").Args --- Generate a function that matches dep queries against the manifest, @@ -54,9 +54,9 @@ local type Args = require("luarocks.core.types.args").Args -- * map of versions to locations -- * version matched via lockfile if any -- * true if rock matched via rocks_provided -local function prepare_get_versions(deps_mode: string, rocks_provided: {string : string}, depskey: string, skip_set?: {string: {string: boolean}}): function(Query): {string}, {string: string | Tree}, string | number | boolean | PersistableTable, boolean +local function prepare_get_versions(deps_mode: string, rocks_provided: {string : string}, depskey: DepsKey, skip_set?: {string: {string: boolean}}): function(Query): {string}, {string: string | Tree}, string, boolean - return function(dep: Query): {string}, {string: string | Tree}, string | number | boolean | PersistableTable, boolean + return function(dep: Query): {string}, {string: string | Tree}, string, boolean local versions, locations: {string}, {string: string | Tree} local provided = rocks_provided[dep.name] if provided then @@ -78,7 +78,7 @@ local function prepare_get_versions(deps_mode: string, rocks_provided: {string : end end - local lockversion = deplocks.get(depskey, dep.name) --! cast? + local lockversion = deplocks.get(depskey, dep.name) return versions, locations, lockversion, provided ~= nil end @@ -206,7 +206,7 @@ function deps.report_missing_dependencies(name: string, version: string, depende end end -function deps.fulfill_dependency(dep: Query, deps_mode: string, rocks_provided: {string: string}, verify: boolean, depskey?: string): boolean, string, string | Tree +function deps.fulfill_dependency(dep: Query, deps_mode: string, rocks_provided: {string: string}, verify: boolean, depskey?: DepsKey): boolean, string, string | Tree deps_mode = deps_mode or "all" rocks_provided = rocks_provided or {} @@ -286,7 +286,7 @@ end -- @return boolean or (nil, string, [string]): True if no errors occurred, or -- nil and an error message if any test failed, followed by an optional -- error code. -function deps.fulfill_dependencies(rockspec: Rockspec, depskey: string, deps_mode: string, verify?: boolean, deplock_dir?: string): boolean, string, string +function deps.fulfill_dependencies(rockspec: Rockspec, depskey: DepsKey, deps_mode: string, verify?: boolean, deplock_dir?: string): boolean, string, string local name = rockspec.name local version = rockspec.version local rocks_provided = rockspec.rocks_provided @@ -296,16 +296,9 @@ function deps.fulfill_dependencies(rockspec: Rockspec, depskey: string, deps_mod util.printout("Using dependencies pinned in lockfile: " .. filename) local get_versions = prepare_get_versions("none", rocks_provided, depskey) - local dnsnamestr, dversionstr: string, string for dnsname, dversion in deplocks.each(depskey) do - if dnsname is string then - dnsnamestr = dnsname - end - if dversion is string then - dversionstr = dversion - end - local dname, dnamespace = util.split_namespace(dnsnamestr) - local depq = queries.new(dname, dnamespace, dversionstr) + local dname, dnamespace = util.split_namespace(dnsname) + local depq = queries.new(dname, dnamespace, dversion) util.printout(("%s %s is pinned to %s (%s)"):format( name, version, tostring(depq), rock_status(depq, get_versions))) diff --git a/src/luarocks/test.tl b/src/luarocks/test.tl index d076d264..e68096b8 100644 --- a/src/luarocks/test.tl +++ b/src/luarocks/test.tl @@ -7,6 +7,7 @@ local util = require("luarocks.util") local type Rockspec = require("luarocks.core.types.rockspec").Rockspec local type Dependencies = require("luarocks.core.types.rockspec").Dependencies +local type DepsKey = require("luarocks.core.types.depskey").DepsKey local type TestRunner = require("luarocks.core.types.testrunner").TestRunner @@ -66,7 +67,7 @@ function test.run_test_suite(rockspec_arg: string | Rockspec, test_type: string, end end - local all_deps = { + local all_deps: {DepsKey} = { "dependencies", "build_dependencies", "test_dependencies", -- cgit v1.2.3-55-g6feb