Commit | Line | Data |
f1db776d |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use File::Find; |
dc8e9f34 |
5 | use Fatal qw(open close); |
6 | #use File::Slurp 'slurp'; |
7 | #use List::MoreUtils 'uniq'; |
8 | #use autodie; |
9 | |
e3ca3ad0 |
10 | print "Generate Mouse::Tiny ...\n"; |
11 | |
dc8e9f34 |
12 | sub slurp { |
13 | open my $in, '<', $_[0]; |
14 | local $/; |
15 | return scalar <$in>; |
16 | } |
17 | sub uniq{ |
18 | my %seen; |
19 | return grep{ !$seen{$_}++ } @_; |
20 | } |
f1db776d |
21 | |
0d2fef85 |
22 | require 'lib/Mouse/Spec.pm'; |
23 | |
24 | my $MouseTinyFile = shift || 'lib/Mouse/Tiny.pm'; |
f1db776d |
25 | |
26 | my @files; |
27 | |
28 | find({ |
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 | |
43 | my $mouse_tiny = ''; |
44 | |
0d2fef85 |
45 | for 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 |
65 | open my $handle, ">$MouseTinyFile"; |
f1db776d |
66 | |
0d2fef85 |
67 | print { $handle } << "EOF"; |
68 | # This file was generated by $0 from Mouse $Mouse::Spec::VERSION. |
69 | # |
70 | # ANY CHANGES MADE HERE WILL BE LOST! |
a4b15169 |
71 | use strict; |
72 | use warnings; |
0d2fef85 |
73 | EOF |
74 | |
75 | print { $handle } << 'EOF'; |
f1db776d |
76 | # if regular Mouse is loaded, bail out |
2da0530e |
77 | unless ($INC{'Mouse.pm'}) { |
c10b6ba9 |
78 | # tell Perl we already have all of the Mouse files loaded: |
f1db776d |
79 | EOF |
80 | |
81 | for my $file (@files) { |
82 | (my $inc = $file) =~ s{^lib/}{}; |
0d2fef85 |
83 | printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}"; |
f1db776d |
84 | } |
85 | |
0d2fef85 |
86 | print { $handle } << 'EOF'; |
87 | eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY'; |
0d2fef85 |
88 | EOF |
89 | |
f1db776d |
90 | print { $handle } "\n# and now their contents\n\n"; |
91 | |
92 | print { $handle } $mouse_tiny; |
2da0530e |
93 | |
0d2fef85 |
94 | print { $handle } << 'EOF'; |
95 | END_OF_TINY |
96 | die $@ if $@; |
97 | } # unless Mouse.pm is loaded |
98 | EOF |
2da0530e |
99 | |
47c73874 |
100 | print { $handle } << "EOF"; |
2da0530e |
101 | package Mouse::Tiny; |
2da0530e |
102 | |
47c73874 |
103 | our \$VERSION = '$Mouse::Spec::VERSION'; |
104 | |
bc69ee88 |
105 | Mouse::Exporter->setup_import_methods(also => 'Mouse'); |
106 | |
0d2fef85 |
107 | 1; |
2da0530e |
108 | EOF |
109 | |
0d2fef85 |
110 | close $handle; |
e3ca3ad0 |
111 | |
112 | print "done.\n"; |