aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-07-22 21:37:54 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commit43c5a899d41533af4c107004643b139ce0349d12 (patch)
treebe089508c3a8f6f0f73a007351476a5e2b61665d
parent74960c88576c9e125d898ebb949e7bfada776c66 (diff)
downloadluarocks-43c5a899d41533af4c107004643b139ce0349d12.tar.gz
luarocks-43c5a899d41533af4c107004643b139ce0349d12.tar.bz2
luarocks-43c5a899d41533af4c107004643b139ce0349d12.zip
fix attempt 2
-rw-r--r--src/luarocks/path-incomplete.lua254
-rw-r--r--src/luarocks/path-origianl.lua263
-rw-r--r--src/luarocks/path.lua269
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 @@
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 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
6local cfg = require("luarocks.core.cfg")
7local core = require("luarocks.core.path")
8local dir = require("luarocks.dir")
9local util = require("luarocks.core.util")
10
11
12
13local path = {}
14
15
16
17
18
19
20
21
22
23path.rocks_dir = core.rocks_dir
24path.versioned_name = core.versioned_name
25path.path_to_module = core.path_to_module
26path.deploy_lua_dir = core.deploy_lua_dir
27path.deploy_lib_dir = core.deploy_lib_dir
28path.map_trees = core.map_trees
29path.rocks_tree_to_string = core.rocks_tree_to_string
30
31function path.root_dir(tree)
32 if type(tree) == "string" then
33 return tree
34 else
35 return tree.root
36 end
37end
38
39
40
41
42function path.rockspec_name_from_rock(rock_name)
43 local base_name = dir.base_name(rock_name)
44 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec"
45end
46
47function path.root_from_rocks_dir(rocks_dir)
48 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$")
49end
50
51function path.deploy_bin_dir(tree)
52 return dir.path(path.root_dir(tree), "bin")
53end
54
55function path.manifest_file(tree)
56 return dir.path(path.rocks_dir(tree), "manifest")
57end
58
59
60
61
62
63
64function path.versions_dir(name, tree)
65 assert(not name:match("/"))
66 return dir.path(path.rocks_dir(tree), name)
67end
68
69
70
71
72
73
74
75function path.install_dir(name, version, tree)
76 assert(not name:match("/"))
77 return dir.path(path.rocks_dir(tree), name, version)
78end
79
80
81
82
83
84
85
86function path.rockspec_file(name, version, tree)
87 assert(not name:match("/"))
88 return dir.path(path.rocks_dir(tree), name, version, name .. "-" .. version .. ".rockspec")
89end
90
91
92
93
94
95
96
97function path.rock_manifest_file(name, version, tree)
98 assert(not name:match("/"))
99 return dir.path(path.rocks_dir(tree), name, version, "rock_manifest")
100end
101
102
103
104
105
106
107
108function path.rock_namespace_file(name, version, tree)
109 assert(not name:match("/"))
110 return dir.path(path.rocks_dir(tree), name, version, "rock_namespace")
111end
112
113
114
115
116
117
118
119function path.lib_dir(name, version, tree)
120 assert(not name:match("/"))
121 return dir.path(path.rocks_dir(tree), name, version, "lib")
122end
123
124
125
126
127
128
129
130function path.lua_dir(name, version, tree)
131 assert(not name:match("/"))
132 return dir.path(path.rocks_dir(tree), name, version, "lua")
133end
134
135
136
137
138
139
140
141function path.doc_dir(name, version, tree)
142 assert(not name:match("/"))
143 return dir.path(path.rocks_dir(tree), name, version, "doc")
144end
145
146
147
148
149
150
151
152function path.conf_dir(name, version, tree)
153 assert(not name:match("/"))
154 return dir.path(path.rocks_dir(tree), name, version, "conf")
155end
156
157
158
159
160
161
162
163
164function path.bin_dir(name, version, tree)
165 assert(not name:match("/"))
166 return dir.path(path.rocks_dir(tree), name, version, "bin")
167end
168
169
170
171
172
173
174function 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
180end
181
182
183
184
185
186
187
188function 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)
199end
200
201
202
203
204
205function path.module_to_path(mod)
206 return (mod:gsub("[^.]*$", ""):gsub("%.", "/"))
207end
208
209function 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)
215end
216
217function 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
223end
224
225
226
227
228
229
230function 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
240end
241
242function 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, ";")
252end
253
254return 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.
5local path = {}
6
7local core = require("luarocks.core.path")
8local dir = require("luarocks.dir")
9local cfg = require("luarocks.core.cfg")
10local util = require("luarocks.util")
11
12path.rocks_dir = core.rocks_dir
13path.versioned_name = core.versioned_name
14path.path_to_module = core.path_to_module
15path.deploy_lua_dir = core.deploy_lua_dir
16path.deploy_lib_dir = core.deploy_lib_dir
17path.map_trees = core.map_trees
18path.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.
23function 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"
27end
28
29function path.root_from_rocks_dir(rocks_dir)
30 assert(type(rocks_dir) == "string")
31 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$")
32end
33
34function 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
41end
42
43function path.deploy_bin_dir(tree)
44 return dir.path(path.root_dir(tree), "bin")
45end
46
47function path.manifest_file(tree)
48 return dir.path(path.rocks_dir(tree), "manifest")
49end
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.
56function path.versions_dir(name, tree)
57 assert(type(name) == "string" and not name:match("/"))
58 return dir.path(path.rocks_dir(tree), name)
59end
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.
67function 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)
71end
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.
79function 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")
83end
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.
91function 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")
95end
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.
103function 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")
107end
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.
115function 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")
119end
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.
127function 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")
131end
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.
139function 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")
143end
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.
151function 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")
155end
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.
164function 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")
168end
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
175function 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
182end
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.
190function 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)
205end
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.
211function path.module_to_path(mod)
212 assert(type(mod) == "string")
213 return (mod:gsub("[^.]*$", ""):gsub("%.", "/"))
214end
215
216function 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)
222end
223
224function 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
230end
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.
237function 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
249end
250
251function 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, ";")
261end
262
263return 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 @@
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 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.
5local path = {}
5 6
6local cfg = require("luarocks.core.cfg")
7local core = require("luarocks.core.path") 7local core = require("luarocks.core.path")
8local dir = require("luarocks.dir") 8local dir = require("luarocks.dir")
9local util = require("luarocks.core.util") 9local cfg = require("luarocks.core.cfg")
10 10local util = require("luarocks.util")
11
12
13local path = {}
14
15
16
17
18
19
20
21
22 11
23path.rocks_dir = core.rocks_dir 12path.rocks_dir = core.rocks_dir
24path.versioned_name = core.versioned_name 13path.versioned_name = core.versioned_name
@@ -28,26 +17,29 @@ path.deploy_lib_dir = core.deploy_lib_dir
28path.map_trees = core.map_trees 17path.map_trees = core.map_trees
29path.rocks_tree_to_string = core.rocks_tree_to_string 18path.rocks_tree_to_string = core.rocks_tree_to_string
30 19
31function 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
37end
38
39
40
41
42function path.rockspec_name_from_rock(rock_name) 23function 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"
45end 27end
46 28
47function path.root_from_rocks_dir(rocks_dir) 29function 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) .. ".*$")
49end 32end
50 33
34function 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
41end
42
51function path.deploy_bin_dir(tree) 43function path.deploy_bin_dir(tree)
52 return dir.path(path.root_dir(tree), "bin") 44 return dir.path(path.root_dir(tree), "bin")
53end 45end
@@ -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")
57end 49end
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.
64function path.versions_dir(name, tree) 56function 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)
67end 59end
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.
75function path.install_dir(name, version, tree) 67function 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)
78end 71end
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.
86function path.rockspec_file(name, version, tree) 79function 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")
89end 83end
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.
97function path.rock_manifest_file(name, version, tree) 91function 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")
100end 95end
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.
108function path.rock_namespace_file(name, version, tree) 103function 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")
111end 107end
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.
119function path.lib_dir(name, version, tree) 115function 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")
122end 119end
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.
130function path.lua_dir(name, version, tree) 127function 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")
133end 131end
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.
141function path.doc_dir(name, version, tree) 139function 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")
144end 143end
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.
152function path.conf_dir(name, version, tree) 151function 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")
155end 155end
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.
164function path.bin_dir(name, version, tree) 164function 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")
167end 168end
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
174function path.parse_name(file_name) 175function 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
180end 182end
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.
188function path.make_url(pathname, name, version, arch) 190function 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)
199end 205end
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.
205function path.module_to_path(mod) 211function path.module_to_path(mod)
212 assert(type(mod) == "string")
206 return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) 213 return (mod:gsub("[^.]*$", ""):gsub("%.", "/"))
207end 214end
208 215
@@ -215,20 +222,22 @@ function path.use_tree(tree)
215end 222end
216 223
217function path.add_to_package_paths(tree) 224function 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
223end 230end
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.
230function path.read_namespace(name, version, tree) 237function 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")