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