File Coverage

blib/lib/Dist/Zilla/Plugin/Babble.pm
Criterion Covered Total %
statement 51 57 89.4
branch 2 6 33.3
condition n/a
subroutine 12 12 100.0
pod 0 4 0.0
total 65 79 82.2


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Babble;
2             $Dist::Zilla::Plugin::Babble::VERSION = '0.004';
3 1     1   3494759 use Carp 'croak';
  1         3  
  1         96  
4 1     1   9 use Moose;
  1         3  
  1         9  
5 1     1   7164 use MooseX::Types::Moose qw/ArrayRef Str/;
  1         7  
  1         14  
6 1     1   5698 use MooseX::Types::Perl qw/StrictVersionStr/;
  1         3  
  1         8  
7 1     1   2254 use List::Util 'any';
  1         2  
  1         942  
8              
9             with 'Dist::Zilla::Role::FileMunger',
10             'Dist::Zilla::Role::FileFinderUser' => {
11             default_finders => [ ':InstallModules', ':ExecFiles' ],
12             },
13             'Dist::Zilla::Role::FileFinderUser' => {
14             method => 'found_test_files',
15             finder_arg_names => [ 'test_finder' ],
16             default_finders => [ ':TestFiles' ],
17             },
18             'Dist::Zilla::Role::FileFinderUser' => {
19             method => 'found_configure_files',
20             finder_arg_names => [ 'configure_finder' ],
21             default_finders => [],
22             };
23              
24              
25             sub mvp_multivalue_args {
26             return qw/files plugins/;
27             }
28              
29             sub mvp_aliases {
30             return {
31 2     2 0 444 file => 'files',
32             plugin => 'plugins',
33             };
34             }
35              
36             has files => (
37             isa => ArrayRef[Str],
38             traits => ['Array'],
39             lazy => 1,
40             default => sub { [] },
41             handles => {
42             files => 'elements',
43             },
44             );
45              
46             has extra_plugins => (
47             isa => ArrayRef[Str],
48             traits => ['Array'],
49             lazy => 1,
50             default => sub { [] },
51             handles => {
52             extra_plugins => 'elements',
53             },
54             );
55              
56             has plugins => (
57             isa => ArrayRef[Str],
58             traits => ['Array'],
59             lazy => 1,
60             builder => '_build_plugins',
61             handles => {
62             plugins => 'elements',
63             },
64             );
65              
66             has filter_plugins => (
67             isa => ArrayRef[Str],
68             traits => ['Array'],
69             lazy => 1,
70             default => sub { [] },
71             handles => {
72             filter_plugins => 'elements',
73             },
74             );
75              
76              
77             my %supported_since = (
78             '::CoreSignatures' => '5.028',
79             '::State' => '5.010',
80             '::DefinedOr' => '5.010',
81             '::PostfixDeref' => '5.020',
82             '::SubstituteAndReturn' => '5.014',
83             '::Ellipsis' => '5.012',
84             '::PackageBlock' => '5.014',
85             '::PackageVersion' => '5.012',
86             );
87              
88             sub _build_plugins {
89 1     1   3 my $self = shift;
90 1         9 my @plugins = grep { $supported_since{$_} > $self->for_version } keys %supported_since;
  8         239  
91 1         52 my %filter = map { $_ => 1 } $self->filter_plugins;
  0         0  
92 1         3 @plugins = grep { not $filter{$_} } @plugins;
  8         48  
93 1         51 push @plugins, $self->extra_plugins;
94 1         39 return \@plugins;
95             }
96              
97             has for_version => (
98             is => 'ro',
99             isa => StrictVersionStr,
100             default => '5.008',
101             );
102              
103             has transformer => (
104             is => 'ro',
105             init_arg => undef,
106             lazy => 1,
107             builder => '_build_transformer',
108             );
109              
110             sub _build_transformer {
111 2     2   7 my $self = shift;
112 2         603 require Babble::PluginChain;
113 2         79745 my $pc = Babble::PluginChain->new;
114 2         1230 $pc->add_plugin($_) for $self->plugins;
115 2         43946 return $pc;
116             }
117              
118             sub get_files {
119 2     2 0 5 my $self = shift;
120 2         5 my @result = @{ $self->found_files };
  2         11  
121 2         3205 push @result, grep { $_->name =~ /\.t/ } @{ $self->found_test_files };
  0         0  
  2         11  
122 2         1222 return @result;
123             }
124              
125             sub munge_files {
126 2     2 0 91966 my $self = shift;
127              
128 2         19 $self->log("Starting to babble source");
129 2 50       724 if (my %filename = map { $_ => 1 } $self->files) {
  0         0  
130 0         0 foreach my $file (@{ $self->zilla->files }) {
  0         0  
131 0 0       0 $self->munge_file($file) if $filename{$file->name};
132             }
133             }
134             else {
135 2         12 $self->munge_file($_) for $self->get_files;;
136             }
137 2         21 $self->log("Finished babbling source");
138              
139 2         1015 return;
140             }
141              
142             sub munge_file {
143 2     2 0 10 my ($self, $file) = @_;
144 2         11 my $content = $file->content;
145 2         2235 my $pc = $self->transformer;
146 2 50       5 eval {
147 2         10 $file->content($pc->transform_document($content));
148 2         1564202 1;
149             } or croak 'Could not munge ' . $file->name . ': ' . $@;
150 2         17 return;
151             }
152              
153             __PACKAGE__->meta->make_immutable;
154 1     1   8 no Moose;
  1         3  
  1         15  
155              
156             'For Science®';
157              
158             # ABSTRACT: EXPERIMENTAL Automatic Babble substitution in Dist::Zilla
159              
160              
161             # vim: ts=4 sts=4 sw=4 noet :
162              
163             __END__
164              
165             =pod
166              
167             =encoding UTF-8
168              
169             =head1 NAME
170              
171             Dist::Zilla::Plugin::Babble - EXPERIMENTAL Automatic Babble substitution in Dist::Zilla
172              
173             =head1 VERSION
174              
175             version 0.004
176              
177             =head1 SYNOPSIS
178              
179             [Babble]
180            
181             #or
182            
183             [Babble]
184             for_version = 5.010 # don't translate defined-or or state
185            
186             #or
187            
188             [Babble]
189             plugin = ::CoreSignatures # only signature transformation
190              
191             =head1 DESCRIPTION
192              
193             Are you in need of Damian's Mad Science™? Are you lacking Matt's Voodoo? Do you want to mix all of the complexities of dzil's transformations with that stack? Then you're in the right place.
194              
195             This module translates various modern Perl langauge features into their older equivalents using L<Babble|Babble>. It is highly experimental, and comes with no warranties whatsoever.
196              
197             By default it transforms code to be able to compile on perl C<5.008>.
198              
199             =head1 AUTHOR
200              
201             Leon Timmermans <leont@cpan.org>
202              
203             =head1 COPYRIGHT AND LICENSE
204              
205             This software is copyright (c) 2018 by Leon Timmermans.
206              
207             This is free software; you can redistribute it and/or modify it under
208             the same terms as the Perl 5 programming language system itself.
209              
210             =cut