aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks/path.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/luarocks/path.lua')
-rw-r--r--src/luarocks/path.lua254
1 files changed, 254 insertions, 0 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua
new file mode 100644
index 00000000..65c1a7d2
--- /dev/null
+++ b/src/luarocks/path.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