Mon, 27 Jul 2009 03:59:39 +0100
Only add a module to the list once, no matter how many times it is added in the squishy file
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 | |
29
0737a3bcf10b
Map print() to print_verbose()
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
32 | print = print_verbose; |
0737a3bcf10b
Map print() to print_verbose()
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
33 | |
4
9806107f59ea
squish.lua: Use --enable-debug flag to decide whether to enable debug options
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
34 | 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
|
35 | |
11
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
36 | 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
|
37 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | -- 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
|
39 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | end |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | local i = #modules+1; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | 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
|
47 | 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
|
48 | return function (path) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | modules[i].path = path; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | end |
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 | |
11
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
53 | function Resource(name, path) |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
54 | local i = #resources+1; |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
55 | resources[i] = { name = name, path = path or name }; |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
56 | return function (path) |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
57 | resources[i].path = path; |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
58 | end |
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 | |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | function AutoFetchURL(url) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | ___fetch_url = url; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | function Main(fn) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | 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
|
67 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | function Output(fn) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | out_fn = fn; |
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 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | function Option(name) |
31
84b653f78e5f
Rewrite '-' to '_' in Option command
Matthew Wild <mwild1@gmail.com>
parents:
29
diff
changeset
|
74 | name = name:gsub("%-", "_"); |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | 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
|
76 | opts[name] = true; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | return function (value) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | opts[name] = value; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | else |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | return function () end; |
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 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | |
13
b93f52cbe253
Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
85 | function GetOption(name) |
b93f52cbe253
Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
86 | return opts[name:gsub('%-', '_')]; |
b93f52cbe253
Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
87 | end |
b93f52cbe253
Add GetOption function for squishy files
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
88 | |
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
|
89 | function Message(message) |
24
73ac55bcc19a
Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
90 | 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
|
91 | print_info(message); |
73ac55bcc19a
Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
92 | 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
|
93 | 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
|
94 | |
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
|
95 | function Error(message) |
24
73ac55bcc19a
Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
96 | 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
|
97 | print_err(message); |
73ac55bcc19a
Make Message and Error commands obey user's verbosity options
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
98 | 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
|
99 | 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
|
100 | |
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
|
101 | 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
|
102 | 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
|
103 | end |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | -- -- -- -- -- -- -- --- -- -- -- -- -- -- -- -- |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | 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
|
107 | 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
|
108 | 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
|
109 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | 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
|
111 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | if not ok then |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | 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
|
114 | os.exit(1); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | |
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
|
117 | 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
|
118 | 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
|
119 | 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
|
120 | 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
|
121 | 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
|
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 | 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
|
124 | |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | local fetch = {}; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | function fetch.filesystem(path) |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | 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
|
128 | 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
|
129 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | 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
|
131 | f:close(); |
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 | return data; |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 | |
16
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
136 | if opts.use_http then |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
137 | function fetch.http(url) |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
138 | local http = require "socket.http"; |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
139 | |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
140 | local body, status = http.request(url); |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
141 | if status == 200 then |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
142 | return body; |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
143 | end |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
144 | 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
|
145 | end |
16
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
146 | else |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
147 | function fetch.http(url) |
aaf1b38007d8
Add --use-http option, and default to off
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
148 | 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
|
149 | end |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 | print_info("Writing "..out_fn.."..."); |
32
d78440c40faa
Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents:
31
diff
changeset
|
153 | local f, err = io.open(out_fn, "w+"); |
d78440c40faa
Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents:
31
diff
changeset
|
154 | if not f then |
d78440c40faa
Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents:
31
diff
changeset
|
155 | 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
|
156 | os.exit(1); |
d78440c40faa
Warn when output file not writeable
Matthew Wild <mwild1@gmail.com>
parents:
31
diff
changeset
|
157 | end |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 | if opts.executable then |
28
99ec02c56716
Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents:
25
diff
changeset
|
160 | if opts.executable == true then |
99ec02c56716
Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents:
25
diff
changeset
|
161 | 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
|
162 | else |
99ec02c56716
Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents:
25
diff
changeset
|
163 | f:write(opts.executable, "\n"); |
99ec02c56716
Allow user or squishy file to specify custom shebang
Matthew Wild <mwild1@gmail.com>
parents:
25
diff
changeset
|
164 | end |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 | if enable_debug then |
34 | 168 | f:write(require_resource("squish.debug")); |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 | |
10 | 171 | 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
|
172 | 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
|
173 | local modulename, path = module.name, base_path..module.path; |
10 | 174 | 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
|
175 | 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
|
176 | 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
|
177 | 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
|
178 | 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
|
179 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 | if data then |
21
27c2e279f0f3
Fix to work with modules which call module(...)
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
181 | 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
|
182 | f:write(data); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 | f:write("end)\n"); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 | if enable_debug then |
34 | 185 | f:write(string.format("package.preload[%q] = ___adjust_chunk(package.preload[%q], %q);\n\n", |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 | modulename, modulename, "@"..path)); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 | else |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 | 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
|
190 | os.exit(1); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 | |
11
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
194 | if #resources > 0 then |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
195 | print_verbose("Packing resources...") |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
196 | f:write("do local resources = {};\n"); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
197 | for _, resource in ipairs(resources) do |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
198 | local name, path = resource.name, resource.path; |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
199 | local res_file, err = io.open(base_path..path); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
200 | if not res_file then |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
201 | print_err("Couldn't load resource: "..tostring(err)); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
202 | os.exit(1); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
203 | end |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
204 | local data = res_file:read("*a"); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
205 | local maxequals = 0; |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
206 | 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
|
207 | |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
208 | f:write(("resources[%q] = ["):format(name), string.rep("=", maxequals+1), "["); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
209 | f:write(data); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
210 | f:write("]", string.rep("=", maxequals+1), "];"); |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
211 | end |
19
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
212 | if opts.enable_virtual_io then |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
213 | local vio = require_resource("vio"); |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
214 | if not vio then |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
215 | 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
|
216 | else |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
217 | -- Insert vio library |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
218 | f:write(vio, "\n") |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
219 | -- 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
|
220 | 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
|
221 | if not resources[fn] then |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
222 | return io_open(fn, mode); |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
223 | else |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
224 | return vio.open(resources[fn]); |
20
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
225 | end end |
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
226 | function io.lines(fn) |
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
227 | if not resources[fn] then |
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
228 | return io_lines(fn); |
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
229 | else |
1766b5a287e1
Add io.lines support for the virtual io
Matthew Wild <mwild1@gmail.com>
parents:
19
diff
changeset
|
230 | 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
|
231 | end end |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
232 | local _dofile = dofile; |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
233 | function dofile(fn) |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
234 | if not resources[fn] then |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
235 | return _dofile(fn); |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
236 | else |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
237 | return assert(loadstring(resources[fn]))(); |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
238 | end end |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
239 | local _loadfile = loadfile; |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
240 | function loadfile(fn) |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
241 | if not resources[fn] then |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
242 | return _loadfile(fn); |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
243 | else |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
244 | return loadstring(resources[fn], "@"..fn); |
cb14d8c0d0f7
Add support for virtual IO in dofile and loadfile
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
245 | end end ]] |
19
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
246 | end |
f72a0f535301
Add virtual io support for accessing resources
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
247 | end |
11
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
248 | 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
|
249 | end |
10bb5834c6db
Support for adding 'resources'
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
250 | |
0
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 | print_debug("Finalising...") |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 | 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
|
253 | 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
|
254 | if not fin then |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 | 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
|
256 | os.exit(1); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 | else |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 | 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
|
259 | fin:close(); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 | end |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 | f:close(); |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 | |
b119b560ca3d
squish, because libraries don't have to be tricky to package!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 | print_info("OK!"); |