aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-06-10 02:33:37 +0200
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commitd53493179d3e16b3f2cc22470483c2926c457751 (patch)
tree532227eadab319f56687637d81cfd2d5de2a8410
parentd66bff649b44aefa7d26f464b606069a062b924f (diff)
downloadluarocks-d53493179d3e16b3f2cc22470483c2926c457751.tar.gz
luarocks-d53493179d3e16b3f2cc22470483c2926c457751.tar.bz2
luarocks-d53493179d3e16b3f2cc22470483c2926c457751.zip
core.path - converted
-rw-r--r--src/luarocks/core/path-original.lua157
-rw-r--r--src/luarocks/core/path.lua77
-rw-r--r--src/luarocks/core/path.tl166
3 files changed, 366 insertions, 34 deletions
diff --git a/src/luarocks/core/path-original.lua b/src/luarocks/core/path-original.lua
new file mode 100644
index 00000000..2f037b41
--- /dev/null
+++ b/src/luarocks/core/path-original.lua
@@ -0,0 +1,157 @@
1
2--- Core LuaRocks-specific path handling functions.
3local path = {}
4
5local cfg = require("luarocks.core.cfg")
6local dir = require("luarocks.core.dir")
7local require = nil
8
9local dir_sep = package.config:sub(1, 1)
10--------------------------------------------------------------------------------
11
12function path.rocks_dir(tree)
13 if tree == nil then
14 tree = cfg.root_dir
15 end
16 if type(tree) == "string" then
17 return dir.path(tree, cfg.rocks_subdir)
18 end
19 assert(type(tree) == "table")
20 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir)
21end
22
23--- Produce a versioned version of a filename.
24-- @param file string: filename (must start with prefix)
25-- @param prefix string: Path prefix for file
26-- @param name string: Rock name
27-- @param version string: Rock version
28-- @return string: a pathname with the same directory parts and a versioned basename.
29function path.versioned_name(file, prefix, name, version)
30 assert(type(file) == "string")
31 assert(type(name) == "string" and not name:match(dir_sep))
32 assert(type(version) == "string")
33
34 local rest = file:sub(#prefix+1):gsub("^" .. dir_sep .. "*", "")
35 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
36 return dir.path(prefix, name_version.."-"..rest)
37end
38
39--- Convert a pathname to a module identifier.
40-- In Unix, for example, a path "foo/bar/baz.lua" is converted to
41-- "foo.bar.baz"; "bla/init.lua" returns "bla.init"; "foo.so" returns "foo".
42-- @param file string: Pathname of module
43-- @return string: The module identifier, or nil if given path is
44-- not a conformant module path (the function does not check if the
45-- path actually exists).
46function path.path_to_module(file)
47 assert(type(file) == "string")
48
49 local exts = {}
50 local paths = package.path .. ";" .. package.cpath
51 for entry in paths:gmatch("[^;]+") do
52 local ext = entry:match("%.([a-z]+)$")
53 if ext then
54 exts[ext] = true
55 end
56 end
57
58 local name
59 for ext, _ in pairs(exts) do
60 name = file:match("(.*)%." .. ext .. "$")
61 if name then
62 name = name:gsub("[\\/]", ".")
63 break
64 end
65 end
66
67 if not name then name = file end
68
69 -- remove any beginning and trailing slashes-converted-to-dots
70 name = name:gsub("^%.+", ""):gsub("%.+$", "")
71
72 return name
73end
74
75function path.deploy_lua_dir(tree)
76 if type(tree) == "string" then
77 return dir.path(tree, cfg.lua_modules_path)
78 else
79 assert(type(tree) == "table")
80 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
81 end
82end
83
84function path.deploy_lib_dir(tree)
85 if type(tree) == "string" then
86 return dir.path(tree, cfg.lib_modules_path)
87 else
88 assert(type(tree) == "table")
89 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
90 end
91end
92
93local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
94
95--- Return the pathname of the file that would be loaded for a module, indexed.
96-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
97-- @param name string: name of the package (eg. "luasocket")
98-- @param version string: version number (eg. "2.0.2-1")
99-- @param tree string: repository path (eg. "/usr/local")
100-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
101-- This is done this way for use by select_module in luarocks.loader.
102-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
103function path.which_i(file_name, name, version, tree, i)
104 local deploy_dir
105 local extension = file_name:match("%.[a-z]+$")
106 if is_src_extension[extension] then
107 deploy_dir = path.deploy_lua_dir(tree)
108 file_name = dir.path(deploy_dir, file_name)
109 else
110 deploy_dir = path.deploy_lib_dir(tree)
111 file_name = dir.path(deploy_dir, file_name)
112 end
113 if i > 1 then
114 file_name = path.versioned_name(file_name, deploy_dir, name, version)
115 end
116 return file_name
117end
118
119function path.rocks_tree_to_string(tree)
120 if type(tree) == "string" then
121 return tree
122 else
123 assert(type(tree) == "table")
124 return tree.root
125 end
126end
127
128--- Apply a given function to the active rocks trees based on chosen dependency mode.
129-- @param deps_mode string: Dependency mode: "one" for the current default tree,
130-- "all" for all trees, "order" for all trees with priority >= the current default,
131-- "none" for no trees (this function becomes a nop).
132-- @param fn function: function to be applied, with the tree dir (string) as the first
133-- argument and the remaining varargs of map_trees as the following arguments.
134-- @return a table with all results of invocations of fn collected.
135function path.map_trees(deps_mode, fn, ...)
136 local result = {}
137 local current = cfg.root_dir or cfg.rocks_trees[1]
138 if deps_mode == "one" then
139 table.insert(result, (fn(current, ...)) or 0)
140 else
141 local use = false
142 if deps_mode == "all" then
143 use = true
144 end
145 for _, tree in ipairs(cfg.rocks_trees or {}) do
146 if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(current)) then
147 use = true
148 end
149 if use then
150 table.insert(result, (fn(tree, ...)) or 0)
151 end
152 end
153 end
154 return result
155end
156
157return path
diff --git a/src/luarocks/core/path.lua b/src/luarocks/core/path.lua
index 2f037b41..abfcb34d 100644
--- a/src/luarocks/core/path.lua
+++ b/src/luarocks/core/path.lua
@@ -1,13 +1,22 @@
1local _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 pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table
1 2
2--- Core LuaRocks-specific path handling functions.
3local path = {} 3local path = {}
4 4
5
6
7
8
9
10
11
12
13
5local cfg = require("luarocks.core.cfg") 14local cfg = require("luarocks.core.cfg")
6local dir = require("luarocks.core.dir") 15local dir = require("luarocks.core.dir")
7local require = nil 16local require = nil
8 17
9local dir_sep = package.config:sub(1, 1) 18local dir_sep = package.config:sub(1, 1)
10-------------------------------------------------------------------------------- 19
11 20
12function path.rocks_dir(tree) 21function path.rocks_dir(tree)
13 if tree == nil then 22 if tree == nil then
@@ -20,29 +29,29 @@ function path.rocks_dir(tree)
20 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir) 29 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir)
21end 30end
22 31
23--- Produce a versioned version of a filename. 32
24-- @param file string: filename (must start with prefix) 33
25-- @param prefix string: Path prefix for file 34
26-- @param name string: Rock name 35
27-- @param version string: Rock version 36
28-- @return string: a pathname with the same directory parts and a versioned basename. 37
29function path.versioned_name(file, prefix, name, version) 38function path.versioned_name(file, prefix, name, version)
30 assert(type(file) == "string") 39 assert(type(file) == "string")
31 assert(type(name) == "string" and not name:match(dir_sep)) 40 assert(type(name) == "string" and not name:match(dir_sep))
32 assert(type(version) == "string") 41 assert(type(version) == "string")
33 42
34 local rest = file:sub(#prefix+1):gsub("^" .. dir_sep .. "*", "") 43 local rest = file:sub(#prefix + 1):gsub("^" .. dir_sep .. "*", "")
35 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_") 44 local name_version = (name .. "_" .. version):gsub("%-", "_"):gsub("%.", "_")
36 return dir.path(prefix, name_version.."-"..rest) 45 return dir.path(prefix, name_version .. "-" .. rest)
37end 46end
38 47
39--- Convert a pathname to a module identifier. 48
40-- In Unix, for example, a path "foo/bar/baz.lua" is converted to 49
41-- "foo.bar.baz"; "bla/init.lua" returns "bla.init"; "foo.so" returns "foo". 50
42-- @param file string: Pathname of module 51
43-- @return string: The module identifier, or nil if given path is 52
44-- not a conformant module path (the function does not check if the 53
45-- path actually exists). 54
46function path.path_to_module(file) 55function path.path_to_module(file)
47 assert(type(file) == "string") 56 assert(type(file) == "string")
48 57
@@ -66,7 +75,7 @@ function path.path_to_module(file)
66 75
67 if not name then name = file end 76 if not name then name = file end
68 77
69 -- remove any beginning and trailing slashes-converted-to-dots 78
70 name = name:gsub("^%.+", ""):gsub("%.+$", "") 79 name = name:gsub("^%.+", ""):gsub("%.+$", "")
71 80
72 return name 81 return name
@@ -92,14 +101,14 @@ end
92 101
93local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true } 102local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
94 103
95--- Return the pathname of the file that would be loaded for a module, indexed. 104
96-- @param file_name string: module file name as in manifest (eg. "socket/core.so") 105
97-- @param name string: name of the package (eg. "luasocket") 106
98-- @param version string: version number (eg. "2.0.2-1") 107
99-- @param tree string: repository path (eg. "/usr/local") 108
100-- @param i number: the index, 1 if version is the current default, > 1 otherwise. 109
101-- This is done this way for use by select_module in luarocks.loader. 110
102-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") 111
103function path.which_i(file_name, name, version, tree, i) 112function path.which_i(file_name, name, version, tree, i)
104 local deploy_dir 113 local deploy_dir
105 local extension = file_name:match("%.[a-z]+$") 114 local extension = file_name:match("%.[a-z]+$")
@@ -125,13 +134,13 @@ function path.rocks_tree_to_string(tree)
125 end 134 end
126end 135end
127 136
128--- Apply a given function to the active rocks trees based on chosen dependency mode. 137
129-- @param deps_mode string: Dependency mode: "one" for the current default tree, 138
130-- "all" for all trees, "order" for all trees with priority >= the current default, 139
131-- "none" for no trees (this function becomes a nop). 140
132-- @param fn function: function to be applied, with the tree dir (string) as the first 141
133-- argument and the remaining varargs of map_trees as the following arguments. 142
134-- @return a table with all results of invocations of fn collected. 143
135function path.map_trees(deps_mode, fn, ...) 144function path.map_trees(deps_mode, fn, ...)
136 local result = {} 145 local result = {}
137 local current = cfg.root_dir or cfg.rocks_trees[1] 146 local current = cfg.root_dir or cfg.rocks_trees[1]
diff --git a/src/luarocks/core/path.tl b/src/luarocks/core/path.tl
new file mode 100644
index 00000000..7c664fda
--- /dev/null
+++ b/src/luarocks/core/path.tl
@@ -0,0 +1,166 @@
1
2--- Core LuaRocks-specific path handling functions.
3local record path
4 rocks_dir: function(tree: string | {any: any}): string --? string or table?
5 versioned_name: function(file: string, prefix: string, name: string, version: string): string
6 path_to_module: function(file: string): string
7 deploy_lua_dir: function(tree: string | {any: any}): string --? string or table?
8 deploy_lib_dir: function(tree: string | {any: any}): string --? string or table?
9 which_i: function(file_name: string, name: string, version: string, tree: string, i: number): string
10 rocks_tree_to_string: function(tree: string | {any: any}): string --? string or table?
11 map_trees: function(deps_mode: string, fn: function, ...: string): {any}
12end
13
14local cfg = require("luarocks.core.cfg") --!
15local dir = require("luarocks.core.dir")
16local require = nil --!
17
18local dir_sep = package.config:sub(1, 1)
19--------------------------------------------------------------------------------
20
21function path.rocks_dir(tree: string | {any: any}): string --? string or table?
22 if tree == nil then
23 tree = cfg.root_dir
24 end
25 if type(tree) == "string" then
26 return dir.path(tree, cfg.rocks_subdir)
27 end
28 assert(type(tree) == "table") --? needed
29 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir) --!
30end
31
32--- Produce a versioned version of a filename.
33-- @param file string: filename (must start with prefix)
34-- @param prefix string: Path prefix for file
35-- @param name string: Rock name
36-- @param version string: Rock version
37-- @return string: a pathname with the same directory parts and a versioned basename.
38function path.versioned_name(file: string, prefix: string, name: string, version: string): string
39 assert(type(file) == "string") --? needed
40 assert(type(name) == "string" and not name:match(dir_sep))
41 assert(type(version) == "string")
42
43 local rest = file:sub(#prefix+1):gsub("^" .. dir_sep .. "*", "")
44 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
45 return dir.path(prefix, name_version.."-"..rest)
46end
47
48--- Convert a pathname to a module identifier.
49-- In Unix, for example, a path "foo/bar/baz.lua" is converted to
50-- "foo.bar.baz"; "bla/init.lua" returns "bla.init"; "foo.so" returns "foo".
51-- @param file string: Pathname of module
52-- @return string: The module identifier, or nil if given path is
53-- not a conformant module path (the function does not check if the
54-- path actually exists).
55function path.path_to_module(file: string): string
56 assert(type(file) == "string")
57
58 local exts = {}
59 local paths = package.path .. ";" .. package.cpath
60 for entry in paths:gmatch("[^;]+") do
61 local ext = entry:match("%.([a-z]+)$")
62 if ext then
63 exts[ext] = true
64 end
65 end
66
67 local name: string
68 for ext, _ in pairs(exts) do
69 name = file:match("(.*)%." .. ext .. "$")
70 if name then
71 name = name:gsub("[\\/]", ".")
72 break
73 end
74 end
75
76 if not name then name = file end
77
78 -- remove any beginning and trailing slashes-converted-to-dots
79 name = name:gsub("^%.+", ""):gsub("%.+$", "")
80
81 return name
82end
83
84function path.deploy_lua_dir(tree: string | {any: any}): string --? string or table?
85 if type(tree) == "string" then
86 return dir.path(tree, cfg.lua_modules_path)
87 else
88 assert(type(tree) == "table")
89 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
90 end
91end
92
93function path.deploy_lib_dir(tree: string | {any: any}): string --? string or table?
94 if type(tree) == "string" then
95 return dir.path(tree, cfg.lib_modules_path)
96 else
97 assert(type(tree) == "table")
98 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
99 end
100end
101
102local is_src_extension: {string: boolean} = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
103
104--- Return the pathname of the file that would be loaded for a module, indexed.
105-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
106-- @param name string: name of the package (eg. "luasocket")
107-- @param version string: version number (eg. "2.0.2-1")
108-- @param tree string: repository path (eg. "/usr/local")
109-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
110-- This is done this way for use by select_module in luarocks.loader.
111-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
112function path.which_i(file_name: string, name: string, version: string, tree: string, i: number): string
113 local deploy_dir: string
114 local extension = file_name:match("%.[a-z]+$")
115 if is_src_extension[extension] then
116 deploy_dir = path.deploy_lua_dir(tree)
117 file_name = dir.path(deploy_dir, file_name)
118 else
119 deploy_dir = path.deploy_lib_dir(tree)
120 file_name = dir.path(deploy_dir, file_name)
121 end
122 if i > 1 then
123 file_name = path.versioned_name(file_name, deploy_dir, name, version)
124 end
125 return file_name
126end
127
128function path.rocks_tree_to_string(tree: string | {any: any}): string --? string or table?
129 if type(tree) == "string" then
130 return tree
131 else
132 assert(type(tree) == "table")
133 return tree.root
134 end
135end
136
137--- Apply a given function to the active rocks trees based on chosen dependency mode.
138-- @param deps_mode string: Dependency mode: "one" for the current default tree,
139-- "all" for all trees, "order" for all trees with priority >= the current default,
140-- "none" for no trees (this function becomes a nop).
141-- @param fn function: function to be applied, with the tree dir (string) as the first
142-- argument and the remaining varargs of map_trees as the following arguments.
143-- @return a table with all results of invocations of fn collected.
144function path.map_trees(deps_mode: string, fn: function, ...: string): {any} --? ... string
145 local result = {}
146 local current = cfg.root_dir or cfg.rocks_trees[1]
147 if deps_mode == "one" then
148 table.insert(result, (fn(current, ...)) or 0)
149 else
150 local use = false
151 if deps_mode == "all" then
152 use = true
153 end
154 for _, tree in ipairs(cfg.rocks_trees or {}) do --!
155 if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(current)) then --!
156 use = true
157 end
158 if use then
159 table.insert(result, (fn(tree, ...)) or 0)
160 end
161 end
162 end
163 return result
164end
165
166return path