Print messages in generating Mouse::Tiny
[gitmo/Mouse.git] / author / 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
45 'lib/Mouse/Exporter.pm',
46 'lib/Mouse/Util.pm',
47 'lib/Mouse/Meta/TypeConstraint.pm',
48 'lib/Mouse/Util/TypeConstraints.pm',
49 sort @files) {
50
f1db776d 51 my $contents = slurp $file;
52
53 $contents =~ s/__END__\b.*//s; # remove documentation
54 $contents =~ s/1;\n*$//; # remove success indicator
f1db776d 55
0d2fef85 56 $mouse_tiny .= "BEGIN{ # #file\n";
f1db776d 57 $mouse_tiny .= $contents;
0d2fef85 58 $mouse_tiny .= "}\n";
f1db776d 59}
60
0d2fef85 61open my $handle, ">$MouseTinyFile";
f1db776d 62
0d2fef85 63print { $handle } << "EOF";
64# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
65#
66# ANY CHANGES MADE HERE WILL BE LOST!
f1db776d 67
0d2fef85 68EOF
69
70print { $handle } << 'EOF';
f1db776d 71# if regular Mouse is loaded, bail out
2da0530e 72unless ($INC{'Mouse.pm'}) {
f1db776d 73EOF
74
75for my $file (@files) {
76 (my $inc = $file) =~ s{^lib/}{};
0d2fef85 77 printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
f1db776d 78}
79
0d2fef85 80print { $handle } << 'EOF';
81eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
82
83# tell Perl we already have all of the Mouse files loaded:
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;
2da0530e 102EOF
103
0d2fef85 104close $handle;
e3ca3ad0 105
106print "done.\n";