gzip/squish.gzip.lua

Tue, 27 Nov 2012 08:25:18 -0800

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 27 Nov 2012 08:25:18 -0800
changeset 86
2322f7932064
parent 75
6028ae579fde
permissions
-rw-r--r--

Remove shebangs from modules, if any (thanks Markus Stenberg)

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
73
9827b66cd3b2 squish.gzip.lua: Open output file in binary mode
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
8 local outfile, err = io.open(outfile_fn..".gzipped", "wb+");
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
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
74
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
22
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
23 local file_with_no_shebang, err = io.open(outfile_fn..".pregzip", "wb+");
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
24 if not file_with_no_shebang then
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
25 print_err("Can't open temp file for writing: "..tostring(err));
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
26 return;
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
27 end
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
28
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
29 file_with_no_shebang:write(code);
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
30 file_with_no_shebang:close();
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
31
74
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
32 local compressed = io.popen("gzip -c '"..outfile_fn..".pregzip'");
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
33 code = compressed:read("*a");
74
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
34 compressed:close();
4a76d75a1456 squish.gzip.lua: Write code without shebang to temporary file to avoid compressing the shebang and passing it to loadstring
Matthew Wild <mwild1@gmail.com>
parents: 73
diff changeset
35 os.remove(outfile_fn..".pregzip");
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
36
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 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
38 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
39
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
40 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
41
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
42 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
43
65
41aeda62af16 gzip: Some changes to make embedding decompression code compatible with uglify
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
44 --outfile:write [[return assert(loadstring(_gunzip]]
75
6028ae579fde squish.gzip.lua: Escape \026 in gzipped output to prevent Windows from interpreting it as EOF (thanks to Vadim Peretokin for his persistence!)
Matthew Wild <mwild1@gmail.com>
parents: 74
diff changeset
45 outfile:write((string.format("%q", code):gsub("\026", "\\026")));
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
46 --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
47 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
48 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
49 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
50 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
51
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
52 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
53 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
54 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
55 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
56 end

mercurial