# HG changeset patch # User Matthew Wild # Date 1248547119 -3600 # Node ID 10bb5834c6db29ec5df820d80c8e9b1c30b58c90 # Parent d5a4aabb104bb27bbbc371246b2c2f8f36b9a9b0 Support for adding 'resources' diff -r d5a4aabb104b -r 10bb5834c6db squish.lua --- a/squish.lua Sat Jul 25 19:38:09 2009 +0100 +++ b/squish.lua Sat Jul 25 19:38:39 2009 +0100 @@ -31,7 +31,7 @@ local enable_debug = opts.enable_debug; -local modules, main_files = {}, {}; +local modules, main_files, resources = {}, {}, {}; -- Functions to be called from squishy file -- @@ -43,6 +43,14 @@ end end +function Resource(name, path) + local i = #resources+1; + resources[i] = { name = name, path = path or name }; + return function (path) + resources[i].path = path; + end +end + function AutoFetchURL(url) ___fetch_url = url; end @@ -157,6 +165,27 @@ end end +if #resources > 0 then + print_verbose("Packing resources...") + f:write("do local resources = {};\n"); + for _, resource in ipairs(resources) do + local name, path = resource.name, resource.path; + local res_file, err = io.open(base_path..path); + if not res_file then + print_err("Couldn't load resource: "..tostring(err)); + os.exit(1); + end + local data = res_file:read("*a"); + local maxequals = 0; + data:gsub("(=+)", function (equals_string) maxequals = math.max(maxequals, #equals_string); end); + + f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "["); + f:write(data); + f:write("]", string.rep("=", maxequals+1), "];"); + end + f:write[[function require_resource(name) return resources[name] or error("resource '"..tostring(name).."' not found"); end end ]] +end + print_debug("Finalising...") for _, fn in pairs(main_files) do local fin, err = io.open(base_path..fn);