From 3ebe7058f649bb0045e844ba0c119ac9dd445d9b Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 22 Aug 2024 17:49:06 -0300 Subject: Teal: convert luarocks.dir --- src/luarocks/dir.lua | 63 -------------------------------------------------- src/luarocks/dir.tl | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 63 deletions(-) delete mode 100644 src/luarocks/dir.lua create mode 100644 src/luarocks/dir.tl diff --git a/src/luarocks/dir.lua b/src/luarocks/dir.lua deleted file mode 100644 index be89e37b..00000000 --- a/src/luarocks/dir.lua +++ /dev/null @@ -1,63 +0,0 @@ - ---- Generic utilities for handling pathnames. -local dir = {} - -local core = require("luarocks.core.dir") - -dir.path = core.path -dir.split_url = core.split_url -dir.normalize = core.normalize - -local dir_sep = package.config:sub(1, 1) - ---- Strip the path off a path+filename. --- @param pathname string: A path+name, such as "/a/b/c" --- or "\a\b\c". --- @return string: The filename without its path, such as "c". -function dir.base_name(pathname) - assert(type(pathname) == "string") - - local b - b = pathname:gsub("[/\\]", "/") -- canonicalize to forward slashes - b = b:gsub("/*$", "") -- drop trailing slashes - b = b:match(".*[/\\]([^/\\]*)") -- match last component - b = b or pathname -- fallback to original if no slashes - - return b -end - ---- Strip the name off a path+filename. --- @param pathname string: A path+name, such as "/a/b/c". --- @return string: The filename without its path, such as "/a/b". --- For entries such as "/a/b/", "/a" is returned. If there are --- no directory separators in input, "" is returned. -function dir.dir_name(pathname) - assert(type(pathname) == "string") - - local d - d = pathname:gsub("[/\\]", "/") -- canonicalize to forward slashes - d = d:gsub("/*$", "") -- drop trailing slashes - d = d:match("(.*)[/]+[^/]*") -- match all components but the last - d = d or "" -- switch to "" if there's no match - d = d:gsub("/", dir_sep) -- decanonicalize to native slashes - - return d -end - ---- Returns true if protocol does not require additional tools. --- @param protocol The protocol name -function dir.is_basic_protocol(protocol) - return protocol == "http" or protocol == "https" or protocol == "ftp" or protocol == "file" -end - -function dir.deduce_base_dir(url) - -- for extensions like foo.tar.gz, "gz" is stripped first - local known_exts = {} - for _, ext in ipairs{"zip", "git", "tgz", "tar", "gz", "bz2"} do - known_exts[ext] = "" - end - local base = dir.base_name(url) - return (base:gsub("%.([^.]*)$", known_exts):gsub("%.tar", "")) -end - -return dir diff --git a/src/luarocks/dir.tl b/src/luarocks/dir.tl new file mode 100644 index 00000000..e9e16fbd --- /dev/null +++ b/src/luarocks/dir.tl @@ -0,0 +1,65 @@ + +--- Generic utilities for handling pathnames. +local record dir + path: function(...: string): string + split_url: function(string): string, string + normalize: function(string): string +end + +local core = require("luarocks.core.dir") + +dir.path = core.path +dir.split_url = core.split_url +dir.normalize = core.normalize + +local dir_sep = package.config:sub(1, 1) + +--- Strip the path off a path+filename. +-- @param pathname string: A path+name, such as "/a/b/c" +-- or "\a\b\c". +-- @return string: The filename without its path, such as "c". +function dir.base_name(pathname: string): string + + local b: string + b = pathname:gsub("[/\\]", "/") -- canonicalize to forward slashes + b = b:gsub("/*$", "") -- drop trailing slashes + b = b:match(".*[/\\]([^/\\]*)") -- match last component + b = b or pathname -- fallback to original if no slashes + + return b +end + +--- Strip the name off a path+filename. +-- @param pathname string: A path+name, such as "/a/b/c". +-- @return string: The filename without its path, such as "/a/b". +-- For entries such as "/a/b/", "/a" is returned. If there are +-- no directory separators in input, "" is returned. +function dir.dir_name(pathname: string): string + + local d: string + d = pathname:gsub("[/\\]", "/") -- canonicalize to forward slashes + d = d:gsub("/*$", "") -- drop trailing slashes + d = d:match("(.*)[/]+[^/]*") -- match all components but the last + d = d or "" -- switch to "" if there's no match + d = d:gsub("/", dir_sep) -- decanonicalize to native slashes + + return d +end + +--- Returns true if protocol does not require additional tools. +-- @param protocol The protocol name +function dir.is_basic_protocol(protocol: string): boolean + return protocol == "http" or protocol == "https" or protocol == "ftp" or protocol == "file" +end + +function dir.deduce_base_dir(url: string): string + -- for extensions like foo.tar.gz, "gz" is stripped first + local known_exts = {} + for _, ext in ipairs{"zip", "git", "tgz", "tar", "gz", "bz2"} do + known_exts[ext] = "" + end + local base = dir.base_name(url) + return (base:gsub("%.([^.]*)$", known_exts):gsub("%.tar", "")) +end + +return dir -- cgit v1.2.3-55-g6feb