autodie in generate-mouse-tiny.pl
[gitmo/Mouse.git] / author / generate-mouse-tiny.pl
CommitLineData
f1db776d 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use File::Find;
5use File::Slurp 'slurp';
6use List::MoreUtils 'uniq';
49540190 7use autodie;
f1db776d 8
9unlink 'lib/Mouse/Tiny.pm';
10
11my @files;
12
13find({
cae2769d 14 wanted => sub {
15 push @files, $_
16 if -f $_
26cf84ad 17 && !/Squirrel/
cae2769d 18 && !/\bouse/
26cf84ad 19 && !/\.sw[po]$/
cae2769d 20 },
f1db776d 21 no_chdir => 1,
22}, 'lib');
23
24my $mouse_tiny = '';
25
26for my $file (uniq 'lib/Mouse/Util.pm', sort @files) {
27 my $contents = slurp $file;
28
29 $contents =~ s/__END__\b.*//s; # remove documentation
30 $contents =~ s/1;\n*$//; # remove success indicator
f1db776d 31
32 $contents =~ s/^use Mouse\S*\s*\n//mg; # we're already loading everything
33 $contents =~ s/^use (Mouse\S*)\s*(.+);/BEGIN { $1->import($2) }/mg;
34
35 $mouse_tiny .= $contents;
36}
37
f1db776d 38open my $handle, '>lib/Mouse/Tiny.pm' or die "Can't write lib/Mouse/Tiny.pm: $!";
39
40print { $handle } << 'EOF';
f1db776d 41# THIS FILE IS AUTOGENERATED!
42
43# if regular Mouse is loaded, bail out
2da0530e 44unless ($INC{'Mouse.pm'}) {
448fbe32 45eval <<'END_OF_TINY';
f1db776d 46
47# tell Perl we already have all of the Mouse files loaded:
48EOF
49
50for my $file (@files) {
51 (my $inc = $file) =~ s{^lib/}{};
52 print { $handle } "\$INC{'$inc'} = __FILE__;\n";
53}
54
55print { $handle } "\n# and now their contents\n\n";
56
57print { $handle } $mouse_tiny;
2da0530e 58
448fbe32 59print { $handle } "END_OF_TINY\n} #unless\n\n";
2da0530e 60
61print { $handle } << 'EOF';
62package Mouse::Tiny;
63use base 'Mouse';
64
65EOF
66
f1db776d 67print { $handle } "1;\n\n";
68