0484cdba735fc6b230e5e614c532e89032c6e1db
[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     if -e 'lib/Mouse/Tiny.pm';
11
12 my @files;
13
14 find({
15     wanted => sub {
16         push @files, $_
17             if -f $_
18             && !/Squirrel/
19             && !/TypeRegistory/
20             && !/\bouse/
21             && !/\.sw[po]$/
22     },
23     no_chdir => 1,
24 }, 'lib');
25
26 my $mouse_tiny = '';
27
28 for my $file (uniq 'lib/Mouse/Util.pm', sort @files) {
29     my $contents = slurp $file;
30
31     $contents =~ s/__END__\b.*//s;          # remove documentation
32     $contents =~ s/1;\n*$//;                # remove success indicator
33
34     $contents =~ s/^use Mouse\S*\s*\n//mg;  # we're already loading everything
35     $contents =~ s/^use (Mouse\S*)\s*(.+);/BEGIN { $1->import($2) }/mg;
36
37     $mouse_tiny .= $contents;
38 }
39
40 open my $handle, '>lib/Mouse/Tiny.pm' or die "Can't write lib/Mouse/Tiny.pm: $!";
41
42 print { $handle } << 'EOF';
43 # THIS FILE IS AUTOGENERATED!
44
45 # if regular Mouse is loaded, bail out
46 unless ($INC{'Mouse.pm'}) {
47 eval <<'END_OF_TINY';
48
49 # tell Perl we already have all of the Mouse files loaded:
50 EOF
51
52 for my $file (@files) {
53     (my $inc = $file) =~ s{^lib/}{};
54     print { $handle } "\$INC{'$inc'} = __FILE__;\n";
55 }
56
57 print { $handle } "\n# and now their contents\n\n";
58
59 print { $handle } $mouse_tiny;
60
61 print { $handle } "END_OF_TINY\n} #unless\n\n";
62
63 print { $handle } << 'EOF';
64 package Mouse::Tiny;
65 use base 'Mouse';
66
67 EOF
68
69 print { $handle } "1;\n\n";
70