Fix Mouse::Exporter to work with Mouse::Tiny
[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
e0541d44 9unlink 'lib/Mouse/Tiny.pm'
10 if -e 'lib/Mouse/Tiny.pm';
f1db776d 11
12my @files;
13
14find({
cae2769d 15 wanted => sub {
16 push @files, $_
17 if -f $_
26cf84ad 18 && !/Squirrel/
7ca5c5fb 19 && !/TypeRegistory/
cae2769d 20 && !/\bouse/
26cf84ad 21 && !/\.sw[po]$/
cae2769d 22 },
f1db776d 23 no_chdir => 1,
24}, 'lib');
25
26my $mouse_tiny = '';
27
28for 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
f1db776d 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
f1db776d 40open my $handle, '>lib/Mouse/Tiny.pm' or die "Can't write lib/Mouse/Tiny.pm: $!";
41
42print { $handle } << 'EOF';
f1db776d 43# THIS FILE IS AUTOGENERATED!
44
45# if regular Mouse is loaded, bail out
2da0530e 46unless ($INC{'Mouse.pm'}) {
448fbe32 47eval <<'END_OF_TINY';
f1db776d 48
49# tell Perl we already have all of the Mouse files loaded:
50EOF
51
52for my $file (@files) {
53 (my $inc = $file) =~ s{^lib/}{};
54 print { $handle } "\$INC{'$inc'} = __FILE__;\n";
55}
56
57print { $handle } "\n# and now their contents\n\n";
58
59print { $handle } $mouse_tiny;
2da0530e 60
448fbe32 61print { $handle } "END_OF_TINY\n} #unless\n\n";
2da0530e 62
63print { $handle } << 'EOF';
64package Mouse::Tiny;
65use base 'Mouse';
66
bc69ee88 67Mouse::Exporter->setup_import_methods(also => 'Mouse');
68
2da0530e 69EOF
70
f1db776d 71print { $handle } "1;\n\n";
72