Make Mouse::Tiny smaller
[gitmo/Mouse.git] / tool / generate-mouse-tiny.pl
CommitLineData
f1db776d 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use File::Find;
dc8e9f34 5use Fatal qw(open close);
6#use File::Slurp 'slurp';
7#use List::MoreUtils 'uniq';
8#use autodie;
9
e3ca3ad0 10print "Generate Mouse::Tiny ...\n";
11
dc8e9f34 12sub slurp {
13 open my $in, '<', $_[0];
14 local $/;
15 return scalar <$in>;
16}
17sub uniq{
18 my %seen;
19 return grep{ !$seen{$_}++ } @_;
20}
f1db776d 21
0d2fef85 22require 'lib/Mouse/Spec.pm';
23
24my $MouseTinyFile = shift || 'lib/Mouse/Tiny.pm';
f1db776d 25
26my @files;
27
28find({
cae2769d 29 wanted => sub {
30 push @files, $_
31 if -f $_
0d2fef85 32 && /\.pm$/
26cf84ad 33 && !/Squirrel/
0d2fef85 34 && !/Tiny/
35 && !/Spec/ # has no functionality
36 && !/TypeRegistry/ # deprecated
37 && !/\bouse/ # ouse.pm
cae2769d 38 },
f1db776d 39 no_chdir => 1,
40}, 'lib');
41
42my $mouse_tiny = '';
43
0d2fef85 44for my $file (uniq
43e49d93 45 'lib/Mouse/PurePerl.pm',
0d2fef85 46 'lib/Mouse/Exporter.pm',
47 'lib/Mouse/Util.pm',
48 'lib/Mouse/Meta/TypeConstraint.pm',
49 'lib/Mouse/Util/TypeConstraints.pm',
50 sort @files) {
51
f1db776d 52 my $contents = slurp $file;
53
54 $contents =~ s/__END__\b.*//s; # remove documentation
55 $contents =~ s/1;\n*$//; # remove success indicator
f1db776d 56
c10b6ba9 57 $contents =~ s{^( (?:[ ]{4})+ )}{ "\t" x (length($1) / 4) }xmsge; # spaces to tabs
58
7801fbf0 59 $mouse_tiny .= "BEGIN{ # $file\n";
f1db776d 60 $mouse_tiny .= $contents;
0d2fef85 61 $mouse_tiny .= "}\n";
f1db776d 62}
63
0d2fef85 64open my $handle, ">$MouseTinyFile";
f1db776d 65
0d2fef85 66print { $handle } << "EOF";
67# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
68#
69# ANY CHANGES MADE HERE WILL BE LOST!
f1db776d 70
0d2fef85 71EOF
72
73print { $handle } << 'EOF';
f1db776d 74# if regular Mouse is loaded, bail out
2da0530e 75unless ($INC{'Mouse.pm'}) {
c10b6ba9 76# tell Perl we already have all of the Mouse files loaded:
f1db776d 77EOF
78
79for my $file (@files) {
80 (my $inc = $file) =~ s{^lib/}{};
0d2fef85 81 printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
f1db776d 82}
83
0d2fef85 84print { $handle } << 'EOF';
85eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
0d2fef85 86EOF
87
f1db776d 88print { $handle } "\n# and now their contents\n\n";
89
90print { $handle } $mouse_tiny;
2da0530e 91
0d2fef85 92print { $handle } << 'EOF';
93END_OF_TINY
94 die $@ if $@;
95} # unless Mouse.pm is loaded
96EOF
2da0530e 97
98print { $handle } << 'EOF';
99package Mouse::Tiny;
2da0530e 100
bc69ee88 101Mouse::Exporter->setup_import_methods(also => 'Mouse');
102
0d2fef85 1031;
2da0530e 104EOF
105
0d2fef85 106close $handle;
e3ca3ad0 107
108print "done.\n";