This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
squish.lua
executable file
·326 lines (285 loc) · 8.19 KB
/
squish.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env lua
-- Initialise LuaRocks if present
pcall(require, "luarocks.require");
local short_opts = { v = "verbose", vv = "very_verbose", o = "output", q = "quiet", qq = "very_quiet", g = "debug" }
local opts = { use_http = false };
for _, opt in ipairs(arg) do
if opt:match("^%-") then
local name = opt:match("^%-%-?([^%s=]+)()")
name = (short_opts[name] or name):gsub("%-+", "_");
if name:match("^no_") then
name = name:sub(4, -1);
opts[name] = false;
else
opts[name] = opt:match("=(.*)$") or true;
end
else
base_path = opt;
end
end
if opts.very_verbose then opts.verbose = true; end
if opts.very_quiet then opts.quiet = true; end
local noprint = function () end
local print_err, print_info, print_verbose, print_debug = noprint, noprint, noprint, noprint;
if not opts.very_quiet then print_err = print; end
if not opts.quiet then print_info = print; end
if opts.verbose or opts.very_verbose then print_verbose = print; end
if opts.very_verbose then print_debug = print; end
print = print_verbose;
local modules, main_files, resources = {}, {}, {};
-- Functions to be called from squishy file --
function Module(name)
if modules[name] then
print_verbose("Ignoring duplicate module definition for "..name);
return function () end
end
local i = #modules+1;
modules[i] = { name = name, url = ___fetch_url };
modules[name] = modules[i];
return function (path)
modules[i].path = path;
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
function Main(fn)
table.insert(main_files, fn);
end
function Output(fn)
if opts.output == nil then
out_fn = fn;
end
end
function Option(name)
name = name:gsub("%-", "_");
if opts[name] == nil then
opts[name] = true;
return function (value)
opts[name] = value;
end
else
return function () end;
end
end
function GetOption(name)
return opts[name:gsub('%-', '_')];
end
function Message(message)
if not opts.quiet then
print_info(message);
end
end
function Error(message)
if not opts.very_quiet then
print_err(message);
end
end
function Exit()
os.exit(1);
end
-- -- -- -- -- -- -- --- -- -- -- -- -- -- -- --
base_path = (base_path or "."):gsub("/$", "").."/"
squishy_file = base_path .. "squishy";
out_fn = opts.output;
local ok, err = pcall(dofile, squishy_file);
if not ok then
print_err("Couldn't read squishy file: "..err);
os.exit(1);
end
if not out_fn then
print_err("No output file specified by user or squishy file");
os.exit(1);
elseif #main_files == 0 and #modules == 0 and #resources == 0 then
print_err("No files, modules or resources. Not going to generate an empty file.");
os.exit(1);
end
local fetch = {};
function fetch.filesystem(path)
local f, err = io.open(path);
if not f then return false, err; end
local data = f:read("*a");
f:close();
return data;
end
if opts.use_http then
function fetch.http(url)
local http = require "socket.http";
local body, status = http.request(url);
if status == 200 then
return body;
end
return false, "HTTP status code: "..tostring(status);
end
else
function fetch.http(url)
return false, "Module not found. Re-squish with --use-http option to fetch it from "..url;
end
end
print_info("Writing "..out_fn.."...");
local f, err = io.open(out_fn, "w+");
if not f then
print_err("Couldn't open output file: "..tostring(err));
os.exit(1);
end
if opts.executable then
if opts.executable == true then
f:write("#!/usr/bin/env lua\n");
else
f:write(opts.executable, "\n");
end
end
print_verbose("Resolving modules...");
do
local LUA_DIRSEP = package.config:sub(1,1);
local LUA_PATH_MARK = package.config:sub(5,5);
local package_path = package.path:gsub("[^;]+", function (path)
if not path:match("^%"..LUA_DIRSEP) then
return base_path..path;
end
end):gsub("/%./", "/");
local package_cpath = package.cpath:gsub("[^;]+", function (path)
if not path:match("^%"..LUA_DIRSEP) then
return base_path..path;
end
end):gsub("/%./", "/");
function resolve_module(name, path)
name = name:gsub("%.", LUA_DIRSEP);
for c in path:gmatch("[^;]+") do
c = c:gsub("%"..LUA_PATH_MARK, name);
print_debug("Looking for "..c)
local f = io.open(c);
if f then
print_debug("Found!");
f:close();
return c;
end
end
return nil; -- not found
end
for i, module in ipairs(modules) do
if not module.path then
module.path = resolve_module(module.name, package_path);
if not module.path then
print_err("Couldn't resolve module: "..module.name);
else
-- Strip base_path from resolved path
module.path = module.path:gsub("^"..base_path:gsub("%p", "%%%1"), "");
end
end
end
end
print_verbose("Packing modules...");
for _, module in ipairs(modules) do
local modulename, path = module.name, module.path;
if module.path:sub(1,1) ~= "/" then
path = base_path..module.path;
end
print_debug("Packing "..modulename.." ("..path..")...");
local data, err = fetch.filesystem(path);
if (not data) and module.url then
local url = module.url:gsub("%?", module.path);
print_debug("Fetching: ".. url)
if url:match("^https?://") then
data, err = fetch.http(url);
elseif url:match("^file://") or url:match("^[/%.]") then
local dataf, dataerr = io.open((url:gsub("^file://", "")));
if dataf then
data, err = dataf:read("*a");
dataf:close();
else
data, err = nil, dataerr;
end
end
end
if data then
if not opts.debug then
f:write("package.preload['", modulename, "'] = (function (...)\n");
f:write(data);
f:write(" end)\n");
else
f:write("package.preload['", modulename, "'] = assert(loadstring(\n");
f:write(("%q\n"):format(data));
f:write(", ", ("%q"):format("@"..path), "))\n");
end
else
print_err("Couldn't pack module '"..modulename.."': "..(err or "unknown error... path to module file correct?"));
os.exit(1);
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, "rb");
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] = %q"):format(name, data));
--[[ f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "[");
f:write(data);
f:write("]", string.rep("=", maxequals+1), "];"); ]]
end
if opts.virtual_io then
local vio = require_resource("vio");
if not vio then
print_err("Virtual IO requested but is not enabled in this build of squish");
else
-- Insert vio library
f:write(vio, "\n")
-- Override standard functions to use vio if opening a resource
f:write[[local io_open, io_lines = io.open, io.lines; function io.open(fn, mode)
if not resources[fn] then
return io_open(fn, mode);
else
return vio.open(resources[fn]);
end end
function io.lines(fn)
if not resources[fn] then
return io_lines(fn);
else
return vio.open(resources[fn]):lines()
end end
local _dofile = dofile;
function dofile(fn)
if not resources[fn] then
return _dofile(fn);
else
return assert(loadstring(resources[fn]))();
end end
local _loadfile = loadfile;
function loadfile(fn)
if not resources[fn] then
return _loadfile(fn);
else
return loadstring(resources[fn], "@"..fn);
end end ]]
end
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);
if not fin then
print_err("Failed to open "..fn..": "..err);
os.exit(1);
else
f:write((fin:read("*a"):gsub("^#.-\n", "")));
fin:close();
end
end
f:close();
print_info("OK!");