Mon, 27 Jul 2009 04:33:39 +0100
-g == --debug
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | #!/usr/bin/env lua |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local short_opts = { v = "verbose", vv = "very_verbose", o = "output", q = "quiet", qq = "very_quiet", f = "force" } |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local files, opts = {}, {}; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | local scanned_files, modules = {}, {}; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | for _, opt in ipairs(arg) do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | if opt:match("^%-") then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | local name = opt:match("^%-%-?([^%s=]+)()") |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | name = (short_opts[name] or name):gsub("%-+", "_"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | if name:match("^no_") then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | name = name:sub(4, -1); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | opts[name] = false; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | else |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | opts[name] = opt:match("=(.*)$") or true; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | else |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | table.insert(files, opt); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | if opts.very_verbose then opts.verbose = true; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | if opts.very_quiet then opts.quiet = true; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | local noprint = function () end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | local print_err, print_info, print_verbose, print_debug = noprint, noprint, noprint, noprint; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | if not opts.very_quiet then print_err = print; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | if not opts.quiet then print_info = print; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | if opts.verbose or opts.very_verbose then print_verbose = print; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | if opts.very_verbose then print_debug = print; end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | if not opts.force then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | local squishy = io.open("squishy.new", "r"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | if squishy then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | print_err("There is already a squishy file in this directory, use -f to force overwriting it"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | squishy:close(); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | os.exit(1); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | local squishy, err = io.open("squishy.new", "w+"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | if not squishy then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | print_err("Couldn't open squishy file for writing: "..tostring(err)); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | os.exit(1); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | local LUA_DIRSEP = package.config:sub(1,1); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | local LUA_PATH_MARK = package.config:sub(5,5); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | local base_path = files[1]:match("^(.-)"..LUA_DIRSEP.."[^"..LUA_DIRSEP.."]*$").."/"; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | local package_path = package.path:gsub("[^;]+", function (path) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | if not path:match("^%"..LUA_DIRSEP) then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | return base_path..path; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | end):gsub("/%./", "/"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | local package_cpath = package.cpath:gsub("[^;]+", function (path) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | if not path:match("^%"..LUA_DIRSEP) then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | return base_path..path; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | end):gsub("/%./", "/"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | function scan_file(outfile, scanfile) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | for line in io.lines(scanfile) do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | for _, module in line:gmatch("[^%w_]require%s*%(?([\"'])(.-)%1") do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | if not modules[module] then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | local binary; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | modules[module] = true; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | local filename = resolve_module(module, package_path); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | if not filename then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | binary = true; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | filename = resolve_module(module, package_cpath); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | if not filename then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | print_info("Couldn't resolve "..module.." (required in "..scanfile..")"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | if not scanned_files[filename] then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | table.insert(files, filename); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | if filename then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | outfile:write((binary and "Binary" or ""), string.format([[Module %q %q]], module, filename:gsub("^"..base_path:gsub("%p", "%%%1"), "")), "\n"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | function resolve_module(name, path) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | name = name:gsub("%.", LUA_DIRSEP); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | for c in path:gmatch("[^;]+") do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | c = c:gsub("%"..LUA_PATH_MARK, name); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | print_debug("Testing: "..c) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | local f = io.open(c); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | if f then |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | f:close(); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | return c; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | return nil; -- not found |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | for _, filename in ipairs(files) do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | squishy:write(string.format([[Main %q]], filename:gsub("^"..base_path:gsub("%p", "%%%1"), "")), "\n"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | squishy:write("\n"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | for _, filename in ipairs(files) do |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | scanned_files[filename] = true; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 | print_verbose("Found:", filename); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | scan_file(squishy, filename) |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | end |