aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/core/dir.tl (renamed from src/luarocks/core/dir.lua)56
1 files changed, 27 insertions, 29 deletions
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.tl
index 5d6f2c9f..20068341 100644
--- a/src/luarocks/core/dir.lua
+++ b/src/luarocks/core/dir.tl
@@ -1,12 +1,10 @@
1 1local record dir
2local dir = {} 2end
3
4local require = nil
5-------------------------------------------------------------------------------- 3--------------------------------------------------------------------------------
6 4
7local dir_sep = package.config:sub(1, 1) 5local dir_sep = package.config:sub(1, 1)
8 6
9local function unquote(c) 7local function unquote(c: string): string
10 local first, last = c:sub(1,1), c:sub(-1) 8 local first, last = c:sub(1,1), c:sub(-1)
11 if (first == '"' and last == '"') or 9 if (first == '"' and last == '"') or
12 (first == "'" and last == "'") then 10 (first == "'" and last == "'") then
@@ -15,33 +13,12 @@ local function unquote(c)
15 return c 13 return c
16end 14end
17 15
18--- Describe a path in a cross-platform way.
19-- Use this function to avoid platform-specific directory
20-- separators in other modules. Removes trailing slashes from
21-- each component given, to avoid repeated separators.
22-- Separators inside strings are kept, to handle URLs containing
23-- protocols.
24-- @param ... strings representing directories
25-- @return string: a string with a platform-specific representation
26-- of the path.
27function dir.path(...)
28 local t = {...}
29 while t[1] == "" do
30 table.remove(t, 1)
31 end
32 for i, c in ipairs(t) do
33 t[i] = unquote(c)
34 end
35 return dir.normalize(table.concat(t, "/"))
36end
37
38--- Split protocol and path from an URL or local pathname. 16--- Split protocol and path from an URL or local pathname.
39-- URLs should be in the "protocol://path" format. 17-- URLs should be in the "protocol://path" format.
40-- For local pathnames, "file" is returned as the protocol. 18-- For local pathnames, "file" is returned as the protocol.
41-- @param url string: an URL or a local pathname. 19-- @param url string: an URL or a local pathname.
42-- @return string, string: the protocol, and the pathname without the protocol. 20-- @return string, string: the protocol, and the pathname without the protocol.
43function dir.split_url(url) 21function dir.split_url(url: string): string, string
44 assert(type(url) == "string")
45 22
46 url = unquote(url) 23 url = unquote(url)
47 local protocol, pathname = url:match("^([^:]*)://(.*)") 24 local protocol, pathname = url:match("^([^:]*)://(.*)")
@@ -58,10 +35,10 @@ end
58-- for 'file' URLs, the native system's slashes are used. 35-- for 'file' URLs, the native system's slashes are used.
59-- @param url string: an URL or a local pathname. 36-- @param url string: an URL or a local pathname.
60-- @return string: Normalized result. 37-- @return string: Normalized result.
61function dir.normalize(name) 38function dir.normalize(name: string): string
62 local protocol, pathname = dir.split_url(name) 39 local protocol, pathname = dir.split_url(name)
63 pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") 40 pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/")
64 local pieces = {} 41 local pieces: {string} = {}
65 local drive = "" 42 local drive = ""
66 if pathname:match("^.:") then 43 if pathname:match("^.:") then
67 drive, pathname = pathname:match("^(.:)(.*)$") 44 drive, pathname = pathname:match("^(.:)(.*)$")
@@ -94,5 +71,26 @@ function dir.normalize(name)
94 return pathname 71 return pathname
95end 72end
96 73
74
75--- Describe a path in a cross-platform way.
76-- Use this function to avoid platform-specific directory
77-- separators in other modules. Removes trailing slashes from
78-- each component given, to avoid repeated separators.
79-- Separators inside strings are kept, to handle URLs containing
80-- protocols.
81-- @param ... strings representing directories
82-- @return string: a string with a platform-specific representation
83-- of the path.
84function dir.path(...: string): string
85 local t = {...}
86 while t[1] == "" do
87 table.remove(t, 1)
88 end
89 for i, c in ipairs(t) do
90 t[i] = unquote(c)
91 end
92 return dir.normalize(table.concat(t, "/"))
93end
94
97return dir 95return dir
98 96