squish.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 83
ff14c066a643
child 87
f39aceada1ad
permissions
-rwxr-xr-x

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

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
80
b70f75414c81 squish.lua: Initialise LuaRocks if available
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
3 -- Initialise LuaRocks if present
b70f75414c81 squish.lua: Initialise LuaRocks if available
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
4 pcall(require, "luarocks.require");
b70f75414c81 squish.lua: Initialise LuaRocks if available
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
5
46
c09caa0317cc -g == --debug
Matthew Wild <mwild1@gmail.com>
parents: 41
diff changeset
6 local short_opts = { v = "verbose", vv = "very_verbose", o = "output", q = "quiet", qq = "very_quiet", g = "debug" }
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
7 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
8
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 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
10 if opt:match("^%-") then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 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
12 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
13 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
14 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
15 opts[name] = false;
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 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
18 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 base_path = opt;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 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 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
25 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
26
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local noprint = function () end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 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
29
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 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
31 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
32 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
33 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
34
29
0737a3bcf10b Map print() to print_verbose()
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
35 print = print_verbose;
0737a3bcf10b Map print() to print_verbose()
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
36
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
37 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
38
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- 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
40
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function Module(name)
37
cb8b48b08e1e Only add a module to the list once, no matter how many times it is added in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
42 if modules[name] then
cb8b48b08e1e Only add a module to the list once, no matter how many times it is added in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
43 print_verbose("Ignoring duplicate module definition for "..name);
cb8b48b08e1e Only add a module to the list once, no matter how many times it is added in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
44 return function () end
cb8b48b08e1e Only add a module to the list once, no matter how many times it is added in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
45 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local i = #modules+1;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 modules[i] = { name = name, url = ___fetch_url };
37
cb8b48b08e1e Only add a module to the list once, no matter how many times it is added in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
48 modules[name] = modules[i];
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return function (path)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 modules[i].path = path;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
54 function Resource(name, path)
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
55 local i = #resources+1;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
56 resources[i] = { name = name, path = path or name };
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
57 return function (path)
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
58 resources[i].path = path;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
59 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
60 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
61
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function AutoFetchURL(url)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 ___fetch_url = url;
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 Main(fn)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 function Output(fn)
55
feb7832393cd Don't let squishy file override the user's specified output file, if one
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
71 if opts.output == nil then
feb7832393cd Don't let squishy file override the user's specified output file, if one
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
72 out_fn = fn;
feb7832393cd Don't let squishy file override the user's specified output file, if one
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
73 end
0
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
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 function Option(name)
31
84b653f78e5f Rewrite '-' to '_' in Option command
Matthew Wild <mwild1@gmail.com>
parents: 29
diff changeset
77 name = name:gsub("%-", "_");
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 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
79 opts[name] = true;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return function (value)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 opts[name] = value;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return function () end;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
13
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
88 function GetOption(name)
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
89 return opts[name:gsub('%-', '_')];
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
90 end
b93f52cbe253 Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
91
23
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
92 function Message(message)
24
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
93 if not opts.quiet then
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
94 print_info(message);
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
95 end
23
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
96 end
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
97
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
98 function Error(message)
24
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
99 if not opts.very_quiet then
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
100 print_err(message);
73ac55bcc19a Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
101 end
23
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
102 end
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
103
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
104 function Exit()
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
105 os.exit(1);
e3b923ccadb1 Support for Message and Error in squishy files to show messages to the user, and Exit to halt squishing
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
106 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 -- -- -- -- -- -- -- --- -- -- -- -- -- -- -- --
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 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
110 squishy_file = base_path .. "squishy";
25
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
111 out_fn = opts.output;
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 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
114
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 if not ok then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 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
117 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
25
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
120 if not out_fn then
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
121 print_err("No output file specified by user or squishy file");
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
122 os.exit(1);
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
123 elseif #main_files == 0 and #modules == 0 and #resources == 0 then
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
124 print_err("No files, modules or resources. Not going to generate an empty file.");
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
125 os.exit(1);
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
126 end
a4972a690064 Complain if output file not specified, or if squish would generate an empty file
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
127
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 local fetch = {};
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 function fetch.filesystem(path)
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 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
131 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
132
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 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
134 f:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 return data;
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
139 if opts.use_http then
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
140 function fetch.http(url)
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
141 local http = require "socket.http";
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
142
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
143 local body, status = http.request(url);
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
144 if status == 200 then
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
145 return body;
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
146 end
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
147 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
148 end
16
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
149 else
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
150 function fetch.http(url)
aaf1b38007d8 Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
151 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
152 end
0
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
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 print_info("Writing "..out_fn.."...");
32
d78440c40faa Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
156 local f, err = io.open(out_fn, "w+");
d78440c40faa Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
157 if not f then
d78440c40faa Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
158 print_err("Couldn't open output file: "..tostring(err));
d78440c40faa Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
159 os.exit(1);
d78440c40faa Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
160 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 if opts.executable then
28
99ec02c56716 Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
163 if opts.executable == true then
99ec02c56716 Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
164 f:write("#!/usr/bin/env lua\n");
99ec02c56716 Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
165 else
99ec02c56716 Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
166 f:write(opts.executable, "\n");
99ec02c56716 Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
167 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
38
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
170 print_verbose("Resolving modules...");
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
171 do
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
172 local LUA_DIRSEP = package.config:sub(1,1);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
173 local LUA_PATH_MARK = package.config:sub(5,5);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
174
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
175 local package_path = package.path:gsub("[^;]+", function (path)
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
176 if not path:match("^%"..LUA_DIRSEP) then
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
177 return base_path..path;
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
178 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
179 end):gsub("/%./", "/");
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
180 local package_cpath = package.cpath:gsub("[^;]+", function (path)
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
181 if not path:match("^%"..LUA_DIRSEP) then
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
182 return base_path..path;
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
183 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
184 end):gsub("/%./", "/");
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
185
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
186 function resolve_module(name, path)
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
187 name = name:gsub("%.", LUA_DIRSEP);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
188 for c in path:gmatch("[^;]+") do
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
189 c = c:gsub("%"..LUA_PATH_MARK, name);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
190 print_debug("Looking for "..c)
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
191 local f = io.open(c);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
192 if f then
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
193 print_debug("Found!");
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
194 f:close();
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
195 return c;
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
196 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
197 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
198 return nil; -- not found
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
199 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
200
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
201 for i, module in ipairs(modules) do
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
202 if not module.path then
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
203 module.path = resolve_module(module.name, package_path);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
204 if not module.path then
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
205 print_err("Couldn't resolve module: "..module.name);
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
206 else
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
207 -- Strip base_path from resolved path
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
208 module.path = module.path:gsub("^"..base_path:gsub("%p", "%%%1"), "");
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
209 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
210 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
211 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
212 end
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
213
903128a42604 Support for resolving the paths of modules when they are not explicitly specified
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
214
10
d5a4aabb104b Some logging level changes
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
215 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
216 for _, module in ipairs(modules) do
71
e27ab4492cdd squish.lua: Only prepend project base path for relative paths specified in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
217 local modulename, path = module.name, module.path;
e27ab4492cdd squish.lua: Only prepend project base path for relative paths specified in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
218 if module.path:sub(1,1) ~= "/" then
e27ab4492cdd squish.lua: Only prepend project base path for relative paths specified in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
219 path = base_path..module.path;
e27ab4492cdd squish.lua: Only prepend project base path for relative paths specified in the squishy file
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
220 end
10
d5a4aabb104b Some logging level changes
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
221 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
222 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
223 if (not data) and module.url then
81
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
224 local url = module.url:gsub("%?", module.path);
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
225 print_debug("Fetching: ".. url)
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
226 if url:match("^https?://") then
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
227 data, err = fetch.http(url);
82
4d6a976c3bb7 squish.lua: AutoFetch file paths can now begin with / or . too
Matthew Wild <mwild1@gmail.com>
parents: 81
diff changeset
228 elseif url:match("^file://") or url:match("^[/%.]") then
81
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
229 local dataf, dataerr = io.open((url:gsub("^file://", "")));
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
230 if dataf then
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
231 data, err = dataf:read("*a");
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
232 dataf:close();
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
233 else
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
234 data, err = nil, dataerr;
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
235 end
44f2945d09cf squish.lua: Support for AutoFetch to use file:// URLs
Matthew Wild <mwild1@gmail.com>
parents: 80
diff changeset
236 end
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 if data then
86
2322f7932064 Remove shebangs from modules, if any (thanks Markus Stenberg)
Matthew Wild <mwild1@gmail.com>
parents: 83
diff changeset
239 data = data:gsub("^#[^\r\n]*\r?\n", ""); -- Remove shebang if any (or we can't concat)
83
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
240 if not opts.debug then
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
241 f:write("package.preload['", modulename, "'] = (function (...)\n");
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
242 f:write(data);
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
243 f:write(" end)\n");
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
244 else
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
245 f:write("package.preload['", modulename, "'] = assert(loadstring(\n");
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
246 f:write(("%q\n"):format(data));
ff14c066a643 Change to behaviour of --debug/-g - at the (temporary) loss of squishing modules -g will now give real tracebacks again with original filenames
Matthew Wild <mwild1@gmail.com>
parents: 82
diff changeset
247 f:write(", ", ("%q"):format("@"..path), "))\n");
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 else
67
2fc48318b8bb squish: Handle nil errors when can't pack a module
Matthew Wild <mwild1@gmail.com>
parents: 58
diff changeset
250 print_err("Couldn't pack module '"..modulename.."': "..(err or "unknown error... path to module file correct?"));
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
255 if #resources > 0 then
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
256 print_verbose("Packing resources...")
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
257 f:write("do local resources = {};\n");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
258 for _, resource in ipairs(resources) do
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
259 local name, path = resource.name, resource.path;
53
bd189ae0de00 Fixes for handling binary resources
Matthew Wild <mwild1@gmail.com>
parents: 46
diff changeset
260 local res_file, err = io.open(base_path..path, "rb");
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
261 if not res_file then
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
262 print_err("Couldn't load resource: "..tostring(err));
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
263 os.exit(1);
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
264 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
265 local data = res_file:read("*a");
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
266 local maxequals = 0;
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
267 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
268
53
bd189ae0de00 Fixes for handling binary resources
Matthew Wild <mwild1@gmail.com>
parents: 46
diff changeset
269 f:write(("resources[%q] = %q"):format(name, data));
bd189ae0de00 Fixes for handling binary resources
Matthew Wild <mwild1@gmail.com>
parents: 46
diff changeset
270 --[[ f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "[");
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
271 f:write(data);
53
bd189ae0de00 Fixes for handling binary resources
Matthew Wild <mwild1@gmail.com>
parents: 46
diff changeset
272 f:write("]", string.rep("=", maxequals+1), "];"); ]]
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
273 end
41
4ff9ecb6f1f6 Rename --enable-virtual-io to --virtual-io
Matthew Wild <mwild1@gmail.com>
parents: 39
diff changeset
274 if opts.virtual_io then
19
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
275 local vio = require_resource("vio");
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
276 if not vio then
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
277 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
278 else
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
279 -- Insert vio library
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
280 f:write(vio, "\n")
54
90bf9ea1aaf0 squish.lua: Small fix to comment
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
281 -- Override standard functions 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
282 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
283 if not resources[fn] then
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
284 return io_open(fn, mode);
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
285 else
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
286 return vio.open(resources[fn]);
20
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
287 end end
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
288 function io.lines(fn)
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
289 if not resources[fn] then
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
290 return io_lines(fn);
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
291 else
1766b5a287e1 Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
292 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
293 end end
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
294 local _dofile = dofile;
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
295 function dofile(fn)
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
296 if not resources[fn] then
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
297 return _dofile(fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
298 else
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
299 return assert(loadstring(resources[fn]))();
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
300 end end
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
301 local _loadfile = loadfile;
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
302 function loadfile(fn)
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
303 if not resources[fn] then
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
304 return _loadfile(fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
305 else
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
306 return loadstring(resources[fn], "@"..fn);
cb14d8c0d0f7 Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
307 end end ]]
19
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
308 end
f72a0f535301 Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
309 end
11
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
310 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
311 end
10bb5834c6db Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
312
0
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 print_debug("Finalising...")
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 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
315 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
316 if not fin then
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 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
318 os.exit(1);
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 else
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 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
321 fin:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 end
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 f:close();
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326
b119b560ca3d squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 print_info("OK!");

mercurial