Thu, 31 Dec 2015 20:08:01 +0000
squish.lua: Remove accidentally-committed debugging
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 | |
50 | 3 | if not arg[1] or arg[1]:match("%-%-?help") or arg[1]:match("^[/-]%?$") then |
4 | print "make_squishy - Generate squishy files for your projects" | |
5 | print "" | |
6 | print(" Usage: "..arg[0].." FILE1 FILE2 FILE..."); | |
7 | print "" | |
8 | print "make_squishy will scan the given files for require() calls, and convert the " | |
9 | print "found modules into a squishy file, 'squishy.new'. make_squishy automatically scans " | |
10 | print "the modules it finds, looking for further dependencies. To disable this, use " | |
11 | print "the --no-recursion option." | |
12 | return; | |
13 | end | |
14 | ||
15 | ||
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | 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
|
17 | 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
|
18 | 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
|
19 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | 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
|
21 | 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
|
22 | 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
|
23 | 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
|
24 | 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
|
25 | 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
|
26 | 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
|
27 | else |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | 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
|
29 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | else |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | 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
|
32 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | 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
|
36 | 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
|
37 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | 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
|
39 | 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
|
40 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | 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
|
47 | 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
|
48 | 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
|
49 | 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
|
50 | squishy:close(); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | 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
|
52 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | 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
|
59 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | 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
|
62 | 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
|
63 | |
69
df181dcc02b7
make_squishy: Fix traceback when file doesn't contain any path component
Matthew Wild <mwild1@gmail.com>
parents:
68
diff
changeset
|
64 | local base_path = (files[1]:match("^(.-)"..LUA_DIRSEP.."[^"..LUA_DIRSEP.."]*$") or ".").."/"; |
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | 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
|
67 | 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
|
68 | 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
|
69 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | end):gsub("/%./", "/"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | 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
|
72 | 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
|
73 | 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
|
74 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | end):gsub("/%./", "/"); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | 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
|
79 | for line in io.lines(scanfile) do |
84
14f827efadf2
make_squishy: Match require calls at the start of a line (thanks R D)
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
80 | for _, module in (" "..line):gmatch("[^%w_]require%s*%(?([\"'])(.-)%1") do |
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | 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
|
82 | local binary; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | 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
|
84 | local filename = resolve_module(module, package_path); |
51
728f8579abc5
make_squishy: Disable resolution of binary modules for now
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
85 | if false and not filename then |
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | binary = true; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | 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
|
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 | if not filename then |
70
0fd43db11abc
make_squishy: Improve 'Couldn't resolve' message
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
90 | print_info("Couldn't resolve module '"..module.."' to a file (required in "..scanfile..")"); |
68
16d59655c817
make_squishy: Fix traceback on modules that couldn't be found on the filesystem (thanks Valerio)
Matthew Wild <mwild1@gmail.com>
parents:
52
diff
changeset
|
91 | elseif opts.recursion ~= false and not scanned_files[filename] then |
52 | 92 | scanned_files[filename] = true; |
43
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | 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
|
94 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | 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
|
96 | 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
|
97 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | end |
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 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | 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
|
105 | 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
|
106 | 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
|
107 | 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
|
108 | 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
|
109 | 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
|
110 | 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
|
111 | f:close(); |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | return c; |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | 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
|
116 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 | |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | 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
|
119 | 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
|
120 | end |
3407f006b9cb
make_squishy: Automatically scan sources looking for modules and generate a squishy file
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | 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
|
122 | 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
|
123 | 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
|
124 | 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
|
125 | 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
|
126 | end |