diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-07-22 21:37:54 +0300 |
---|---|---|
committer | V1K1NGbg <victor@ilchev.com> | 2024-08-05 20:49:17 +0300 |
commit | 43c5a899d41533af4c107004643b139ce0349d12 (patch) | |
tree | be089508c3a8f6f0f73a007351476a5e2b61665d | |
parent | 74960c88576c9e125d898ebb949e7bfada776c66 (diff) | |
download | luarocks-43c5a899d41533af4c107004643b139ce0349d12.tar.gz luarocks-43c5a899d41533af4c107004643b139ce0349d12.tar.bz2 luarocks-43c5a899d41533af4c107004643b139ce0349d12.zip |
fix attempt 2
-rw-r--r-- | src/luarocks/path-incomplete.lua | 254 | ||||
-rw-r--r-- | src/luarocks/path-origianl.lua | 263 | ||||
-rw-r--r-- | src/luarocks/path.lua | 269 |
3 files changed, 393 insertions, 393 deletions
diff --git a/src/luarocks/path-incomplete.lua b/src/luarocks/path-incomplete.lua new file mode 100644 index 00000000..65c1a7d2 --- /dev/null +++ b/src/luarocks/path-incomplete.lua | |||
@@ -0,0 +1,254 @@ | |||
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 io = _tl_compat and _tl_compat.io or io; 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 | |||
3 | |||
4 | |||
5 | |||
6 | local cfg = require("luarocks.core.cfg") | ||
7 | local core = require("luarocks.core.path") | ||
8 | local dir = require("luarocks.dir") | ||
9 | local util = require("luarocks.core.util") | ||
10 | |||
11 | |||
12 | |||
13 | local path = {} | ||
14 | |||
15 | |||
16 | |||
17 | |||
18 | |||
19 | |||
20 | |||
21 | |||
22 | |||
23 | path.rocks_dir = core.rocks_dir | ||
24 | path.versioned_name = core.versioned_name | ||
25 | path.path_to_module = core.path_to_module | ||
26 | path.deploy_lua_dir = core.deploy_lua_dir | ||
27 | path.deploy_lib_dir = core.deploy_lib_dir | ||
28 | path.map_trees = core.map_trees | ||
29 | path.rocks_tree_to_string = core.rocks_tree_to_string | ||
30 | |||
31 | function path.root_dir(tree) | ||
32 | if type(tree) == "string" then | ||
33 | return tree | ||
34 | else | ||
35 | return tree.root | ||
36 | end | ||
37 | end | ||
38 | |||
39 | |||
40 | |||
41 | |||
42 | function path.rockspec_name_from_rock(rock_name) | ||
43 | local base_name = dir.base_name(rock_name) | ||
44 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | ||
45 | end | ||
46 | |||
47 | function path.root_from_rocks_dir(rocks_dir) | ||
48 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") | ||
49 | end | ||
50 | |||
51 | function path.deploy_bin_dir(tree) | ||
52 | return dir.path(path.root_dir(tree), "bin") | ||
53 | end | ||
54 | |||
55 | function path.manifest_file(tree) | ||
56 | return dir.path(path.rocks_dir(tree), "manifest") | ||
57 | end | ||
58 | |||
59 | |||
60 | |||
61 | |||
62 | |||
63 | |||
64 | function path.versions_dir(name, tree) | ||
65 | assert(not name:match("/")) | ||
66 | return dir.path(path.rocks_dir(tree), name) | ||
67 | end | ||
68 | |||
69 | |||
70 | |||
71 | |||
72 | |||
73 | |||
74 | |||
75 | function path.install_dir(name, version, tree) | ||
76 | assert(not name:match("/")) | ||
77 | return dir.path(path.rocks_dir(tree), name, version) | ||
78 | end | ||
79 | |||
80 | |||
81 | |||
82 | |||
83 | |||
84 | |||
85 | |||
86 | function path.rockspec_file(name, version, tree) | ||
87 | assert(not name:match("/")) | ||
88 | return dir.path(path.rocks_dir(tree), name, version, name .. "-" .. version .. ".rockspec") | ||
89 | end | ||
90 | |||
91 | |||
92 | |||
93 | |||
94 | |||
95 | |||
96 | |||
97 | function path.rock_manifest_file(name, version, tree) | ||
98 | assert(not name:match("/")) | ||
99 | return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") | ||
100 | end | ||
101 | |||
102 | |||
103 | |||
104 | |||
105 | |||
106 | |||
107 | |||
108 | function path.rock_namespace_file(name, version, tree) | ||
109 | assert(not name:match("/")) | ||
110 | return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") | ||
111 | end | ||
112 | |||
113 | |||
114 | |||
115 | |||
116 | |||
117 | |||
118 | |||
119 | function path.lib_dir(name, version, tree) | ||
120 | assert(not name:match("/")) | ||
121 | return dir.path(path.rocks_dir(tree), name, version, "lib") | ||
122 | end | ||
123 | |||
124 | |||
125 | |||
126 | |||
127 | |||
128 | |||
129 | |||
130 | function path.lua_dir(name, version, tree) | ||
131 | assert(not name:match("/")) | ||
132 | return dir.path(path.rocks_dir(tree), name, version, "lua") | ||
133 | end | ||
134 | |||
135 | |||
136 | |||
137 | |||
138 | |||
139 | |||
140 | |||
141 | function path.doc_dir(name, version, tree) | ||
142 | assert(not name:match("/")) | ||
143 | return dir.path(path.rocks_dir(tree), name, version, "doc") | ||
144 | end | ||
145 | |||
146 | |||
147 | |||
148 | |||
149 | |||
150 | |||
151 | |||
152 | function path.conf_dir(name, version, tree) | ||
153 | assert(not name:match("/")) | ||
154 | return dir.path(path.rocks_dir(tree), name, version, "conf") | ||
155 | end | ||
156 | |||
157 | |||
158 | |||
159 | |||
160 | |||
161 | |||
162 | |||
163 | |||
164 | function path.bin_dir(name, version, tree) | ||
165 | assert(not name:match("/")) | ||
166 | return dir.path(path.rocks_dir(tree), name, version, "bin") | ||
167 | end | ||
168 | |||
169 | |||
170 | |||
171 | |||
172 | |||
173 | |||
174 | function path.parse_name(file_name) | ||
175 | if file_name:match("%.rock$") then | ||
176 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") | ||
177 | else | ||
178 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") | ||
179 | end | ||
180 | end | ||
181 | |||
182 | |||
183 | |||
184 | |||
185 | |||
186 | |||
187 | |||
188 | function path.make_url(pathname, name, version, arch) | ||
189 | assert(not name:match("/")) | ||
190 | local filename = name .. "-" .. version | ||
191 | if arch == "installed" then | ||
192 | filename = dir.path(name, version, filename .. ".rockspec") | ||
193 | elseif arch == "rockspec" then | ||
194 | filename = filename .. ".rockspec" | ||
195 | else | ||
196 | filename = filename .. "." .. arch .. ".rock" | ||
197 | end | ||
198 | return dir.path(pathname, filename) | ||
199 | end | ||
200 | |||
201 | |||
202 | |||
203 | |||
204 | |||
205 | function path.module_to_path(mod) | ||
206 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) | ||
207 | end | ||
208 | |||
209 | function path.use_tree(tree) | ||
210 | cfg.root_dir = tree | ||
211 | cfg.rocks_dir = path.rocks_dir(tree) | ||
212 | cfg.deploy_bin_dir = path.deploy_bin_dir(tree) | ||
213 | cfg.deploy_lua_dir = path.deploy_lua_dir(tree) | ||
214 | cfg.deploy_lib_dir = path.deploy_lib_dir(tree) | ||
215 | end | ||
216 | |||
217 | function path.add_to_package_paths(tree) | ||
218 | package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" .. | ||
219 | dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" .. | ||
220 | package.path | ||
221 | package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" .. | ||
222 | package.cpath | ||
223 | end | ||
224 | |||
225 | |||
226 | |||
227 | |||
228 | |||
229 | |||
230 | function path.read_namespace(name, version, tree) | ||
231 | assert(not name:match("/")) | ||
232 | |||
233 | local namespace | ||
234 | local fd = io.open(path.rock_namespace_file(name, version, tree), "r") | ||
235 | if fd then | ||
236 | namespace = fd:read("*a") | ||
237 | fd:close() | ||
238 | end | ||
239 | return namespace | ||
240 | end | ||
241 | |||
242 | function path.package_paths(deps_mode) | ||
243 | local lpaths = {} | ||
244 | local lcpaths = {} | ||
245 | path.map_trees(deps_mode, function(tree) | ||
246 | local root = path.root_dir(tree) | ||
247 | table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) | ||
248 | table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) | ||
249 | table.insert(lcpaths, dir.path(root, cfg.lib_modules_path, "?." .. cfg.lib_extension)) | ||
250 | end) | ||
251 | return table.concat(lpaths, ";"), table.concat(lcpaths, ";") | ||
252 | end | ||
253 | |||
254 | return path | ||
diff --git a/src/luarocks/path-origianl.lua b/src/luarocks/path-origianl.lua deleted file mode 100644 index 19657c83..00000000 --- a/src/luarocks/path-origianl.lua +++ /dev/null | |||
@@ -1,263 +0,0 @@ | |||
1 | |||
2 | --- LuaRocks-specific path handling functions. | ||
3 | -- All paths are configured in this module, making it a single | ||
4 | -- point where the layout of the local installation is defined in LuaRocks. | ||
5 | local path = {} | ||
6 | |||
7 | local core = require("luarocks.core.path") | ||
8 | local dir = require("luarocks.dir") | ||
9 | local cfg = require("luarocks.core.cfg") | ||
10 | local util = require("luarocks.util") | ||
11 | |||
12 | path.rocks_dir = core.rocks_dir | ||
13 | path.versioned_name = core.versioned_name | ||
14 | path.path_to_module = core.path_to_module | ||
15 | path.deploy_lua_dir = core.deploy_lua_dir | ||
16 | path.deploy_lib_dir = core.deploy_lib_dir | ||
17 | path.map_trees = core.map_trees | ||
18 | path.rocks_tree_to_string = core.rocks_tree_to_string | ||
19 | |||
20 | --- Infer rockspec filename from a rock filename. | ||
21 | -- @param rock_name string: Pathname of a rock file. | ||
22 | -- @return string: Filename of the rockspec, without path. | ||
23 | function path.rockspec_name_from_rock(rock_name) | ||
24 | assert(type(rock_name) == "string") | ||
25 | local base_name = dir.base_name(rock_name) | ||
26 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | ||
27 | end | ||
28 | |||
29 | function path.root_from_rocks_dir(rocks_dir) | ||
30 | assert(type(rocks_dir) == "string") | ||
31 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") | ||
32 | end | ||
33 | |||
34 | function path.root_dir(tree) | ||
35 | if type(tree) == "string" then | ||
36 | return tree | ||
37 | else | ||
38 | assert(type(tree) == "table") | ||
39 | return tree.root | ||
40 | end | ||
41 | end | ||
42 | |||
43 | function path.deploy_bin_dir(tree) | ||
44 | return dir.path(path.root_dir(tree), "bin") | ||
45 | end | ||
46 | |||
47 | function path.manifest_file(tree) | ||
48 | return dir.path(path.rocks_dir(tree), "manifest") | ||
49 | end | ||
50 | |||
51 | --- Get the directory for all versions of a package in a tree. | ||
52 | -- @param name string: The package name. | ||
53 | -- @return string: The resulting path -- does not guarantee that | ||
54 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
55 | -- the package (and by extension, the path) exists. | ||
56 | function path.versions_dir(name, tree) | ||
57 | assert(type(name) == "string" and not name:match("/")) | ||
58 | return dir.path(path.rocks_dir(tree), name) | ||
59 | end | ||
60 | |||
61 | --- Get the local installation directory (prefix) for a package. | ||
62 | -- @param name string: The package name. | ||
63 | -- @param version string: The package version. | ||
64 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
65 | -- @return string: The resulting path -- does not guarantee that | ||
66 | -- the package (and by extension, the path) exists. | ||
67 | function path.install_dir(name, version, tree) | ||
68 | assert(type(name) == "string" and not name:match("/")) | ||
69 | assert(type(version) == "string") | ||
70 | return dir.path(path.rocks_dir(tree), name, version) | ||
71 | end | ||
72 | |||
73 | --- Get the local filename of the rockspec of an installed rock. | ||
74 | -- @param name string: The package name. | ||
75 | -- @param version string: The package version. | ||
76 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
77 | -- @return string: The resulting path -- does not guarantee that | ||
78 | -- the package (and by extension, the file) exists. | ||
79 | function path.rockspec_file(name, version, tree) | ||
80 | assert(type(name) == "string" and not name:match("/")) | ||
81 | assert(type(version) == "string") | ||
82 | return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") | ||
83 | end | ||
84 | |||
85 | --- Get the local filename of the rock_manifest file of an installed rock. | ||
86 | -- @param name string: The package name. | ||
87 | -- @param version string: The package version. | ||
88 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
89 | -- @return string: The resulting path -- does not guarantee that | ||
90 | -- the package (and by extension, the file) exists. | ||
91 | function path.rock_manifest_file(name, version, tree) | ||
92 | assert(type(name) == "string" and not name:match("/")) | ||
93 | assert(type(version) == "string") | ||
94 | return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") | ||
95 | end | ||
96 | |||
97 | --- Get the local filename of the rock_namespace file of an installed rock. | ||
98 | -- @param name string: The package name (without a namespace). | ||
99 | -- @param version string: The package version. | ||
100 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
101 | -- @return string: The resulting path -- does not guarantee that | ||
102 | -- the package (and by extension, the file) exists. | ||
103 | function path.rock_namespace_file(name, version, tree) | ||
104 | assert(type(name) == "string" and not name:match("/")) | ||
105 | assert(type(version) == "string") | ||
106 | return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") | ||
107 | end | ||
108 | |||
109 | --- Get the local installation directory for C libraries of a package. | ||
110 | -- @param name string: The package name. | ||
111 | -- @param version string: The package version. | ||
112 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
113 | -- @return string: The resulting path -- does not guarantee that | ||
114 | -- the package (and by extension, the path) exists. | ||
115 | function path.lib_dir(name, version, tree) | ||
116 | assert(type(name) == "string" and not name:match("/")) | ||
117 | assert(type(version) == "string") | ||
118 | return dir.path(path.rocks_dir(tree), name, version, "lib") | ||
119 | end | ||
120 | |||
121 | --- Get the local installation directory for Lua modules of a package. | ||
122 | -- @param name string: The package name. | ||
123 | -- @param version string: The package version. | ||
124 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
125 | -- @return string: The resulting path -- does not guarantee that | ||
126 | -- the package (and by extension, the path) exists. | ||
127 | function path.lua_dir(name, version, tree) | ||
128 | assert(type(name) == "string" and not name:match("/")) | ||
129 | assert(type(version) == "string") | ||
130 | return dir.path(path.rocks_dir(tree), name, version, "lua") | ||
131 | end | ||
132 | |||
133 | --- Get the local installation directory for documentation of a package. | ||
134 | -- @param name string: The package name. | ||
135 | -- @param version string: The package version. | ||
136 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
137 | -- @return string: The resulting path -- does not guarantee that | ||
138 | -- the package (and by extension, the path) exists. | ||
139 | function path.doc_dir(name, version, tree) | ||
140 | assert(type(name) == "string" and not name:match("/")) | ||
141 | assert(type(version) == "string") | ||
142 | return dir.path(path.rocks_dir(tree), name, version, "doc") | ||
143 | end | ||
144 | |||
145 | --- Get the local installation directory for configuration files of a package. | ||
146 | -- @param name string: The package name. | ||
147 | -- @param version string: The package version. | ||
148 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
149 | -- @return string: The resulting path -- does not guarantee that | ||
150 | -- the package (and by extension, the path) exists. | ||
151 | function path.conf_dir(name, version, tree) | ||
152 | assert(type(name) == "string" and not name:match("/")) | ||
153 | assert(type(version) == "string") | ||
154 | return dir.path(path.rocks_dir(tree), name, version, "conf") | ||
155 | end | ||
156 | |||
157 | --- Get the local installation directory for command-line scripts | ||
158 | -- of a package. | ||
159 | -- @param name string: The package name. | ||
160 | -- @param version string: The package version. | ||
161 | -- @param tree string or nil: If given, specifies the local tree to use. | ||
162 | -- @return string: The resulting path -- does not guarantee that | ||
163 | -- the package (and by extension, the path) exists. | ||
164 | function path.bin_dir(name, version, tree) | ||
165 | assert(type(name) == "string" and not name:match("/")) | ||
166 | assert(type(version) == "string") | ||
167 | return dir.path(path.rocks_dir(tree), name, version, "bin") | ||
168 | end | ||
169 | |||
170 | --- Extract name, version and arch of a rock filename, | ||
171 | -- or name, version and "rockspec" from a rockspec name. | ||
172 | -- @param file_name string: pathname of a rock or rockspec | ||
173 | -- @return (string, string, string) or nil: name, version and arch | ||
174 | -- or nil if name could not be parsed | ||
175 | function path.parse_name(file_name) | ||
176 | assert(type(file_name) == "string") | ||
177 | if file_name:match("%.rock$") then | ||
178 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") | ||
179 | else | ||
180 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") | ||
181 | end | ||
182 | end | ||
183 | |||
184 | --- Make a rockspec or rock URL. | ||
185 | -- @param pathname string: Base URL or pathname. | ||
186 | -- @param name string: Package name. | ||
187 | -- @param version string: Package version. | ||
188 | -- @param arch string: Architecture identifier, or "rockspec" or "installed". | ||
189 | -- @return string: A URL or pathname following LuaRocks naming conventions. | ||
190 | function path.make_url(pathname, name, version, arch) | ||
191 | assert(type(pathname) == "string") | ||
192 | assert(type(name) == "string" and not name:match("/")) | ||
193 | assert(type(version) == "string") | ||
194 | assert(type(arch) == "string") | ||
195 | |||
196 | local filename = name.."-"..version | ||
197 | if arch == "installed" then | ||
198 | filename = dir.path(name, version, filename..".rockspec") | ||
199 | elseif arch == "rockspec" then | ||
200 | filename = filename..".rockspec" | ||
201 | else | ||
202 | filename = filename.."."..arch..".rock" | ||
203 | end | ||
204 | return dir.path(pathname, filename) | ||
205 | end | ||
206 | |||
207 | --- Obtain the directory name where a module should be stored. | ||
208 | -- For example, on Unix, "foo.bar.baz" will return "foo/bar". | ||
209 | -- @param mod string: A module name in Lua dot-separated format. | ||
210 | -- @return string: A directory name using the platform's separator. | ||
211 | function path.module_to_path(mod) | ||
212 | assert(type(mod) == "string") | ||
213 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) | ||
214 | end | ||
215 | |||
216 | function path.use_tree(tree) | ||
217 | cfg.root_dir = tree | ||
218 | cfg.rocks_dir = path.rocks_dir(tree) | ||
219 | cfg.deploy_bin_dir = path.deploy_bin_dir(tree) | ||
220 | cfg.deploy_lua_dir = path.deploy_lua_dir(tree) | ||
221 | cfg.deploy_lib_dir = path.deploy_lib_dir(tree) | ||
222 | end | ||
223 | |||
224 | function path.add_to_package_paths(tree) | ||
225 | package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" | ||
226 | .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" | ||
227 | .. package.path | ||
228 | package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" | ||
229 | .. package.cpath | ||
230 | end | ||
231 | |||
232 | --- Get the namespace of a locally-installed rock, if any. | ||
233 | -- @param name string: The rock name, without a namespace. | ||
234 | -- @param version string: The rock version. | ||
235 | -- @param tree string: The local tree to use. | ||
236 | -- @return string?: The namespace if it exists, or nil. | ||
237 | function path.read_namespace(name, version, tree) | ||
238 | assert(type(name) == "string" and not name:match("/")) | ||
239 | assert(type(version) == "string") | ||
240 | assert(type(tree) == "string") | ||
241 | |||
242 | local namespace | ||
243 | local fd = io.open(path.rock_namespace_file(name, version, tree), "r") | ||
244 | if fd then | ||
245 | namespace = fd:read("*a") | ||
246 | fd:close() | ||
247 | end | ||
248 | return namespace | ||
249 | end | ||
250 | |||
251 | function path.package_paths(deps_mode) | ||
252 | local lpaths = {} | ||
253 | local lcpaths = {} | ||
254 | path.map_trees(deps_mode, function(tree) | ||
255 | local root = path.root_dir(tree) | ||
256 | table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) | ||
257 | table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) | ||
258 | table.insert(lcpaths, dir.path(root, cfg.lib_modules_path, "?." .. cfg.lib_extension)) | ||
259 | end) | ||
260 | return table.concat(lpaths, ";"), table.concat(lcpaths, ";") | ||
261 | end | ||
262 | |||
263 | return path | ||
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index 65c1a7d2..19657c83 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua | |||
@@ -1,24 +1,13 @@ | |||
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 io = _tl_compat and _tl_compat.io or io; 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 | |||
3 | |||
4 | 1 | ||
2 | --- LuaRocks-specific path handling functions. | ||
3 | -- All paths are configured in this module, making it a single | ||
4 | -- point where the layout of the local installation is defined in LuaRocks. | ||
5 | local path = {} | ||
5 | 6 | ||
6 | local cfg = require("luarocks.core.cfg") | ||
7 | local core = require("luarocks.core.path") | 7 | local core = require("luarocks.core.path") |
8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local util = require("luarocks.core.util") | 9 | local cfg = require("luarocks.core.cfg") |
10 | 10 | local util = require("luarocks.util") | |
11 | |||
12 | |||
13 | local path = {} | ||
14 | |||
15 | |||
16 | |||
17 | |||
18 | |||
19 | |||
20 | |||
21 | |||
22 | 11 | ||
23 | path.rocks_dir = core.rocks_dir | 12 | path.rocks_dir = core.rocks_dir |
24 | path.versioned_name = core.versioned_name | 13 | path.versioned_name = core.versioned_name |
@@ -28,26 +17,29 @@ path.deploy_lib_dir = core.deploy_lib_dir | |||
28 | path.map_trees = core.map_trees | 17 | path.map_trees = core.map_trees |
29 | path.rocks_tree_to_string = core.rocks_tree_to_string | 18 | path.rocks_tree_to_string = core.rocks_tree_to_string |
30 | 19 | ||
31 | function path.root_dir(tree) | 20 | --- Infer rockspec filename from a rock filename. |
32 | if type(tree) == "string" then | 21 | -- @param rock_name string: Pathname of a rock file. |
33 | return tree | 22 | -- @return string: Filename of the rockspec, without path. |
34 | else | ||
35 | return tree.root | ||
36 | end | ||
37 | end | ||
38 | |||
39 | |||
40 | |||
41 | |||
42 | function path.rockspec_name_from_rock(rock_name) | 23 | function path.rockspec_name_from_rock(rock_name) |
24 | assert(type(rock_name) == "string") | ||
43 | local base_name = dir.base_name(rock_name) | 25 | local base_name = dir.base_name(rock_name) |
44 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | 26 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" |
45 | end | 27 | end |
46 | 28 | ||
47 | function path.root_from_rocks_dir(rocks_dir) | 29 | function path.root_from_rocks_dir(rocks_dir) |
30 | assert(type(rocks_dir) == "string") | ||
48 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") | 31 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") |
49 | end | 32 | end |
50 | 33 | ||
34 | function path.root_dir(tree) | ||
35 | if type(tree) == "string" then | ||
36 | return tree | ||
37 | else | ||
38 | assert(type(tree) == "table") | ||
39 | return tree.root | ||
40 | end | ||
41 | end | ||
42 | |||
51 | function path.deploy_bin_dir(tree) | 43 | function path.deploy_bin_dir(tree) |
52 | return dir.path(path.root_dir(tree), "bin") | 44 | return dir.path(path.root_dir(tree), "bin") |
53 | end | 45 | end |
@@ -56,122 +48,132 @@ function path.manifest_file(tree) | |||
56 | return dir.path(path.rocks_dir(tree), "manifest") | 48 | return dir.path(path.rocks_dir(tree), "manifest") |
57 | end | 49 | end |
58 | 50 | ||
59 | 51 | --- Get the directory for all versions of a package in a tree. | |
60 | 52 | -- @param name string: The package name. | |
61 | 53 | -- @return string: The resulting path -- does not guarantee that | |
62 | 54 | -- @param tree string or nil: If given, specifies the local tree to use. | |
63 | 55 | -- the package (and by extension, the path) exists. | |
64 | function path.versions_dir(name, tree) | 56 | function path.versions_dir(name, tree) |
65 | assert(not name:match("/")) | 57 | assert(type(name) == "string" and not name:match("/")) |
66 | return dir.path(path.rocks_dir(tree), name) | 58 | return dir.path(path.rocks_dir(tree), name) |
67 | end | 59 | end |
68 | 60 | ||
69 | 61 | --- Get the local installation directory (prefix) for a package. | |
70 | 62 | -- @param name string: The package name. | |
71 | 63 | -- @param version string: The package version. | |
72 | 64 | -- @param tree string or nil: If given, specifies the local tree to use. | |
73 | 65 | -- @return string: The resulting path -- does not guarantee that | |
74 | 66 | -- the package (and by extension, the path) exists. | |
75 | function path.install_dir(name, version, tree) | 67 | function path.install_dir(name, version, tree) |
76 | assert(not name:match("/")) | 68 | assert(type(name) == "string" and not name:match("/")) |
69 | assert(type(version) == "string") | ||
77 | return dir.path(path.rocks_dir(tree), name, version) | 70 | return dir.path(path.rocks_dir(tree), name, version) |
78 | end | 71 | end |
79 | 72 | ||
80 | 73 | --- Get the local filename of the rockspec of an installed rock. | |
81 | 74 | -- @param name string: The package name. | |
82 | 75 | -- @param version string: The package version. | |
83 | 76 | -- @param tree string or nil: If given, specifies the local tree to use. | |
84 | 77 | -- @return string: The resulting path -- does not guarantee that | |
85 | 78 | -- the package (and by extension, the file) exists. | |
86 | function path.rockspec_file(name, version, tree) | 79 | function path.rockspec_file(name, version, tree) |
87 | assert(not name:match("/")) | 80 | assert(type(name) == "string" and not name:match("/")) |
88 | return dir.path(path.rocks_dir(tree), name, version, name .. "-" .. version .. ".rockspec") | 81 | assert(type(version) == "string") |
82 | return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") | ||
89 | end | 83 | end |
90 | 84 | ||
91 | 85 | --- Get the local filename of the rock_manifest file of an installed rock. | |
92 | 86 | -- @param name string: The package name. | |
93 | 87 | -- @param version string: The package version. | |
94 | 88 | -- @param tree string or nil: If given, specifies the local tree to use. | |
95 | 89 | -- @return string: The resulting path -- does not guarantee that | |
96 | 90 | -- the package (and by extension, the file) exists. | |
97 | function path.rock_manifest_file(name, version, tree) | 91 | function path.rock_manifest_file(name, version, tree) |
98 | assert(not name:match("/")) | 92 | assert(type(name) == "string" and not name:match("/")) |
93 | assert(type(version) == "string") | ||
99 | return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") | 94 | return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") |
100 | end | 95 | end |
101 | 96 | ||
102 | 97 | --- Get the local filename of the rock_namespace file of an installed rock. | |
103 | 98 | -- @param name string: The package name (without a namespace). | |
104 | 99 | -- @param version string: The package version. | |
105 | 100 | -- @param tree string or nil: If given, specifies the local tree to use. | |
106 | 101 | -- @return string: The resulting path -- does not guarantee that | |
107 | 102 | -- the package (and by extension, the file) exists. | |
108 | function path.rock_namespace_file(name, version, tree) | 103 | function path.rock_namespace_file(name, version, tree) |
109 | assert(not name:match("/")) | 104 | assert(type(name) == "string" and not name:match("/")) |
105 | assert(type(version) == "string") | ||
110 | return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") | 106 | return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") |
111 | end | 107 | end |
112 | 108 | ||
113 | 109 | --- Get the local installation directory for C libraries of a package. | |
114 | 110 | -- @param name string: The package name. | |
115 | 111 | -- @param version string: The package version. | |
116 | 112 | -- @param tree string or nil: If given, specifies the local tree to use. | |
117 | 113 | -- @return string: The resulting path -- does not guarantee that | |
118 | 114 | -- the package (and by extension, the path) exists. | |
119 | function path.lib_dir(name, version, tree) | 115 | function path.lib_dir(name, version, tree) |
120 | assert(not name:match("/")) | 116 | assert(type(name) == "string" and not name:match("/")) |
117 | assert(type(version) == "string") | ||
121 | return dir.path(path.rocks_dir(tree), name, version, "lib") | 118 | return dir.path(path.rocks_dir(tree), name, version, "lib") |
122 | end | 119 | end |
123 | 120 | ||
124 | 121 | --- Get the local installation directory for Lua modules of a package. | |
125 | 122 | -- @param name string: The package name. | |
126 | 123 | -- @param version string: The package version. | |
127 | 124 | -- @param tree string or nil: If given, specifies the local tree to use. | |
128 | 125 | -- @return string: The resulting path -- does not guarantee that | |
129 | 126 | -- the package (and by extension, the path) exists. | |
130 | function path.lua_dir(name, version, tree) | 127 | function path.lua_dir(name, version, tree) |
131 | assert(not name:match("/")) | 128 | assert(type(name) == "string" and not name:match("/")) |
129 | assert(type(version) == "string") | ||
132 | return dir.path(path.rocks_dir(tree), name, version, "lua") | 130 | return dir.path(path.rocks_dir(tree), name, version, "lua") |
133 | end | 131 | end |
134 | 132 | ||
135 | 133 | --- Get the local installation directory for documentation of a package. | |
136 | 134 | -- @param name string: The package name. | |
137 | 135 | -- @param version string: The package version. | |
138 | 136 | -- @param tree string or nil: If given, specifies the local tree to use. | |
139 | 137 | -- @return string: The resulting path -- does not guarantee that | |
140 | 138 | -- the package (and by extension, the path) exists. | |
141 | function path.doc_dir(name, version, tree) | 139 | function path.doc_dir(name, version, tree) |
142 | assert(not name:match("/")) | 140 | assert(type(name) == "string" and not name:match("/")) |
141 | assert(type(version) == "string") | ||
143 | return dir.path(path.rocks_dir(tree), name, version, "doc") | 142 | return dir.path(path.rocks_dir(tree), name, version, "doc") |
144 | end | 143 | end |
145 | 144 | ||
146 | 145 | --- Get the local installation directory for configuration files of a package. | |
147 | 146 | -- @param name string: The package name. | |
148 | 147 | -- @param version string: The package version. | |
149 | 148 | -- @param tree string or nil: If given, specifies the local tree to use. | |
150 | 149 | -- @return string: The resulting path -- does not guarantee that | |
151 | 150 | -- the package (and by extension, the path) exists. | |
152 | function path.conf_dir(name, version, tree) | 151 | function path.conf_dir(name, version, tree) |
153 | assert(not name:match("/")) | 152 | assert(type(name) == "string" and not name:match("/")) |
153 | assert(type(version) == "string") | ||
154 | return dir.path(path.rocks_dir(tree), name, version, "conf") | 154 | return dir.path(path.rocks_dir(tree), name, version, "conf") |
155 | end | 155 | end |
156 | 156 | ||
157 | 157 | --- Get the local installation directory for command-line scripts | |
158 | 158 | -- of a package. | |
159 | 159 | -- @param name string: The package name. | |
160 | 160 | -- @param version string: The package version. | |
161 | 161 | -- @param tree string or nil: If given, specifies the local tree to use. | |
162 | 162 | -- @return string: The resulting path -- does not guarantee that | |
163 | 163 | -- the package (and by extension, the path) exists. | |
164 | function path.bin_dir(name, version, tree) | 164 | function path.bin_dir(name, version, tree) |
165 | assert(not name:match("/")) | 165 | assert(type(name) == "string" and not name:match("/")) |
166 | assert(type(version) == "string") | ||
166 | return dir.path(path.rocks_dir(tree), name, version, "bin") | 167 | return dir.path(path.rocks_dir(tree), name, version, "bin") |
167 | end | 168 | end |
168 | 169 | ||
169 | 170 | --- Extract name, version and arch of a rock filename, | |
170 | 171 | -- or name, version and "rockspec" from a rockspec name. | |
171 | 172 | -- @param file_name string: pathname of a rock or rockspec | |
172 | 173 | -- @return (string, string, string) or nil: name, version and arch | |
173 | 174 | -- or nil if name could not be parsed | |
174 | function path.parse_name(file_name) | 175 | function path.parse_name(file_name) |
176 | assert(type(file_name) == "string") | ||
175 | if file_name:match("%.rock$") then | 177 | if file_name:match("%.rock$") then |
176 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") | 178 | return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") |
177 | else | 179 | else |
@@ -179,30 +181,35 @@ function path.parse_name(file_name) | |||
179 | end | 181 | end |
180 | end | 182 | end |
181 | 183 | ||
182 | 184 | --- Make a rockspec or rock URL. | |
183 | 185 | -- @param pathname string: Base URL or pathname. | |
184 | 186 | -- @param name string: Package name. | |
185 | 187 | -- @param version string: Package version. | |
186 | 188 | -- @param arch string: Architecture identifier, or "rockspec" or "installed". | |
187 | 189 | -- @return string: A URL or pathname following LuaRocks naming conventions. | |
188 | function path.make_url(pathname, name, version, arch) | 190 | function path.make_url(pathname, name, version, arch) |
189 | assert(not name:match("/")) | 191 | assert(type(pathname) == "string") |
190 | local filename = name .. "-" .. version | 192 | assert(type(name) == "string" and not name:match("/")) |
193 | assert(type(version) == "string") | ||
194 | assert(type(arch) == "string") | ||
195 | |||
196 | local filename = name.."-"..version | ||
191 | if arch == "installed" then | 197 | if arch == "installed" then |
192 | filename = dir.path(name, version, filename .. ".rockspec") | 198 | filename = dir.path(name, version, filename..".rockspec") |
193 | elseif arch == "rockspec" then | 199 | elseif arch == "rockspec" then |
194 | filename = filename .. ".rockspec" | 200 | filename = filename..".rockspec" |
195 | else | 201 | else |
196 | filename = filename .. "." .. arch .. ".rock" | 202 | filename = filename.."."..arch..".rock" |
197 | end | 203 | end |
198 | return dir.path(pathname, filename) | 204 | return dir.path(pathname, filename) |
199 | end | 205 | end |
200 | 206 | ||
201 | 207 | --- Obtain the directory name where a module should be stored. | |
202 | 208 | -- For example, on Unix, "foo.bar.baz" will return "foo/bar". | |
203 | 209 | -- @param mod string: A module name in Lua dot-separated format. | |
204 | 210 | -- @return string: A directory name using the platform's separator. | |
205 | function path.module_to_path(mod) | 211 | function path.module_to_path(mod) |
212 | assert(type(mod) == "string") | ||
206 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) | 213 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) |
207 | end | 214 | end |
208 | 215 | ||
@@ -215,20 +222,22 @@ function path.use_tree(tree) | |||
215 | end | 222 | end |
216 | 223 | ||
217 | function path.add_to_package_paths(tree) | 224 | function path.add_to_package_paths(tree) |
218 | package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" .. | 225 | package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" |
219 | dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" .. | 226 | .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" |
220 | package.path | 227 | .. package.path |
221 | package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" .. | 228 | package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" |
222 | package.cpath | 229 | .. package.cpath |
223 | end | 230 | end |
224 | 231 | ||
225 | 232 | --- Get the namespace of a locally-installed rock, if any. | |
226 | 233 | -- @param name string: The rock name, without a namespace. | |
227 | 234 | -- @param version string: The rock version. | |
228 | 235 | -- @param tree string: The local tree to use. | |
229 | 236 | -- @return string?: The namespace if it exists, or nil. | |
230 | function path.read_namespace(name, version, tree) | 237 | function path.read_namespace(name, version, tree) |
231 | assert(not name:match("/")) | 238 | assert(type(name) == "string" and not name:match("/")) |
239 | assert(type(version) == "string") | ||
240 | assert(type(tree) == "string") | ||
232 | 241 | ||
233 | local namespace | 242 | local namespace |
234 | local fd = io.open(path.rock_namespace_file(name, version, tree), "r") | 243 | local fd = io.open(path.rock_namespace_file(name, version, tree), "r") |