Support for adding 'resources'

Sat, 25 Jul 2009 19:38:39 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 25 Jul 2009 19:38:39 +0100
changeset 11
10bb5834c6db
parent 10
d5a4aabb104b
child 12
fb4ca0cc228e

Support for adding 'resources'

squish.lua file | annotate | diff | comparison | revisions
--- 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);

mercurial