From 3be22b3f07dc1558c7734cf7639e3468ec8de796 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 2 Sep 2024 17:50:32 -0300 Subject: Teal: make luarocks.fs not dependent on luarocks.util Break circular dependency detected by Cyan --- src/luarocks/core/util.lua | 26 ++++++++++++++++++++++++++ src/luarocks/core/util.tl | 36 +++++++++++++++++++++++++++++++----- src/luarocks/fs/lua.lua | 2 +- src/luarocks/util.lua | 25 ++++--------------------- src/luarocks/util.tl | 25 ++++--------------------- 5 files changed, 66 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 6a457c54..76ec1a62 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua @@ -331,4 +331,30 @@ function util.sortedpairs(tbl, sort_by) end end + + + + + + + + +function util.exists(file) + local fd, _, code = io.open(file, "r") + if code == 13 then + + + return true + end + if fd then + fd:close() + return true + end + return false +end + +function util.starts_with(s, prefix) + return s:sub(1, #prefix) == prefix +end + return util diff --git a/src/luarocks/core/util.tl b/src/luarocks/core/util.tl index a9be2d43..b1bbea05 100644 --- a/src/luarocks/core/util.tl +++ b/src/luarocks/core/util.tl @@ -298,13 +298,13 @@ function util.sortedpairs(tbl: {K: V}, sort_by?: SortBy): function(): K table.sort(keys, sort_by) else -- sort_by is Ordering - + sub_orders = sort_by.sub_orders local seen_ordered_key: {K: boolean} = {} - + local my_ordered_keys: {K} = {} - + for _, key in ipairs(sort_by) do if tbl[key] then seen_ordered_key[key] = true @@ -319,10 +319,10 @@ function util.sortedpairs(tbl: {K: V}, sort_by?: SortBy): function(): K table.insert(my_ordered_keys, key) end end - + keys = my_ordered_keys end - + local i = 1 return function(): K, V, Ordering local key = keys[i] @@ -331,5 +331,31 @@ function util.sortedpairs(tbl: {K: V}, sort_by?: SortBy): function(): K end end +-------------------------------------------------------------------------------- +-- Functions needed by luarocks.fs, +-- added here to avoid a circular dependency: +-------------------------------------------------------------------------------- + +-- A portable version of fs.exists that can be used at early startup, +-- before the platform has been determined and luarocks.fs has been +-- initialized. +function util.exists(file: string): boolean + local fd, _, code = io.open(file, "r") + if code == 13 then + -- code 13 means "Permission denied" on both Unix and Windows + -- io.open on folders always fails with code 13 on Windows + return true + end + if fd then + fd:close() + return true + end + return false +end + +function util.starts_with(s: string, prefix: string): boolean + return s:sub(1,#prefix) == prefix +end + return util diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index fdf3af78..02d9353a 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -8,7 +8,7 @@ local fs = require("luarocks.fs") local cfg = require("luarocks.core.cfg") local dir = require("luarocks.dir") -local util = require("luarocks.util") +local util = require("luarocks.core.util") local vers = require("luarocks.core.vers") local pack = table.pack or function(...) return { n = select("#", ...), ... } end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 0ea480be..15ed2cf0 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -29,6 +29,8 @@ local util = {Fn = {}, } + + util.cleanup_path = core.cleanup_path util.split_string = core.split_string util.sortedpairs = core.sortedpairs @@ -40,6 +42,8 @@ util.printerr = core.printerr util.warning = core.warning util.keys = core.keys util.matchquote = core.matchquote +util.exists = core.exists +util.starts_with = core.starts_with @@ -188,10 +192,6 @@ function util.lua_path_variables() return lpath_var, lcpath_var end -function util.starts_with(s, prefix) - return s:sub(1, #prefix) == prefix -end - function util.printout(...) io.stdout:write(table.concat({ ... }, "\t")) @@ -396,23 +396,6 @@ function util.deep_copy(tbl) return copy end - - - -function util.exists(file) - local fd, _, code = io.open(file, "r") - if code == 13 then - - - return true - end - if fd then - fd:close() - return true - end - return false -end - function util.lua_is_wrapper(interp) local fd, err = io.open(interp, "r") if not fd then diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl index 80d39b16..436914ab 100644 --- a/src/luarocks/util.tl +++ b/src/luarocks/util.tl @@ -22,6 +22,8 @@ local record util warning: function(string) keys: function({K : V}): {K} matchquote: function(string): string + exists: function(string): boolean + starts_with: function(s: string, prefix: string): boolean record Fn fn: function(any) @@ -40,6 +42,8 @@ util.printerr = core.printerr util.warning = core.warning util.keys = core.keys util.matchquote = core.matchquote +util.exists = core.exists +util.starts_with = core.starts_with local type Fn = util.Fn local type Rockspec = require("luarocks.core.types.rockspec").Rockspec @@ -188,10 +192,6 @@ function util.lua_path_variables(): string, string return lpath_var, lcpath_var end -function util.starts_with(s: string, prefix: string): boolean - return s:sub(1,#prefix) == prefix -end - --- Print a line to standard output function util.printout(...: string) io.stdout:write(table.concat({...},"\t")) @@ -396,23 +396,6 @@ function util.deep_copy(tbl: {any: any}): {any: any} return copy end --- A portable version of fs.exists that can be used at early startup, --- before the platform has been determined and luarocks.fs has been --- initialized. -function util.exists(file: string): boolean - local fd, _, code = io.open(file, "r") - if code == 13 then - -- code 13 means "Permission denied" on both Unix and Windows - -- io.open on folders always fails with code 13 on Windows - return true - end - if fd then - fd:close() - return true - end - return false -end - function util.lua_is_wrapper(interp: string): boolean, string local fd, err = io.open(interp, "r") if not fd then -- cgit v1.2.3-55-g6feb