generate-mouse-tiny.pl must depend on only standard libraries
[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
10sub slurp {
11 open my $in, '<', $_[0];
12 local $/;
13 return scalar <$in>;
14}
15sub uniq{
16 my %seen;
17 return grep{ !$seen{$_}++ } @_;
18}
f1db776d 19
0d2fef85 20require 'lib/Mouse/Spec.pm';
21
22my $MouseTinyFile = shift || 'lib/Mouse/Tiny.pm';
f1db776d 23
24my @files;
25
26find({
cae2769d 27 wanted => sub {
28 push @files, $_
29 if -f $_
0d2fef85 30 && /\.pm$/
26cf84ad 31 && !/Squirrel/
0d2fef85 32 && !/Tiny/
33 && !/Spec/ # has no functionality
34 && !/TypeRegistry/ # deprecated
35 && !/\bouse/ # ouse.pm
cae2769d 36 },
f1db776d 37 no_chdir => 1,
38}, 'lib');
39
40my $mouse_tiny = '';
41
0d2fef85 42for my $file (uniq
43 'lib/Mouse/Exporter.pm',
44 'lib/Mouse/Util.pm',
45 'lib/Mouse/Meta/TypeConstraint.pm',
46 'lib/Mouse/Util/TypeConstraints.pm',
47 sort @files) {
48
f1db776d 49 my $contents = slurp $file;
50
51 $contents =~ s/__END__\b.*//s; # remove documentation
52 $contents =~ s/1;\n*$//; # remove success indicator
f1db776d 53
0d2fef85 54 $mouse_tiny .= "BEGIN{ # #file\n";
f1db776d 55 $mouse_tiny .= $contents;
0d2fef85 56 $mouse_tiny .= "}\n";
f1db776d 57}
58
0d2fef85 59open my $handle, ">$MouseTinyFile";
f1db776d 60
0d2fef85 61print { $handle } << "EOF";
62# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
63#
64# ANY CHANGES MADE HERE WILL BE LOST!
f1db776d 65
0d2fef85 66EOF
67
68print { $handle } << 'EOF';
f1db776d 69# if regular Mouse is loaded, bail out
2da0530e 70unless ($INC{'Mouse.pm'}) {
f1db776d 71EOF
72
73for my $file (@files) {
74 (my $inc = $file) =~ s{^lib/}{};
0d2fef85 75 printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
f1db776d 76}
77
0d2fef85 78print { $handle } << 'EOF';
79eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
80
81# tell Perl we already have all of the Mouse files loaded:
82EOF
83
f1db776d 84print { $handle } "\n# and now their contents\n\n";
85
86print { $handle } $mouse_tiny;
2da0530e 87
0d2fef85 88print { $handle } << 'EOF';
89END_OF_TINY
90 die $@ if $@;
91} # unless Mouse.pm is loaded
92EOF
2da0530e 93
94print { $handle } << 'EOF';
95package Mouse::Tiny;
2da0530e 96
bc69ee88 97Mouse::Exporter->setup_import_methods(also => 'Mouse');
98
0d2fef85 991;
2da0530e 100EOF
101
0d2fef85 102close $handle;