From bd1c9d3f2e2e289e5b0cf98a65381ef6ca8e4b07 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 23 Aug 2024 19:34:00 -0300 Subject: repos: simplify logic to avoid cast, consistent callback returns --- src/luarocks/cmd/show.lua | 3 +++ src/luarocks/cmd/show.tl | 3 +++ src/luarocks/repos.lua | 35 ++++++++++++++++++++--------------- src/luarocks/repos.tl | 37 +++++++++++++++++++++---------------- 4 files changed, 47 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd/show.lua b/src/luarocks/cmd/show.lua index 58d6701e..7f9ca9c6 100644 --- a/src/luarocks/cmd/show.lua +++ b/src/luarocks/cmd/show.lua @@ -206,12 +206,14 @@ local function modules_to_list(name, version, repo) name = path.path_to_module(pathname), file = adjust_path(name, version, lua_dir, pathname), }) + return true end) repos.recurse_rock_manifest_entry(rock_manifest.lib, function(pathname) table.insert(ret, { name = path.path_to_module(pathname), file = adjust_path(name, version, lib_dir, pathname), }) + return true end) table.sort(ret, function(a, b) if a.name == b.name then @@ -232,6 +234,7 @@ local function commands_to_list(name, version, repo) name = name, file = adjust_path(name, version, bin_dir, pathname, cfg.wrapper_suffix), }) + return true end) table.sort(ret, function(a, b) if a.name == b.name then diff --git a/src/luarocks/cmd/show.tl b/src/luarocks/cmd/show.tl index 08dd9895..6063179a 100644 --- a/src/luarocks/cmd/show.tl +++ b/src/luarocks/cmd/show.tl @@ -206,12 +206,14 @@ local function modules_to_list(name: string, version: string, repo: string | Tre name = path.path_to_module(pathname), file = adjust_path(name, version, lua_dir, pathname), }) + return true end) repos.recurse_rock_manifest_entry(rock_manifest.lib, function(pathname: string): boolean, string table.insert(ret, { name = path.path_to_module(pathname), file = adjust_path(name, version, lib_dir, pathname), }) + return true end) table.sort(ret, function(a: Return, b: Return): boolean if a.name == b.name then @@ -232,6 +234,7 @@ local function commands_to_list(name: string, version: string, repo: string | Tr name = name, file = adjust_path(name, version, bin_dir, pathname, cfg.wrapper_suffix), }) + return true end) table.sort(ret, function(a: Return, b: Return): boolean if a.name == b.name then diff --git a/src/luarocks/repos.lua b/src/luarocks/repos.lua index 84c01815..956825e4 100644 --- a/src/luarocks/repos.lua +++ b/src/luarocks/repos.lua @@ -79,21 +79,19 @@ function repos.recurse_rock_manifest_entry(entry, action) return true end - local function do_recurse_rock_manifest_entry(tree, parent_path) - - for file, sub in pairs(tree) do - local sub_path = (parent_path and (parent_path .. "/") or "") .. file - local ok, err - - if type(sub) == "table" then - ok, err = do_recurse_rock_manifest_entry(sub, sub_path) - else - ok, err = action(sub_path) + local function do_recurse_rock_manifest_entry(tree, pathname) + if type(tree) == "string" then + return action(pathname) + else + for file, sub in pairs(tree) do + local sub_path = pathname and dir.path(pathname, file) or file + local ok, err = do_recurse_rock_manifest_entry(sub, sub_path) + if not ok then + return nil, err + end end - - if err then return nil, err end + return true end - return true end return do_recurse_rock_manifest_entry(entry) end @@ -412,14 +410,15 @@ function repos.check_everything_is_installed(name, version, rock_manifest, repo, if category == "bin" then if (fs.exists(paths.nv) or fs.exists(paths.nv .. suffix)) or (accept_versioned and (fs.exists(paths.v) or fs.exists(paths.v .. suffix))) then - return + return true end else if fs.exists(paths.nv) or (accept_versioned and fs.exists(paths.v)) then - return + return true end end table.insert(missing, paths.nv) + return true end) end end @@ -481,6 +480,7 @@ function repos.deploy_local_files(name, version, wrap_bin_scripts, deps_mode) target = target .. (cfg.wrapper_suffix or "") end table.insert(installs, { fn = install_binary, src = source, dst = target, backup = backup }) + return true end) end @@ -500,6 +500,7 @@ function repos.deploy_local_files(name, version, wrap_bin_scripts, deps_mode) local target = mode == "nv" and paths.nv or paths.v local backup = name ~= cur_name or version ~= cur_version table.insert(installs, { fn = move_lua, src = source, dst = target, backup = backup }) + return true end) end @@ -519,6 +520,7 @@ function repos.deploy_local_files(name, version, wrap_bin_scripts, deps_mode) local target = mode == "nv" and paths.nv or paths.v local backup = name ~= cur_name or version ~= cur_version table.insert(installs, { fn = move_lib, src = source, dst = target, backup = backup }) + return true end) end @@ -613,6 +615,7 @@ function repos.delete_local_version(name, version, deps_mode, quick) table.insert(renames, { src = next_paths.v, dst = next_paths.nv, suffix = cfg.wrapper_suffix }) end end + return true end) end @@ -634,6 +637,7 @@ function repos.delete_local_version(name, version, deps_mode, quick) table.insert(renames, { src = next_lib_paths.v, dst = next_lib_paths.nv }) end end + return true end) end @@ -655,6 +659,7 @@ function repos.delete_local_version(name, version, deps_mode, quick) table.insert(renames, { src = next_lib_paths.v, dst = next_lib_paths.nv }) end end + return true end) end diff --git a/src/luarocks/repos.tl b/src/luarocks/repos.tl index 46458607..29645112 100644 --- a/src/luarocks/repos.tl +++ b/src/luarocks/repos.tl @@ -79,21 +79,19 @@ function repos.recurse_rock_manifest_entry(entry: Entry, action: (function(strin return true end - local function do_recurse_rock_manifest_entry(tree: Entry, parent_path?: string): boolean, string - - for file, sub in pairs(tree as {string: any}) do --! - local sub_path = (parent_path and (parent_path .. "/") or "") .. file - local ok, err: boolean, string -- luacheck: ignore 231 - - if sub is Entry then - ok, err = do_recurse_rock_manifest_entry(sub, sub_path) - else - ok, err = action(sub_path) + local function do_recurse_rock_manifest_entry(tree: Entry, pathname?: string): boolean, string + if tree is string then + return action(pathname) + else + for file, sub in pairs(tree) do + local sub_path = pathname and dir.path(pathname, file) or file + local ok, err = do_recurse_rock_manifest_entry(sub, sub_path) + if not ok then + return nil, err + end end - - if err then return nil, err end + return true end - return true end return do_recurse_rock_manifest_entry(entry) end @@ -412,14 +410,15 @@ function repos.check_everything_is_installed(name: string, version: string, rock if category == "bin" then if (fs.exists(paths.nv) or fs.exists(paths.nv .. suffix)) or (accept_versioned and (fs.exists(paths.v) or fs.exists(paths.v .. suffix))) then - return + return true end else if fs.exists(paths.nv) or (accept_versioned and fs.exists(paths.v)) then - return + return true end end table.insert(missing, paths.nv) + return true end) end end @@ -481,6 +480,7 @@ function repos.deploy_local_files(name: string, version: string, wrap_bin_script target = target .. (cfg.wrapper_suffix or "") end table.insert(installs, { fn = install_binary, src = source, dst = target, backup = backup }) + return true end) end @@ -500,6 +500,7 @@ function repos.deploy_local_files(name: string, version: string, wrap_bin_script local target = mode == "nv" and paths.nv or paths.v local backup = name ~= cur_name or version ~= cur_version table.insert(installs, { fn = move_lua, src = source, dst = target, backup = backup }) + return true end) end @@ -519,6 +520,7 @@ function repos.deploy_local_files(name: string, version: string, wrap_bin_script local target = mode == "nv" and paths.nv or paths.v local backup = name ~= cur_name or version ~= cur_version table.insert(installs, { fn = move_lib, src = source, dst = target, backup = backup }) + return true end) end @@ -613,6 +615,7 @@ function repos.delete_local_version(name: string, version: string, deps_mode: st table.insert(renames, { src = next_paths.v, dst = next_paths.nv, suffix = cfg.wrapper_suffix }) end end + return true end) end @@ -634,6 +637,7 @@ function repos.delete_local_version(name: string, version: string, deps_mode: st table.insert(renames, { src = next_lib_paths.v, dst = next_lib_paths.nv }) end end + return true end) end @@ -655,6 +659,7 @@ function repos.delete_local_version(name: string, version: string, deps_mode: st table.insert(renames, { src = next_lib_paths.v, dst = next_lib_paths.nv }) end end + return true end) end @@ -687,4 +692,4 @@ function repos.delete_local_version(name: string, version: string, deps_mode: st return true, nil, "remove" end -return repos \ No newline at end of file +return repos -- cgit v1.2.3-55-g6feb