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