Add some document
[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
7801fbf0 57 $mouse_tiny .= "BEGIN{ # $file\n";
f1db776d 58 $mouse_tiny .= $contents;
0d2fef85 59 $mouse_tiny .= "}\n";
f1db776d 60}
61
0d2fef85 62open my $handle, ">$MouseTinyFile";
f1db776d 63
0d2fef85 64print { $handle } << "EOF";
65# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
66#
67# ANY CHANGES MADE HERE WILL BE LOST!
f1db776d 68
0d2fef85 69EOF
70
71print { $handle } << 'EOF';
f1db776d 72# if regular Mouse is loaded, bail out
2da0530e 73unless ($INC{'Mouse.pm'}) {
6e64f809 74 # tell Perl we already have all of the Mouse files loaded:
f1db776d 75EOF
76
77for my $file (@files) {
78 (my $inc = $file) =~ s{^lib/}{};
0d2fef85 79 printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
f1db776d 80}
81
0d2fef85 82print { $handle } << 'EOF';
83eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
0d2fef85 84EOF
85
f1db776d 86print { $handle } "\n# and now their contents\n\n";
87
88print { $handle } $mouse_tiny;
2da0530e 89
0d2fef85 90print { $handle } << 'EOF';
91END_OF_TINY
92 die $@ if $@;
93} # unless Mouse.pm is loaded
94EOF
2da0530e 95
96print { $handle } << 'EOF';
97package Mouse::Tiny;
2da0530e 98
bc69ee88 99Mouse::Exporter->setup_import_methods(also => 'Mouse');
100
0d2fef85 1011;
3654f7fa 102__END__
103
104=head1 NAME
105
106Mouse::Tiny - Mouse in a single file
107
108=head1 DESCRIPTION
109
4f21a0b3 110Mouse::Tiny is Mouse, but it is in a single file.
3654f7fa 111
112This is B<not> tiny. In fact, it requires a little more memory and time than Mouse.
113
e229d5a9 114Use Mouse directly unless you know what you are doing.
3654f7fa 115
4f21a0b3 116=head1 SEE ALSO
117
118L<Mouse>
119
3654f7fa 120=cut
121
2da0530e 122EOF
123
0d2fef85 124close $handle;
e3ca3ad0 125
126print "done.\n";