Fix author/generate-mouse-tiny.pl. Now Mouse::Tiny is generated in the "make" command
[gitmo/Mouse.git] / author / generate-mouse-tiny.pl
index 4799507..9e4120d 100755 (executable)
@@ -6,8 +6,9 @@ use File::Slurp 'slurp';
 use List::MoreUtils 'uniq';
 use autodie;
 
-unlink 'lib/Mouse/Tiny.pm'
-    if -e 'lib/Mouse/Tiny.pm';
+require 'lib/Mouse/Spec.pm';
+
+my $MouseTinyFile = shift || 'lib/Mouse/Tiny.pm';
 
 my @files;
 
@@ -15,58 +16,76 @@ find({
     wanted => sub {
         push @files, $_
             if -f $_
+            &&  /\.pm$/
             && !/Squirrel/
-            && !/TypeRegistory/
-            && !/\bouse/
-            && !/\.sw[po]$/
+            && !/Tiny/
+            && !/Spec/         # has no functionality
+            && !/TypeRegistry/ # deprecated
+            && !/\bouse/       # ouse.pm
     },
     no_chdir => 1,
 }, 'lib');
 
 my $mouse_tiny = '';
 
-for my $file (uniq 'lib/Mouse/Util.pm', sort @files) {
+for my $file (uniq
+        'lib/Mouse/Exporter.pm',
+        'lib/Mouse/Util.pm',
+        'lib/Mouse/Meta/TypeConstraint.pm',
+        'lib/Mouse/Util/TypeConstraints.pm',
+            sort @files) {
+
     my $contents = slurp $file;
 
     $contents =~ s/__END__\b.*//s;          # remove documentation
     $contents =~ s/1;\n*$//;                # remove success indicator
 
-    $contents =~ s/^use Mouse\S*\s*\n//mg;  # we're already loading everything
-    $contents =~ s/^use (Mouse\S*)\s*(.+);/BEGIN { $1->import($2) }/mg;
-
+    $mouse_tiny .= "BEGIN{ # #file\n";
     $mouse_tiny .= $contents;
+    $mouse_tiny .= "}\n";
 }
 
-open my $handle, '>lib/Mouse/Tiny.pm' or die "Can't write lib/Mouse/Tiny.pm: $!";
+open my $handle, ">$MouseTinyFile";
 
-print { $handle } << 'EOF';
-# THIS FILE IS AUTOGENERATED!
+print { $handle } << "EOF";
+# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
+#
+# ANY CHANGES MADE HERE WILL BE LOST!
 
+EOF
+
+print { $handle } << 'EOF';
 # if regular Mouse is loaded, bail out
 unless ($INC{'Mouse.pm'}) {
-eval <<'END_OF_TINY';
-
-# tell Perl we already have all of the Mouse files loaded:
 EOF
 
 for my $file (@files) {
     (my $inc = $file) =~ s{^lib/}{};
-    print { $handle } "\$INC{'$inc'} = __FILE__;\n";
+    printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
 }
 
+print { $handle } << 'EOF';
+eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
+
+# tell Perl we already have all of the Mouse files loaded:
+EOF
+
 print { $handle } "\n# and now their contents\n\n";
 
 print { $handle } $mouse_tiny;
 
-print { $handle } "END_OF_TINY\n} #unless\n\n";
+print { $handle } << 'EOF';
+END_OF_TINY
+    die $@ if $@;
+} # unless Mouse.pm is loaded
+EOF
 
 print { $handle } << 'EOF';
 package Mouse::Tiny;
-use base 'Mouse';
 
 Mouse::Exporter->setup_import_methods(also => 'Mouse');
 
+1;
 EOF
 
-print { $handle } "1;\n\n";
-
+close $handle;