diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-06-09 18:53:57 +0200 |
---|---|---|
committer | V1K1NGbg <victor@ilchev.com> | 2024-08-05 20:49:17 +0300 |
commit | 413d1ef8a74c42ead47f3740835e0f0d0203eb7f (patch) | |
tree | 36511e55d5e2f2bc0d15d5c3ef0b73314a0b0007 | |
parent | 2377e668c424f77f90a577f42608eccfdfaca07b (diff) | |
download | luarocks-413d1ef8a74c42ead47f3740835e0f0d0203eb7f.tar.gz luarocks-413d1ef8a74c42ead47f3740835e0f0d0203eb7f.tar.bz2 luarocks-413d1ef8a74c42ead47f3740835e0f0d0203eb7f.zip |
core.dir - converted
-rw-r--r-- | src/luarocks/core/dir-origianl.lua | 98 | ||||
-rw-r--r-- | src/luarocks/core/dir.lua | 51 | ||||
-rw-r--r-- | src/luarocks/core/dir.tl | 98 |
3 files changed, 221 insertions, 26 deletions
diff --git a/src/luarocks/core/dir-origianl.lua b/src/luarocks/core/dir-origianl.lua new file mode 100644 index 00000000..5d6f2c9f --- /dev/null +++ b/src/luarocks/core/dir-origianl.lua | |||
@@ -0,0 +1,98 @@ | |||
1 | |||
2 | local dir = {} | ||
3 | |||
4 | local require = nil | ||
5 | -------------------------------------------------------------------------------- | ||
6 | |||
7 | local dir_sep = package.config:sub(1, 1) | ||
8 | |||
9 | local function unquote(c) | ||
10 | local first, last = c:sub(1,1), c:sub(-1) | ||
11 | if (first == '"' and last == '"') or | ||
12 | (first == "'" and last == "'") then | ||
13 | return c:sub(2,-2) | ||
14 | end | ||
15 | return c | ||
16 | end | ||
17 | |||
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. | ||
27 | function 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, "/")) | ||
36 | end | ||
37 | |||
38 | --- Split protocol and path from an URL or local pathname. | ||
39 | -- URLs should be in the "protocol://path" format. | ||
40 | -- For local pathnames, "file" is returned as the protocol. | ||
41 | -- @param url string: an URL or a local pathname. | ||
42 | -- @return string, string: the protocol, and the pathname without the protocol. | ||
43 | function dir.split_url(url) | ||
44 | assert(type(url) == "string") | ||
45 | |||
46 | url = unquote(url) | ||
47 | local protocol, pathname = url:match("^([^:]*)://(.*)") | ||
48 | if not protocol then | ||
49 | protocol = "file" | ||
50 | pathname = url | ||
51 | end | ||
52 | return protocol, pathname | ||
53 | end | ||
54 | |||
55 | --- Normalize a url or local path. | ||
56 | -- URLs should be in the "protocol://path" format. | ||
57 | -- Removes trailing and double slashes, and '.' and '..' components. | ||
58 | -- for 'file' URLs, the native system's slashes are used. | ||
59 | -- @param url string: an URL or a local pathname. | ||
60 | -- @return string: Normalized result. | ||
61 | function dir.normalize(name) | ||
62 | local protocol, pathname = dir.split_url(name) | ||
63 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | ||
64 | local pieces = {} | ||
65 | local drive = "" | ||
66 | if pathname:match("^.:") then | ||
67 | drive, pathname = pathname:match("^(.:)(.*)$") | ||
68 | end | ||
69 | pathname = pathname .. "/" | ||
70 | for piece in pathname:gmatch("(.-)/") do | ||
71 | if piece == ".." then | ||
72 | local prev = pieces[#pieces] | ||
73 | if not prev or prev == ".." then | ||
74 | table.insert(pieces, "..") | ||
75 | elseif prev ~= "" then | ||
76 | table.remove(pieces) | ||
77 | end | ||
78 | elseif piece ~= "." then | ||
79 | table.insert(pieces, piece) | ||
80 | end | ||
81 | end | ||
82 | if #pieces == 0 then | ||
83 | pathname = drive .. "." | ||
84 | elseif #pieces == 1 and pieces[1] == "" then | ||
85 | pathname = drive .. "/" | ||
86 | else | ||
87 | pathname = drive .. table.concat(pieces, "/") | ||
88 | end | ||
89 | if protocol ~= "file" then | ||
90 | pathname = protocol .. "://" .. pathname | ||
91 | else | ||
92 | pathname = pathname:gsub("/", dir_sep) | ||
93 | end | ||
94 | return pathname | ||
95 | end | ||
96 | |||
97 | return dir | ||
98 | |||
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua index 5d6f2c9f..ce73363b 100644 --- a/src/luarocks/core/dir.lua +++ b/src/luarocks/core/dir.lua | |||
@@ -1,31 +1,31 @@ | |||
1 | 1 | local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local package = _tl_compat and _tl_compat.package or package; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table | |
2 | local dir = {} | 2 | local dir = {} |
3 | 3 | ||
4 | local require = nil | 4 | local require = nil |
5 | -------------------------------------------------------------------------------- | 5 | |
6 | 6 | ||
7 | local dir_sep = package.config:sub(1, 1) | 7 | local dir_sep = package.config:sub(1, 1) |
8 | 8 | ||
9 | local function unquote(c) | 9 | local function unquote(c) |
10 | local first, last = c:sub(1,1), c:sub(-1) | 10 | local first, last = c:sub(1, 1), c:sub(-1) |
11 | if (first == '"' and last == '"') or | 11 | if (first == '"' and last == '"') or |
12 | (first == "'" and last == "'") then | 12 | (first == "'" and last == "'") then |
13 | return c:sub(2,-2) | 13 | return c:sub(2, -2) |
14 | end | 14 | end |
15 | return c | 15 | return c |
16 | end | 16 | end |
17 | 17 | ||
18 | --- Describe a path in a cross-platform way. | 18 | |
19 | -- Use this function to avoid platform-specific directory | 19 | |
20 | -- separators in other modules. Removes trailing slashes from | 20 | |
21 | -- each component given, to avoid repeated separators. | 21 | |
22 | -- Separators inside strings are kept, to handle URLs containing | 22 | |
23 | -- protocols. | 23 | |
24 | -- @param ... strings representing directories | 24 | |
25 | -- @return string: a string with a platform-specific representation | 25 | |
26 | -- of the path. | 26 | |
27 | function dir.path(...) | 27 | function dir.path(...) |
28 | local t = {...} | 28 | local t = { ... } |
29 | while t[1] == "" do | 29 | while t[1] == "" do |
30 | table.remove(t, 1) | 30 | table.remove(t, 1) |
31 | end | 31 | end |
@@ -35,11 +35,11 @@ function dir.path(...) | |||
35 | return dir.normalize(table.concat(t, "/")) | 35 | return dir.normalize(table.concat(t, "/")) |
36 | end | 36 | end |
37 | 37 | ||
38 | --- Split protocol and path from an URL or local pathname. | 38 | |
39 | -- URLs should be in the "protocol://path" format. | 39 | |
40 | -- For local pathnames, "file" is returned as the protocol. | 40 | |
41 | -- @param url string: an URL or a local pathname. | 41 | |
42 | -- @return string, string: the protocol, and the pathname without the protocol. | 42 | |
43 | function dir.split_url(url) | 43 | function dir.split_url(url) |
44 | assert(type(url) == "string") | 44 | assert(type(url) == "string") |
45 | 45 | ||
@@ -52,12 +52,12 @@ function dir.split_url(url) | |||
52 | return protocol, pathname | 52 | return protocol, pathname |
53 | end | 53 | end |
54 | 54 | ||
55 | --- Normalize a url or local path. | 55 | |
56 | -- URLs should be in the "protocol://path" format. | 56 | |
57 | -- Removes trailing and double slashes, and '.' and '..' components. | 57 | |
58 | -- for 'file' URLs, the native system's slashes are used. | 58 | |
59 | -- @param url string: an URL or a local pathname. | 59 | |
60 | -- @return string: Normalized result. | 60 | |
61 | function dir.normalize(name) | 61 | function dir.normalize(name) |
62 | local protocol, pathname = dir.split_url(name) | 62 | local protocol, pathname = dir.split_url(name) |
63 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | 63 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") |
@@ -95,4 +95,3 @@ function dir.normalize(name) | |||
95 | end | 95 | end |
96 | 96 | ||
97 | return dir | 97 | return dir |
98 | |||
diff --git a/src/luarocks/core/dir.tl b/src/luarocks/core/dir.tl new file mode 100644 index 00000000..55604e64 --- /dev/null +++ b/src/luarocks/core/dir.tl | |||
@@ -0,0 +1,98 @@ | |||
1 | |||
2 | local dir = {} --! | ||
3 | |||
4 | local require = nil --! | ||
5 | -------------------------------------------------------------------------------- | ||
6 | |||
7 | local dir_sep = package.config:sub(1, 1) --! | ||
8 | |||
9 | local function unquote(c: string): string | ||
10 | local first, last = c:sub(1,1), c:sub(-1) | ||
11 | if (first == '"' and last == '"') or | ||
12 | (first == "'" and last == "'") then | ||
13 | return c:sub(2,-2) | ||
14 | end | ||
15 | return c | ||
16 | end | ||
17 | |||
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. | ||
27 | function dir.path(...: string): string | ||
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, "/")) --! | ||
36 | end | ||
37 | |||
38 | --- Split protocol and path from an URL or local pathname. | ||
39 | -- URLs should be in the "protocol://path" format. | ||
40 | -- For local pathnames, "file" is returned as the protocol. | ||
41 | -- @param url string: an URL or a local pathname. | ||
42 | -- @return string, string: the protocol, and the pathname without the protocol. | ||
43 | function dir.split_url(url: string): string, string | ||
44 | assert(type(url) == "string") | ||
45 | |||
46 | url = unquote(url) | ||
47 | local protocol, pathname = url:match("^([^:]*)://(.*)") | ||
48 | if not protocol then | ||
49 | protocol = "file" | ||
50 | pathname = url | ||
51 | end | ||
52 | return protocol, pathname | ||
53 | end | ||
54 | |||
55 | --- Normalize a url or local path. | ||
56 | -- URLs should be in the "protocol://path" format. | ||
57 | -- Removes trailing and double slashes, and '.' and '..' components. | ||
58 | -- for 'file' URLs, the native system's slashes are used. | ||
59 | -- @param url string: an URL or a local pathname. | ||
60 | -- @return string: Normalized result. | ||
61 | function dir.normalize(name: string): string | ||
62 | local protocol, pathname = dir.split_url(name) | ||
63 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | ||
64 | local pieces: {string} = {} | ||
65 | local drive = "" | ||
66 | if pathname:match("^.:") then | ||
67 | drive, pathname = pathname:match("^(.:)(.*)$") | ||
68 | end | ||
69 | pathname = pathname .. "/" | ||
70 | for piece in pathname:gmatch("(.-)/") do | ||
71 | if piece == ".." then | ||
72 | local prev = pieces[#pieces] | ||
73 | if not prev or prev == ".." then | ||
74 | table.insert(pieces, "..") | ||
75 | elseif prev ~= "" then | ||
76 | table.remove(pieces) | ||
77 | end | ||
78 | elseif piece ~= "." then | ||
79 | table.insert(pieces, piece) | ||
80 | end | ||
81 | end | ||
82 | if #pieces == 0 then | ||
83 | pathname = drive .. "." | ||
84 | elseif #pieces == 1 and pieces[1] == "" then | ||
85 | pathname = drive .. "/" | ||
86 | else | ||
87 | pathname = drive .. table.concat(pieces, "/") | ||
88 | end | ||
89 | if protocol ~= "file" then | ||
90 | pathname = protocol .. "://" .. pathname | ||
91 | else | ||
92 | pathname = pathname:gsub("/", dir_sep) | ||
93 | end | ||
94 | return pathname | ||
95 | end | ||
96 | |||
97 | return dir | ||
98 | |||