squish.lua

changeset 11
10bb5834c6db
parent 10
d5a4aabb104b
child 13
b93f52cbe253
equal deleted inserted replaced
10:d5a4aabb104b 11:10bb5834c6db
29 if opts.verbose or opts.very_verbose then print_verbose = print; end 29 if opts.verbose or opts.very_verbose then print_verbose = print; end
30 if opts.very_verbose then print_debug = print; end 30 if opts.very_verbose then print_debug = print; end
31 31
32 local enable_debug = opts.enable_debug; 32 local enable_debug = opts.enable_debug;
33 33
34 local modules, main_files = {}, {}; 34 local modules, main_files, resources = {}, {}, {};
35 35
36 -- Functions to be called from squishy file -- 36 -- Functions to be called from squishy file --
37 37
38 function Module(name) 38 function Module(name)
39 local i = #modules+1; 39 local i = #modules+1;
40 modules[i] = { name = name, url = ___fetch_url }; 40 modules[i] = { name = name, url = ___fetch_url };
41 return function (path) 41 return function (path)
42 modules[i].path = path; 42 modules[i].path = path;
43 end
44 end
45
46 function Resource(name, path)
47 local i = #resources+1;
48 resources[i] = { name = name, path = path or name };
49 return function (path)
50 resources[i].path = path;
43 end 51 end
44 end 52 end
45 53
46 function AutoFetchURL(url) 54 function AutoFetchURL(url)
47 ___fetch_url = url; 55 ___fetch_url = url;
155 print_err("Couldn't pack module '"..modulename.."': "..err); 163 print_err("Couldn't pack module '"..modulename.."': "..err);
156 os.exit(1); 164 os.exit(1);
157 end 165 end
158 end 166 end
159 167
168 if #resources > 0 then
169 print_verbose("Packing resources...")
170 f:write("do local resources = {};\n");
171 for _, resource in ipairs(resources) do
172 local name, path = resource.name, resource.path;
173 local res_file, err = io.open(base_path..path);
174 if not res_file then
175 print_err("Couldn't load resource: "..tostring(err));
176 os.exit(1);
177 end
178 local data = res_file:read("*a");
179 local maxequals = 0;
180 data:gsub("(=+)", function (equals_string) maxequals = math.max(maxequals, #equals_string); end);
181
182 f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "[");
183 f:write(data);
184 f:write("]", string.rep("=", maxequals+1), "];");
185 end
186 f:write[[function require_resource(name) return resources[name] or error("resource '"..tostring(name).."' not found"); end end ]]
187 end
188
160 print_debug("Finalising...") 189 print_debug("Finalising...")
161 for _, fn in pairs(main_files) do 190 for _, fn in pairs(main_files) do
162 local fin, err = io.open(base_path..fn); 191 local fin, err = io.open(base_path..fn);
163 if not fin then 192 if not fin then
164 print_err("Failed to open "..fn..": "..err); 193 print_err("Failed to open "..fn..": "..err);

mercurial