gzip/squish.gzip.lua

Thu, 27 May 2010 15:31:53 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 27 May 2010 15:31:53 +0100
changeset 65
41aeda62af16
parent 59
165b36273ce7
child 73
9827b66cd3b2
permissions
-rw-r--r--

gzip: Some changes to make embedding decompression code compatible with uglify

59
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 function gzip_file(infile_fn, outfile_fn)
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local infile, err = io.open(infile_fn);
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 if not infile then
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 print_err("Can't open input file for reading: "..tostring(err));
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 return;
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local outfile, err = io.open(outfile_fn..".gzipped", "w+");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not outfile then
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 print_err("Can't open output file for writing: "..tostring(err));
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return;
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local data = infile:read("*a");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 infile:close();
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local shebang, newdata = data:match("^(#.-\n)(.+)$");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local code = newdata or data;
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if shebang then
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 outfile:write(shebang)
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local compressed = io.popen("gzip -c '"..infile_fn.."'");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 code = compressed:read("*a");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local maxequals = 0;
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 code:gsub("(=+)", function (equals_string) maxequals = math.max(maxequals, #equals_string); end);
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
29 outfile:write("local ungz = (function ()", require_resource "gunzip.lua", " end)()\n");
59
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
31 outfile:write[[return assert(loadstring((function (i)local o={} ungz{input=i,output=function(b)table.insert(o,string.char(b))end}return table.concat(o)end) ]];
59
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
33 --outfile:write [[return assert(loadstring(_gunzip]]
59
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 outfile:write(string.format("%q", code));
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 --outfile:write("[", string.rep("=", maxequals+1), "[", code, "]", string.rep("=", maxequals+1), "]");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 outfile:write(", '@", outfile_fn,"'))()");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 outfile:close();
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 os.rename(outfile_fn..".gzipped", outfile_fn);
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if opts.gzip then
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 print_info("Gzipping "..out_fn.."...");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 gzip_file(out_fn, out_fn);
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 print_info("OK!");
165b36273ce7 gzip: Add support for gzipping output files, based on a very hacked compress.deflatelua by David Manura
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end

mercurial