/
combine-perl.pl
executable file
·187 lines (158 loc) · 4.58 KB
/
combine-perl.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
185
186
#!/usr/bin/env perl
# Combine a perl program and modules it needs into one file
use strict;
use warnings;
use Getopt::Long;
use Module::CoreList;
use UNIVERSAL::require;
use constant TARGETED_MIN => 5.008;
# Include a warning in files we output, so it is obvious they are not the
# original source.
use constant COMBINED_WARNING => <<EOF;
#################################################
#
# This file was automatically generated by $0
# You should edit the original files, not this
# combined version.
#
# The original files are available at:
# http://github.com/monsieurvideo/get-flash-videos
#
#################################################
EOF
my $include = ".*";
# Note exclude takes precendence over include.
my $exclude = "^(HTML::Parser|HTML::Entities|Crypt::Rijndael|parent)";
# Our name, ends up in $::SCRIPT_NAME
my $name = "";
GetOptions(
"include|i=s" => \$include,
"exclude|e=s" => \$exclude,
"name|n=s" => \$name);
my %done;
for my $file(@ARGV) {
print process_file($file, 1);
}
sub process_file {
my($file, $main) = @_;
my $start = 1;
my $pre = "";
my $output = "";
$output .= "##{ $file\n{\n";
if(defined $main && $main) {
$output .= "package main;\n";
if($name) {
$output .= "\$::SCRIPT_NAME = '$name';\n";
$name = "";
}
}
open my $fh, "<", $file or die $!;
while(<$fh>) {
if(/^(?:require|\s*use) ([^ ;(]+)/) {
my $module = $1;
my $base = "";
if($module eq 'base') {
$base = $_;
/use\s+base\s+([^;]+)/ && ($module = eval $1);
$_ = "use $module;";
}
# Pass version dependencies through
$output .= $_, next unless $module =~ /^[A-Z]/i;
if(has_module($module) || $module =~ $exclude || $module !~ $include) {
if (/use\sExporter\s'import'/) {
$output .= "require Exporter;\nour \@ISA = qw(Exporter);\n";
} else {
$output .= $_;
}
} else {
if(/^\s*use [^ ;(]+((?: |\()[^;]*)?;/) {
my $params = defined $1 ? $1 : "";
$params =~ s/^(\s*[0-9.]+)\s*\(\s*\)\s*$/$1/;
$params =~ s/^\s*[0-9.]+(\s*qw\(\s*\))\s*$/$1/;
if($params !~ /^\s*\(\s*\)\s*$/) {
my @items = eval $params;
$output .= "BEGIN { $module->import($params); } # (added by $0)\n";
if(!@items) {
no strict 'refs';
$module->require;
@items = @{$module . "::EXPORT"};
}
if(@items) {
$output .= "BEGIN { no strict 'refs'; ";
@items = filter_imports($file, @items);
for my $item(@items) {
next if $item =~ /^\d/;
next if $item =~ /^RC_/;
next if $item =~ /^__PACKAGE__$/;
$output .= "*$item = \\&${module}::${item}; ";
}
$output .= "}\n";
}
}
} elsif(!/^\s*require /) {
die "Unable to handle use for: $module ($file:$.)\n";
}
unless($done{$module}++) {
my $module_file = module_to_file($module, "$file:$.");
my $module_path = module_to_path($module);
$pre .= "BEGIN { \$INC{'$module_path'}++; }\n";
$pre .= process_file($module_file);
}
}
if($base) {
$output .= $base;
}
} elsif (/^\s*our\s+@(EXPORT|EXPORT_OK).*;/) {
if (/is_/) {
$output .= $_;
} else {
chomp;
$output .= "BEGIN { ".$_." }\n";
}
} elsif(/^=(?!cut)\w+/) {
while(<$fh>) {
last if /^=cut/;
}
} elsif(/^__END__$/) {
last;
} elsif(/^__DATA__$/) {
die "Data sections not supported ($file:$.)\n";
} elsif($start && /^\s*(#|$)/) {
$pre .= COMBINED_WARNING if $. == 2;
$pre .= $_;
} elsif(/^__PACKAGE__$/) {
next;
} elsif(!/^\s*#/) {
$start = 0;
s/ HTTP::Status::RC_/ &HTTP::Status::RC_/;
$output .= $_;
}
}
$output .= "}\n##} $file\n";
return $pre . $output;
}
sub has_module {
my($module) = @_;
my $first = Module::CoreList->first_release($module);
return defined $first && $first <= TARGETED_MIN;
}
sub module_to_file {
my($module, $from) = @_;
my $file = module_to_path($module);
for my $dir(@INC) {
return "$dir/$file" if -f "$dir/$file";
}
die "Unable to find '$module' in \@INC (from $from)\n";
}
sub module_to_path {
my($file) = @_;
$file =~ s/::/\//g;
$file .= ".pm";
return $file;
}
sub filter_imports {
my($file, @imports) = @_;
open my $fh, "<", $file or die $!;
my $text = join "", <$fh>;
return grep { $text =~ /\Q$_\E/ } @imports;
}