make_squishy

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