diff --git a/crush.lua b/crush.lua index 8e36fc9..248e0a3 100644 --- a/crush.lua +++ b/crush.lua @@ -7,29 +7,7 @@ local io = require 'io' local os = require 'os' --- Utility functions - -local function split(sep, s) - local t = {} - - for i in s:gmatch("([^"..sep.."]+)") do - t[#t+1] = i - end - - return t -end - -local function startswith(s, prefix) - return s:sub(1, #prefix) == prefix -end - -local function trim(s) - local from = s:match("^%s*()") - - return from > #s and "" or s:match(".*%S", from) -end - --- System specific +-- System specific functions -- -- Portions of this code are based on work from the LuaRocks project. -- LuaRocks is free software and uses the MIT license. @@ -207,33 +185,54 @@ end -- .lovedeps file scan +local function map_file(name) + local fh = io.open(name, 'r') + if fh == nil then + error(name..": can't read file.") + end + + local contents = fh:read('*all') + fh:close() + + return contents +end + local function scandeps(manifest, mode, deps) mode = mode or 'nodups' deps = deps or {} - local i = 0 + local contents = map_file(manifest) + contents = "return "..contents + + local fun, res = load(contents, manifest, 't', {}) + if not fun then + error(res) + end - for line in io.lines(manifest) do - line = trim(line) - i = i + 1 + local ok, def = pcall(fun) + if not ok then + error(def) -- def is now pcall()'s error message + end + if type(def) ~= 'table' then + error("[\""..manifest.."\"] loading file resulted in a '"..type(def).."', 'table' expected.") + end - if line == "" or startswith(line, "#") then - goto skip + for name,url in pairs(def) do + if type(url) == 'function' then + goto skip -- ignore functions end - local fields = split(' ', line) - if #fields ~= 2 then - error(manifest..":"..i..": Syntax error, line must be in 'name url' form.") + if type(url) ~= 'string' then + error(manifest..": unexpected type "..type(dep).." as "..name.." git repository URL.") end - local name, url = fields[1], fields[2] for i in ipairs(deps) do if name == deps[i].name then if mode == 'skipdups' then goto skip end - error(manifest..":"..i..": Duplicate dependency '"..name.."'.") + error(manifest..": Duplicate dependency '"..name.."'.") end end