From b2f4e356b6ede8860b22501aa4ee2578ec9a1a03 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 14 Dec 2010 12:12:26 -0200 Subject: Fix sorting and saving of persisted manifest table. --- src/luarocks/manif.lua | 4 ++-- src/luarocks/manif_core.lua | 4 ++-- src/luarocks/persist.lua | 10 +++------- src/luarocks/util.lua | 16 +++++++++++++++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua index c677a22c..a7211d70 100644 --- a/src/luarocks/manif.lua +++ b/src/luarocks/manif.lua @@ -304,9 +304,9 @@ local function find_providers(file, root) assert(type(file) == "string") root = root or cfg.root_dir - local manifest = manif_core.load_local_manifest(path.rocks_dir(root)) + local manifest, err = manif_core.load_local_manifest(path.rocks_dir(root)) if not manifest then - return nil, "manifest file is missing. Corrupted local rocks tree?" + return nil, err .. " -- corrupted local rocks tree?" end local deploy_bin = path.deploy_bin_dir(root) local deploy_lua = path.deploy_lua_dir(root) diff --git a/src/luarocks/manif_core.lua b/src/luarocks/manif_core.lua index d1eb4ebb..2e1a9518 100644 --- a/src/luarocks/manif_core.lua +++ b/src/luarocks/manif_core.lua @@ -16,9 +16,9 @@ manifest_cache = {} -- @param file string: The local filename of the manifest file. -- @param repo_url string: The repository identifier. function manifest_loader(file, repo_url, quick) - local manifest = persist.load_into_table(file) + local manifest, err = persist.load_into_table(file) if not manifest then - return nil, "Failed loading manifest for "..repo_url + return nil, "Failed loading manifest for "..repo_url..": "..err end if not quick then local ok, err = type_check.type_check_manifest(manifest) diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index d74a805e..c809b51c 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua @@ -28,10 +28,6 @@ function load_into_table(filename, tbl) return result end -local function string_sort(a,b) - return tostring(a) < tostring(b) -end - --- Write a table as Lua code representing a table to disk -- (that is, in curly brackets notation). -- This function handles only numbers, strings and tables @@ -43,7 +39,7 @@ local function write_table(out, tbl, level) local sep = "\n" local indent = true local i = 1 - for k, v in util.sortedpairs(tbl, string_sort) do + for k, v in util.sortedpairs(tbl) do out:write(sep) if indent then for n = 1,level do out:write(" ") end @@ -52,7 +48,7 @@ local function write_table(out, tbl, level) indent = true if type(k) == "number" then if k ~= i then - out:write(tostring(k).."=") + out:write('['..tostring(k).."]=") else i = i + 1 end @@ -95,7 +91,7 @@ function save_from_table(filename, tbl) if not out then return nil, "Cannot create file at "..filename end - for k, v in util.sortedpairs(tbl, string_sort) do + for k, v in util.sortedpairs(tbl) do out:write(k.." = ") write_table(out, v, 1) out:write("\n") diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 10fc1e36..e0c01421 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -216,6 +216,20 @@ function keys(tbl) return ks end +local function default_sort(a, b) + local ta = type(a) + local tb = type(b) + if ta == "number" and tb == "number" then + return a < b + elseif ta == "number" then + return true + elseif tb == "number" then + return false + else + return tostring(a) < tostring(b) + end +end + -- The iterator function used internally by util.sortedpairs. -- @param tbl table: The table to be iterated. -- @param sort_function function or nil: An optional comparison function @@ -223,7 +237,7 @@ end -- @see sortedpairs local function sortedpairs_iterator(tbl, sort_function) local ks = keys(tbl) - table.sort(ks, sort_function) + table.sort(ks, sort_function or default_sort) for _, k in ipairs(ks) do coroutine.yield(k, tbl[k]) end -- cgit v1.2.3-55-g6feb