squish.lua

Sun, 26 Jul 2009 03:59:35 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 26 Jul 2009 03:59:35 +0100
changeset 22
cb14d8c0d0f7
parent 21
27c2e279f0f3
child 23
e3b923ccadb1
permissions
-rwxr-xr-x

Add support for virtual IO in dofile and loadfile

0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local short_opts = { v = "verbose", vv = "very_verbose", o = "output", q = "quiet", qq = "very_quiet" }
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
4 local opts = { use_http = false };
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 for _, opt in ipairs(arg) do
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 if opt:match("^%-") then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local name = opt:match("^%-%-?([^%s=]+)()")
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 name = (short_opts[name] or name):gsub("%-+", "_");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 if name:match("^no_") then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 name = name:sub(4, -1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 opts[name] = false;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 opts[name] = opt:match("=(.*)$") or true;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 base_path = opt;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if opts.very_verbose then opts.verbose = true; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if opts.very_quiet then opts.quiet = true; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local noprint = function () end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local print_err, print_info, print_verbose, print_debug = noprint, noprint, noprint, noprint;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if not opts.very_quiet then print_err = print; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if not opts.quiet then print_info = print; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if opts.verbose or opts.very_verbose then print_verbose = print; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if opts.very_verbose then print_debug = print; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
4
9806107f59ea squish.lua: Use --enable-debug flag to decide whether to enable debug options
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
32 local enable_debug = opts.enable_debug;
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
34 local modules, main_files, resources = {}, {}, {};
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 -- Functions to be called from squishy file --
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 function Module(name)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local i = #modules+1;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 modules[i] = { name = name, url = ___fetch_url };
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return function (path)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 modules[i].path = path;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
46 function Resource(name, path)
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
47 local i = #resources+1;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
48 resources[i] = { name = name, path = path or name };
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
49 return function (path)
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
50 resources[i].path = path;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
51 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
52 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
53
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function AutoFetchURL(url)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 ___fetch_url = url;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 function Main(fn)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 table.insert(main_files, fn);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function Output(fn)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 out_fn = fn;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function Option(name)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if opts[name] == nil then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 opts[name] = true;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return function (value)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 opts[name] = value;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return function () end;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
13
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
77 function GetOption(name)
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
78 return opts[name:gsub('%-', '_')];
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
79 end
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
80
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- -- -- -- -- -- -- --- -- -- -- -- -- -- -- --
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 base_path = (base_path or "."):gsub("/$", "").."/"
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 squishy_file = base_path .. "squishy";
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 out_fn = opts.output or "squished.out.lua";
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local ok, err = pcall(dofile, squishy_file);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if not ok then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 print_err("Couldn't read squishy file: "..err);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local fetch = {};
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 function fetch.filesystem(path)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local f, err = io.open(path);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 if not f then return false, err; end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local data = f:read("*a");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 f:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 return data;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
105 if opts.use_http then
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
106 function fetch.http(url)
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
107 local http = require "socket.http";
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
108
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
109 local body, status = http.request(url);
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
110 if status == 200 then
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
111 return body;
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
112 end
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
113 return false, "HTTP status code: "..tostring(status);
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
115 else
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
116 function fetch.http(url)
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
117 return false, "Module not found. Re-squish with --use-http option to fetch it from "..url;
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
118 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 print_info("Writing "..out_fn.."...");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local f = io.open(out_fn, "w+");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 if opts.executable then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 f:write("#!/usr/bin/env lua\n");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if enable_debug then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 f:write [[
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 local function ___rename_chunk(chunk, name)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 if type(chunk) == "function" then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 chunk = string.dump(chunk);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local intsize = chunk:sub(8,8):byte();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local b = { chunk:sub(13, 13+intsize-1):byte(1, intsize) };
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local oldlen = 0;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 for i = 1, #b do
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 oldlen = oldlen + b[i] * 2^((i-1)*8);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 local newname = name.."\0";
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local newlen = #newname;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local b = { };
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 for i=1,intsize do
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 b[i] = string.char(math.floor(newlen / 2^((i-1)*8)) % (2^(i*8)));
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 return loadstring(chunk:sub(1, 12)..table.concat(b)..newname
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 ..chunk:sub(13+intsize+oldlen, -1));
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 ]];
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
10
d5a4aabb104b Some logging level changes
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
155 print_verbose("Packing modules...");
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 for _, module in ipairs(modules) do
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 local modulename, path = module.name, base_path..module.path;
10
d5a4aabb104b Some logging level changes
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
158 print_debug("Packing "..modulename.." ("..path..")...");
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local data, err = fetch.filesystem(path);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 if (not data) and module.url then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 print_debug("Fetching: ".. module.url:gsub("%?", module.path))
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 data, err = fetch.http(module.url:gsub("%?", module.path));
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if data then
21
27c2e279f0f3 Fix to work with modules which call module(...)
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
165 f:write("package.preload['", modulename, "'] = (function (...)\n");
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 f:write(data);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 f:write("end)\n");
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if enable_debug then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 f:write(string.format("package.preload[%q] = ___rename_chunk(package.preload[%q], %q);\n\n",
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 modulename, modulename, "@"..path));
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 print_err("Couldn't pack module '"..modulename.."': "..err);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
178 if #resources > 0 then
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
179 print_verbose("Packing resources...")
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
180 f:write("do local resources = {};\n");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
181 for _, resource in ipairs(resources) do
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
182 local name, path = resource.name, resource.path;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
183 local res_file, err = io.open(base_path..path);
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
184 if not res_file then
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
185 print_err("Couldn't load resource: "..tostring(err));
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
186 os.exit(1);
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
187 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
188 local data = res_file:read("*a");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
189 local maxequals = 0;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
190 data:gsub("(=+)", function (equals_string) maxequals = math.max(maxequals, #equals_string); end);
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
191
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
192 f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "[");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
193 f:write(data);
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
194 f:write("]", string.rep("=", maxequals+1), "];");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
195 end
19
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
196 if opts.enable_virtual_io then
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
197 local vio = require_resource("vio");
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
198 if not vio then
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
199 print_err("Virtual IO requested but is not enabled in this build of squish");
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
200 else
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
201 -- Insert vio library
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
202 f:write(vio, "\n")
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
203 -- Override io.open to use vio if opening a resource
20
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
204 f:write[[local io_open, io_lines = io.open, io.lines; function io.open(fn, mode)
19
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
205 if not resources[fn] then
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
206 return io_open(fn, mode);
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
207 else
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
208 return vio.open(resources[fn]);
20
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
209 end end
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
210 function io.lines(fn)
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
211 if not resources[fn] then
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
212 return io_lines(fn);
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
213 else
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
214 return vio.open(resources[fn]):lines()
22
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
215 end end
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
216 local _dofile = dofile;
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
217 function dofile(fn)
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
218 if not resources[fn] then
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
219 return _dofile(fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
220 else
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
221 return assert(loadstring(resources[fn]))();
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
222 end end
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
223 local _loadfile = loadfile;
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
224 function loadfile(fn)
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
225 if not resources[fn] then
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
226 return _loadfile(fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
227 else
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
228 return loadstring(resources[fn], "@"..fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
229 end end ]]
19
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
230 end
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
231 end
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
232 f:write[[function require_resource(name) return resources[name] or error("resource '"..tostring(name).."' not found"); end end ]]
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
233 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
234
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 print_debug("Finalising...")
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 for _, fn in pairs(main_files) do
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 local fin, err = io.open(base_path..fn);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 if not fin then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 print_err("Failed to open "..fn..": "..err);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 f:write((fin:read("*a"):gsub("^#.-\n", "")));
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 fin:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 f:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 print_info("OK!");

mercurial