Apply a patch contributed by chocolateboy (RT #54383) to allow "use Mouse::Tiny VERSION"
[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/
4b7f0b17 35 && !/Test/ # only for testing
0d2fef85 36 && !/Spec/ # has no functionality
37 && !/TypeRegistry/ # deprecated
38 && !/\bouse/ # ouse.pm
cae2769d 39 },
f1db776d 40 no_chdir => 1,
41}, 'lib');
42
43my $mouse_tiny = '';
44
0d2fef85 45for my $file (uniq
43e49d93 46 'lib/Mouse/PurePerl.pm',
0d2fef85 47 'lib/Mouse/Exporter.pm',
48 'lib/Mouse/Util.pm',
49 'lib/Mouse/Meta/TypeConstraint.pm',
50 'lib/Mouse/Util/TypeConstraints.pm',
51 sort @files) {
52
f1db776d 53 my $contents = slurp $file;
54
55 $contents =~ s/__END__\b.*//s; # remove documentation
56 $contents =~ s/1;\n*$//; # remove success indicator
f1db776d 57
c10b6ba9 58 $contents =~ s{^( (?:[ ]{4})+ )}{ "\t" x (length($1) / 4) }xmsge; # spaces to tabs
59
7801fbf0 60 $mouse_tiny .= "BEGIN{ # $file\n";
f1db776d 61 $mouse_tiny .= $contents;
0d2fef85 62 $mouse_tiny .= "}\n";
f1db776d 63}
64
0d2fef85 65open my $handle, ">$MouseTinyFile";
f1db776d 66
0d2fef85 67print { $handle } << "EOF";
68# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
69#
70# ANY CHANGES MADE HERE WILL BE LOST!
f1db776d 71
0d2fef85 72EOF
73
74print { $handle } << 'EOF';
f1db776d 75# if regular Mouse is loaded, bail out
2da0530e 76unless ($INC{'Mouse.pm'}) {
c10b6ba9 77# tell Perl we already have all of the Mouse files loaded:
f1db776d 78EOF
79
80for my $file (@files) {
81 (my $inc = $file) =~ s{^lib/}{};
0d2fef85 82 printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
f1db776d 83}
84
0d2fef85 85print { $handle } << 'EOF';
86eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
0d2fef85 87EOF
88
f1db776d 89print { $handle } "\n# and now their contents\n\n";
90
91print { $handle } $mouse_tiny;
2da0530e 92
0d2fef85 93print { $handle } << 'EOF';
94END_OF_TINY
95 die $@ if $@;
96} # unless Mouse.pm is loaded
97EOF
2da0530e 98
47c73874 99print { $handle } << "EOF";
2da0530e 100package Mouse::Tiny;
2da0530e 101
47c73874 102our \$VERSION = '$Mouse::Spec::VERSION';
103
bc69ee88 104Mouse::Exporter->setup_import_methods(also => 'Mouse');
105
0d2fef85 1061;
2da0530e 107EOF
108
0d2fef85 109close $handle;
e3ca3ad0 110
111print "done.\n";