File: generate-mouse-tiny.pl

package info (click to toggle)
libmouse-perl 2.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,156 kB
  • sloc: perl: 14,613; ansic: 213; makefile: 8
file content (114 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Fatal qw(open close);
use lib ('.', 'lib');
#use File::Slurp 'slurp';
#use List::MoreUtils 'uniq';
#use autodie;

print "Generate Mouse::Tiny ...\n";

sub slurp {
    open my $in, '<', $_[0];
    local $/;
    return scalar <$in>;
}
sub uniq{
    my %seen;
    return grep{ !$seen{$_}++ } @_;
}

require Mouse::Spec;

my $MouseTinyFile = shift || 'lib/Mouse/Tiny.pm';

my @files;

find({
    wanted => sub {
        push @files, $_
            if -f $_
            &&  /\.pm$/
            && !/Squirrel/
            && !/Tiny/
            && !/Test/         # only for testing
            && !/Spec/         # has no functionality
            && !/TypeRegistry/ # deprecated
            && !/\bouse/       # ouse.pm
    },
    no_chdir => 1,
}, 'lib');
@files = sort @files;

my $mouse_tiny = '';

for my $file (uniq
        'lib/Mouse/PurePerl.pm',
        'lib/Mouse/Exporter.pm',
        'lib/Mouse/Util.pm',
        'lib/Mouse/Meta/TypeConstraint.pm',
        'lib/Mouse/Util/TypeConstraints.pm',
            @files) {

    my $contents = slurp $file;

    $contents =~ s/__END__\b.*//s;          # remove documentation
    $contents =~ s/1;\n*$//;                # remove success indicator

    $contents =~ s{^( (?:[ ]{4})+ )}{ "\t" x (length($1) / 4) }xmsge; # spaces to tabs

    $mouse_tiny .= "BEGIN{ # $file\n";
    $mouse_tiny .= $contents;
    $mouse_tiny .= "}\n";
}

open my $handle, ">$MouseTinyFile";

print { $handle } << "EOF";
# This file was generated by $0 from Mouse $Mouse::Spec::VERSION.
#
# ANY CHANGES MADE HERE WILL BE LOST!
use strict;
use warnings;
EOF

print { $handle } << 'EOF';
# if regular Mouse is loaded, bail out
unless ($INC{'Mouse.pm'}) {
# tell Perl we already have all of the Mouse files loaded:
EOF

for my $file (sort @files) {
    (my $inc = $file) =~ s{^lib/}{};
    printf { $handle } "%-45s = __FILE__;\n", "\$INC{'$inc'}";
}

print { $handle } << 'EOF';
eval sprintf("#line %d %s\n", __LINE__, __FILE__) . <<'END_OF_TINY';
EOF

print { $handle } "\n# and now their contents\n\n";

print { $handle } $mouse_tiny;

print { $handle } << 'EOF';
END_OF_TINY
    die $@ if $@;
} # unless Mouse.pm is loaded
EOF

print { $handle } << "EOF";
package Mouse::Tiny;

use version; our \$VERSION = version->declare('$Mouse::Spec::VERSION');

Mouse::Exporter->setup_import_methods(also => 'Mouse');

1;
EOF

close $handle;

print "done.\n";