forked from noctux/philote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatpack.pl
executable file
·184 lines (139 loc) · 3.63 KB
/
fatpack.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use File::Basename qw/dirname basename/;
use Storable qw/dclone/;
use Data::Compare;
use Data::Dumper;
my $outdir;
my $truncate = '';
my $inputfile;
my $usage='';
my @whitelist=('io');
GetOptions('output=s' => \$outdir, 'truncate' => \$truncate, 'input=s' => \$inputfile, 'help' => \$usage, 'whitelist=s' => \@whitelist);
@whitelist = uniq(split(/,/, join(',', @whitelist)));
my $whitelisted = join '|', map{ "^" . $_ } map{quotemeta} sort {length($b)<=>length($a)}@whitelist;
my $whitelistre = qr/($whitelisted)/;
if ( $usage
|| ((!$outdir) || (! -d $outdir))
|| ((!$inputfile) || (! -f $inputfile))) {
print <<"EOF";
$0 --input <file.lua> --output <directory> [--truncate] [--whitelist <module>,<module>]
--help Print this help message
--input file to fatpack. Expects all libs to reside in basedir(file)
--output output directory for fatpacked files
--truncate unconditionally override in outdir (default=false)
--whitelist modules not to fatpack
EOF
exit -1;
}
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
}
sub slurp {
my $filename = shift;
return do {
local $/;
open my $file, '<:encoding(UTF-8)', $filename or die "Failed to open file $filename";
<$file>;
};
}
sub extractIncludes {
# get all requires from a given modules
my $filecontent = shift;
my @requires = ($filecontent =~ m/require\((.*?)\)/g);
@requires = map {sanitizeRequire($_)} @requires;
return \@requires;
}
sub sanitizeRequire {
my $require = shift;
# remove all quotes and whitespaces from requires
$require =~ s/^['"\s]+|['"\s]+$//g;
return $require;
}
sub fix {
my $op = shift;
my $old = shift;
my $new = $old;
do {
$old = $new;
$new = dclone($old);
$new = $op->($new);
} while (!Compare($old, $new));
return $new;
}
sub getModule {
my $includedir = shift;
my $modulename = shift;
return slurp("$includedir/$modulename.lua");
}
sub fatpack {
my $mainmodule = shift;
my $modules = shift;
# split the module in shebang+header and the actual code
$modules->{$mainmodule} =~ /^(?<head>(#!.*\n|--.*\n)+)(?<tail>(.|\n)*)/;
my $head = $+{head};
my $tail = $+{tail};
# Build the fatpacked script
my $packed = $head;
$packed .= <<"EOF";
do
local _ENV = _ENV
EOF
foreach my $module (sort keys %{$modules}) {
next if ($module eq $mainmodule);
my $effcontent = %{$modules}{$module};
# Strip the shebang
$effcontent =~ s/^#!.*\n//g;
$packed .= <<"EOF";
package.preload["$module"] = function( ... )
local arg = _G.arg;
_ENV = _ENV;
$effcontent
end
EOF
}
$packed .= "\nend\n";
$packed .= $tail;
return $packed;
}
sub unslurp {
my $dst = shift;
my $content = shift;
if (-f $dst && !$truncate) {
print STDERR "$dst already exists and --truncate was not specified\n";
exit(1);
}
open(my $fh, '>', $dst) or die "Could not open file '$dst'";
print $fh $content;
close $fh;
}
sub main {
my $includedir = dirname($inputfile);
my $inputmodule = basename($inputfile, ".lua");
my $modules = {
$inputmodule => getModule($includedir, $inputmodule),
};
my $process = sub {
my $modules = shift;
my @new = ();
# gather all includes in all modules
while(my ($key, $value) = each %{$modules}) {
my $extracted = extractIncludes($value);
push @new, @$extracted;
}
# Add all new modules and their contents
foreach my $module (@new) {
if (! exists $modules->{$module} && $module !~ $whitelistre) {
$modules->{$module} = getModule($includedir, $module);
}
}
return $modules;
};
$modules = fix($process, $modules);
my $fat = fatpack($inputmodule, $modules);
unslurp("$outdir/$inputmodule.lua", $fat);
}
main();