diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2012-09-25 17:17:27 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2012-09-25 17:17:27 -0300 |
commit | 74b0087d28c735dc5440c4587be3085abbd05257 (patch) | |
tree | 2b2593efc8cc06c982b49bc4924ec09875bee190 /src | |
parent | 233a9af0f19e00cc8aa488cc1557497c76d2a7b0 (diff) | |
download | luarocks-74b0087d28c735dc5440c4587be3085abbd05257.tar.gz luarocks-74b0087d28c735dc5440c4587be3085abbd05257.tar.bz2 luarocks-74b0087d28c735dc5440c4587be3085abbd05257.zip |
factor out tree map operation and use it in "luarocks path" command, providing proper bin paths.
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/cfg.lua | 4 | ||||
-rw-r--r-- | src/luarocks/dir.lua | 4 | ||||
-rw-r--r-- | src/luarocks/fs/lua.lua | 40 | ||||
-rw-r--r-- | src/luarocks/manif_core.lua | 29 | ||||
-rw-r--r-- | src/luarocks/path.lua | 28 | ||||
-rw-r--r-- | src/luarocks/util.lua | 2 |
6 files changed, 60 insertions, 47 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index b2ed2a96..330fabca 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
@@ -277,7 +277,7 @@ if detected.windows then | |||
277 | lib = { "?.dll", "lib?.dll" }, | 277 | lib = { "?.dll", "lib?.dll" }, |
278 | include = { "?.h" } | 278 | include = { "?.h" } |
279 | } | 279 | } |
280 | defaults.export_path = "SET PATH=%s;%s" | 280 | defaults.export_path = "SET PATH=%s" |
281 | defaults.export_path_separator = ";" | 281 | defaults.export_path_separator = ";" |
282 | defaults.export_lua_path = "SET LUA_PATH=%s" | 282 | defaults.export_lua_path = "SET LUA_PATH=%s" |
283 | defaults.export_lua_cpath = "SET LUA_CPATH=%s" | 283 | defaults.export_lua_cpath = "SET LUA_CPATH=%s" |
@@ -322,7 +322,7 @@ if detected.unix then | |||
322 | lib = { "lib?.so", "lib?.so.*" }, | 322 | lib = { "lib?.so", "lib?.so.*" }, |
323 | include = { "?.h" } | 323 | include = { "?.h" } |
324 | } | 324 | } |
325 | defaults.export_path = "export PATH='%s:%s'" | 325 | defaults.export_path = "export PATH='%s'" |
326 | defaults.export_path_separator = ":" | 326 | defaults.export_path_separator = ":" |
327 | defaults.export_lua_path = "export LUA_PATH='%s'" | 327 | defaults.export_lua_path = "export LUA_PATH='%s'" |
328 | defaults.export_lua_cpath = "export LUA_CPATH='%s'" | 328 | defaults.export_lua_cpath = "export LUA_CPATH='%s'" |
diff --git a/src/luarocks/dir.lua b/src/luarocks/dir.lua index 3de9e241..c2309ff1 100644 --- a/src/luarocks/dir.lua +++ b/src/luarocks/dir.lua | |||
@@ -68,3 +68,7 @@ function split_url(url) | |||
68 | end | 68 | end |
69 | return protocol, pathname | 69 | return protocol, pathname |
70 | end | 70 | end |
71 | |||
72 | function normalize(name) | ||
73 | return name:gsub("\\", "/"):gsub("(.)/*$", "%1") | ||
74 | end | ||
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index c413ccb3..eba61214 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -43,10 +43,6 @@ function Q(arg) | |||
43 | return "'" .. arg:gsub("\\", "\\\\"):gsub("'", "'\\''") .. "'" | 43 | return "'" .. arg:gsub("\\", "\\\\"):gsub("'", "'\\''") .. "'" |
44 | end | 44 | end |
45 | 45 | ||
46 | local function normalize(name) | ||
47 | return name:gsub("\\", "/"):gsub("(.)/*$", "%1") | ||
48 | end | ||
49 | |||
50 | --- Test is file/dir is writable. | 46 | --- Test is file/dir is writable. |
51 | -- Warning: testing if a file/dir is writable does not guarantee | 47 | -- Warning: testing if a file/dir is writable does not guarantee |
52 | -- that it will remain writable and therefore it is no replacement | 48 | -- that it will remain writable and therefore it is no replacement |
@@ -55,7 +51,7 @@ end | |||
55 | -- @return boolean: true if file exists, false otherwise. | 51 | -- @return boolean: true if file exists, false otherwise. |
56 | function is_writable(file) | 52 | function is_writable(file) |
57 | assert(file) | 53 | assert(file) |
58 | file = normalize(file) | 54 | file = dir.normalize(file) |
59 | local result | 55 | local result |
60 | if fs.is_dir(file) then | 56 | if fs.is_dir(file) then |
61 | local file2 = dir.path(file, '.tmpluarockstestwritable') | 57 | local file2 = dir.path(file, '.tmpluarockstestwritable') |
@@ -77,7 +73,7 @@ end | |||
77 | -- @return string or nil: name of temporary directory or nil on failure. | 73 | -- @return string or nil: name of temporary directory or nil on failure. |
78 | function make_temp_dir(name) | 74 | function make_temp_dir(name) |
79 | assert(type(name) == "string") | 75 | assert(type(name) == "string") |
80 | name = normalize(name) | 76 | name = dir.normalize(name) |
81 | 77 | ||
82 | local temp_dir = (os.getenv("TMP") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) | 78 | local temp_dir = (os.getenv("TMP") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) |
83 | if fs.make_dir(temp_dir) then | 79 | if fs.make_dir(temp_dir) then |
@@ -110,7 +106,7 @@ end | |||
110 | -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not | 106 | -- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false if not |
111 | -- or if it could not perform the check for any reason. | 107 | -- or if it could not perform the check for any reason. |
112 | function check_md5(file, md5sum) | 108 | function check_md5(file, md5sum) |
113 | file = normalize(file) | 109 | file = dir.normalize(file) |
114 | local computed = fs.get_md5(file) | 110 | local computed = fs.get_md5(file) |
115 | if not computed then | 111 | if not computed then |
116 | return false | 112 | return false |
@@ -156,7 +152,7 @@ end | |||
156 | -- @param d string: The directory to switch to. | 152 | -- @param d string: The directory to switch to. |
157 | function change_dir(d) | 153 | function change_dir(d) |
158 | table.insert(dir_stack, lfs.currentdir()) | 154 | table.insert(dir_stack, lfs.currentdir()) |
159 | d = normalize(d) | 155 | d = dir.normalize(d) |
160 | lfs.chdir(d) | 156 | lfs.chdir(d) |
161 | end | 157 | end |
162 | 158 | ||
@@ -187,7 +183,7 @@ end | |||
187 | -- @return boolean: true on success, false on failure. | 183 | -- @return boolean: true on success, false on failure. |
188 | function make_dir(directory) | 184 | function make_dir(directory) |
189 | assert(type(directory) == "string") | 185 | assert(type(directory) == "string") |
190 | directory = normalize(directory) | 186 | directory = dir.normalize(directory) |
191 | local path = nil | 187 | local path = nil |
192 | if directory:sub(2, 2) == ":" then | 188 | if directory:sub(2, 2) == ":" then |
193 | path = directory:sub(1, 2) | 189 | path = directory:sub(1, 2) |
@@ -217,7 +213,7 @@ end | |||
217 | -- @param d string: pathname of directory to remove. | 213 | -- @param d string: pathname of directory to remove. |
218 | function remove_dir_if_empty(d) | 214 | function remove_dir_if_empty(d) |
219 | assert(d) | 215 | assert(d) |
220 | d = normalize(d) | 216 | d = dir.normalize(d) |
221 | lfs.rmdir(d) | 217 | lfs.rmdir(d) |
222 | end | 218 | end |
223 | 219 | ||
@@ -227,7 +223,7 @@ end | |||
227 | -- @param d string: pathname of directory to remove. | 223 | -- @param d string: pathname of directory to remove. |
228 | function remove_dir_tree_if_empty(d) | 224 | function remove_dir_tree_if_empty(d) |
229 | assert(d) | 225 | assert(d) |
230 | d = normalize(d) | 226 | d = dir.normalize(d) |
231 | for i=1,10 do | 227 | for i=1,10 do |
232 | lfs.rmdir(d) | 228 | lfs.rmdir(d) |
233 | d = dir.dir_name(d) | 229 | d = dir.dir_name(d) |
@@ -243,8 +239,8 @@ end | |||
243 | -- plus an error message. | 239 | -- plus an error message. |
244 | function copy(src, dest, perms) | 240 | function copy(src, dest, perms) |
245 | assert(src and dest) | 241 | assert(src and dest) |
246 | src = normalize(src) | 242 | src = dir.normalize(src) |
247 | dest = normalize(dest) | 243 | dest = dir.normalize(dest) |
248 | local destmode = lfs.attributes(dest, "mode") | 244 | local destmode = lfs.attributes(dest, "mode") |
249 | if destmode == "directory" then | 245 | if destmode == "directory" then |
250 | dest = dir.path(dest, dir.base_name(src)) | 246 | dest = dir.path(dest, dir.base_name(src)) |
@@ -296,8 +292,8 @@ end | |||
296 | -- plus an error message. | 292 | -- plus an error message. |
297 | function copy_contents(src, dest) | 293 | function copy_contents(src, dest) |
298 | assert(src and dest) | 294 | assert(src and dest) |
299 | src = normalize(src) | 295 | src = dir.normalize(src) |
300 | dest = normalize(dest) | 296 | dest = dir.normalize(dest) |
301 | assert(lfs.attributes(src, "mode") == "directory") | 297 | assert(lfs.attributes(src, "mode") == "directory") |
302 | 298 | ||
303 | for file in lfs.dir(src) do | 299 | for file in lfs.dir(src) do |
@@ -338,7 +334,7 @@ end | |||
338 | -- @param name string: Pathname of source | 334 | -- @param name string: Pathname of source |
339 | -- @return boolean: true on success, false on failure. | 335 | -- @return boolean: true on success, false on failure. |
340 | function delete(name) | 336 | function delete(name) |
341 | name = normalize(name) | 337 | name = dir.normalize(name) |
342 | return recursive_delete(name) or false | 338 | return recursive_delete(name) or false |
343 | end | 339 | end |
344 | 340 | ||
@@ -352,7 +348,7 @@ function list_dir(at) | |||
352 | if not at then | 348 | if not at then |
353 | at = fs.current_dir() | 349 | at = fs.current_dir() |
354 | end | 350 | end |
355 | at = normalize(at) | 351 | at = dir.normalize(at) |
356 | if not fs.is_dir(at) then | 352 | if not fs.is_dir(at) then |
357 | return {} | 353 | return {} |
358 | end | 354 | end |
@@ -393,7 +389,7 @@ function find(at) | |||
393 | if not at then | 389 | if not at then |
394 | at = fs.current_dir() | 390 | at = fs.current_dir() |
395 | end | 391 | end |
396 | at = normalize(at) | 392 | at = dir.normalize(at) |
397 | if not fs.is_dir(at) then | 393 | if not fs.is_dir(at) then |
398 | return {} | 394 | return {} |
399 | end | 395 | end |
@@ -407,7 +403,7 @@ end | |||
407 | -- @return boolean: true if file exists, false otherwise. | 403 | -- @return boolean: true if file exists, false otherwise. |
408 | function exists(file) | 404 | function exists(file) |
409 | assert(file) | 405 | assert(file) |
410 | file = normalize(file) | 406 | file = dir.normalize(file) |
411 | return type(lfs.attributes(file)) == "table" | 407 | return type(lfs.attributes(file)) == "table" |
412 | end | 408 | end |
413 | 409 | ||
@@ -416,7 +412,7 @@ end | |||
416 | -- @return boolean: true if it is a directory, false otherwise. | 412 | -- @return boolean: true if it is a directory, false otherwise. |
417 | function is_dir(file) | 413 | function is_dir(file) |
418 | assert(file) | 414 | assert(file) |
419 | file = normalize(file) | 415 | file = dir.normalize(file) |
420 | return lfs.attributes(file, "mode") == "directory" | 416 | return lfs.attributes(file, "mode") == "directory" |
421 | end | 417 | end |
422 | 418 | ||
@@ -425,12 +421,12 @@ end | |||
425 | -- @return boolean: true if it is a file, false otherwise. | 421 | -- @return boolean: true if it is a file, false otherwise. |
426 | function is_file(file) | 422 | function is_file(file) |
427 | assert(file) | 423 | assert(file) |
428 | file = normalize(file) | 424 | file = dir.normalize(file) |
429 | return lfs.attributes(file, "mode") == "file" | 425 | return lfs.attributes(file, "mode") == "file" |
430 | end | 426 | end |
431 | 427 | ||
432 | function set_time(file, time) | 428 | function set_time(file, time) |
433 | file = normalize(file) | 429 | file = dir.normalize(file) |
434 | return lfs.touch(file, time) | 430 | return lfs.touch(file, time) |
435 | end | 431 | end |
436 | 432 | ||
diff --git a/src/luarocks/manif_core.lua b/src/luarocks/manif_core.lua index ed6cac07..6af99fe7 100644 --- a/src/luarocks/manif_core.lua +++ b/src/luarocks/manif_core.lua | |||
@@ -63,30 +63,15 @@ function get_versions(name, use_trees) | |||
63 | assert(type(name) == "string") | 63 | assert(type(name) == "string") |
64 | assert(type(use_trees) == "string") | 64 | assert(type(use_trees) == "string") |
65 | 65 | ||
66 | local manifest | 66 | local manifest = {} |
67 | 67 | path.map_trees(use_trees, function(tree) | |
68 | if use_trees == "one" then | 68 | local loaded = load_local_manifest(path.rocks_dir(tree)) |
69 | manifest = load_local_manifest(cfg.rocks_dir) | 69 | if loaded then |
70 | elseif use_trees == "all" or use_trees == "order" then | 70 | util.deep_merge(manifest, loaded) |
71 | manifest = {} | ||
72 | local use = false | ||
73 | if use_trees == "all" then | ||
74 | use = true | ||
75 | end | ||
76 | for _, tree in ipairs(cfg.rocks_trees) do | ||
77 | if tree == cfg.rocks_dir then | ||
78 | use = true | ||
79 | end | ||
80 | if use then | ||
81 | local loaded = load_local_manifest(path.rocks_dir(tree)) | ||
82 | if loaded then | ||
83 | util.deep_merge(manifest, loaded) | ||
84 | end | ||
85 | end | ||
86 | end | 71 | end |
87 | end | 72 | end) |
88 | 73 | ||
89 | local item = manifest and manifest.repository[name] | 74 | local item = next(manifest) and manifest.repository[name] |
90 | if item then | 75 | if item then |
91 | return util.keys(item) | 76 | return util.keys(item) |
92 | end | 77 | end |
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index bbd928a0..17d9d52b 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua | |||
@@ -7,6 +7,7 @@ module("luarocks.path", package.seeall) | |||
7 | local dir = require("luarocks.dir") | 7 | local dir = require("luarocks.dir") |
8 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.cfg") |
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | local deps = require("luarocks.deps") | ||
10 | 11 | ||
11 | help_summary = "Return the currently configured package path." | 12 | help_summary = "Return the currently configured package path." |
12 | help_arguments = "" | 13 | help_arguments = "" |
@@ -305,6 +306,27 @@ function use_tree(tree) | |||
305 | cfg.deploy_lib_dir = deploy_lib_dir(tree) | 306 | cfg.deploy_lib_dir = deploy_lib_dir(tree) |
306 | end | 307 | end |
307 | 308 | ||
309 | function map_trees(use_trees, fn, ...) | ||
310 | local result = {} | ||
311 | if use_trees == "one" then | ||
312 | table.insert(result, fn(cfg.root_dir, ...)) | ||
313 | elseif use_trees == "all" or use_trees == "order" then | ||
314 | local use = false | ||
315 | if use_trees == "all" then | ||
316 | use = true | ||
317 | end | ||
318 | for _, tree in ipairs(cfg.rocks_trees) do | ||
319 | if dir.normalize(tree) == dir.normalize(cfg.root_dir) then | ||
320 | use = true | ||
321 | end | ||
322 | if use then | ||
323 | table.insert(result, fn(tree, ...)) | ||
324 | end | ||
325 | end | ||
326 | end | ||
327 | return result | ||
328 | end | ||
329 | |||
308 | --- Return the pathname of the file that would be loaded for a module, indexed. | 330 | --- Return the pathname of the file that would be loaded for a module, indexed. |
309 | -- @param module_name string: module name (eg. "socket.core") | 331 | -- @param module_name string: module name (eg. "socket.core") |
310 | -- @param name string: name of the package (eg. "luasocket") | 332 | -- @param name string: name of the package (eg. "luasocket") |
@@ -352,10 +374,14 @@ end | |||
352 | -- @return boolean This function always succeeds. | 374 | -- @return boolean This function always succeeds. |
353 | function run(...) | 375 | function run(...) |
354 | local flags = util.parse_flags(...) | 376 | local flags = util.parse_flags(...) |
377 | local deps_mode = deps.flags_to_deps_mode(flags) | ||
378 | |||
355 | util.printout(cfg.export_lua_path:format(util.remove_path_dupes(package.path, ';'))) | 379 | util.printout(cfg.export_lua_path:format(util.remove_path_dupes(package.path, ';'))) |
356 | util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(package.cpath, ';'))) | 380 | util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(package.cpath, ';'))) |
357 | if flags["bin"] then | 381 | if flags["bin"] then |
358 | util.printout(cfg.export_path:format(util.remove_path_dupes(os.getenv("PATH"), cfg.export_path_separator), cfg.deploy_bin_dir)) | 382 | local bin_dirs = map_trees(deps_mode, deploy_bin_dir) |
383 | table.insert(bin_dirs, 1, os.getenv("PATH")) | ||
384 | util.printout(cfg.export_path:format(util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator))) | ||
359 | end | 385 | end |
360 | return true | 386 | return true |
361 | end | 387 | end |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 1a1cccf9..46802255 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -331,6 +331,8 @@ end | |||
331 | -- @param list string: A path string (from $PATH or package.path) | 331 | -- @param list string: A path string (from $PATH or package.path) |
332 | -- @param sep string: The separator | 332 | -- @param sep string: The separator |
333 | function remove_path_dupes(list, sep) | 333 | function remove_path_dupes(list, sep) |
334 | assert(type(list) == "string") | ||
335 | assert(type(sep) == "string") | ||
334 | local parts = split_string(list, sep) | 336 | local parts = split_string(list, sep) |
335 | local final, entries = {}, {} | 337 | local final, entries = {}, {} |
336 | for _, part in ipairs(parts) do | 338 | for _, part in ipairs(parts) do |