make_squishy

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

mercurial