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