File Coverage

blib/lib/Dist/Zilla/Plugin/Prepender.pm
Criterion Covered Total %
statement 36 39 92.3
branch 14 18 77.7
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 59 70 84.2


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dist-Zilla-Plugin-Prepender
3             #
4             # This software is copyright (c) 2009 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 1     1   2008408 use 5.008;
  1         2  
10 1     1   4 use strict;
  1         2  
  1         20  
11 1     1   4 use warnings;
  1         2  
  1         47  
12              
13             package Dist::Zilla::Plugin::Prepender;
14             # ABSTRACT: Prepend lines at the top of your perl files
15             $Dist::Zilla::Plugin::Prepender::VERSION = '2.004';
16 1     1   3 use Moose;
  1         1  
  1         7  
17              
18             with 'Dist::Zilla::Role::FileMunger';
19              
20              
21             # -- attributes
22              
23             # accept some arguments multiple times.
24 1     1 0 154 sub mvp_multivalue_args { qw{ line skip } }
25              
26             has copyright => (
27             is => 'ro',
28             default => 1,
29             );
30             has _lines => (
31             lazy => 1,
32             isa => 'ArrayRef[Str]',
33             init_arg => 'line',
34             traits => ['Array'],
35             handles => { _lines => 'elements' },
36             default => sub { [] },
37             );
38             has _skips => (
39             lazy => 1,
40             isa => 'ArrayRef[Str]',
41             init_arg => 'skip',
42             traits => ['Array'],
43             handles => { _skips => 'elements' },
44             default => sub { [] },
45             );
46              
47             our $DZIL_5 = eval { Dist::Zilla->VERSION(5.000) };
48              
49             # -- public methods
50              
51             sub munge_file {
52 5     5 0 157200 my ($self, $file) = @_;
53              
54 5         157 foreach my $skip ( $self->_skips ){
55 9 100       190 return if $file->name =~ $skip;
56             }
57              
58 4 50       214 if (not $file->does('Dist::Zilla::Role::MutableFile'))
59             {
60 0         0 $self->log_debug($file->name . ' is not a mutable type, skipping...');
61 0         0 return;
62             }
63 4 50 33     948 return if $DZIL_5 and $file->encoding eq 'bytes';
64 4 100       34 return $self->_munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i;
65 3 100       100 return $self->_munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
66 1         738 return;
67             }
68              
69             # -- private methods
70              
71             #
72             # $self->_munge_perl($file);
73             #
74             # munge content of perl $file: add stuff at the top of the file
75             #
76              
77             my %re = (
78             shebang => qr/^#!(?:.*)perl(?:$|\s.*$)/m,
79             vimmode => qr/^#\s*(?:vim?|ex):.*$/m,
80             emacsmode => qr/^#\s*-\*-[^\n]+?-\*-.*$/m,
81             );
82              
83             sub _munge_perl {
84 3     3   542 my ($self, $file) = @_;
85 3         5 my @prepend;
86              
87             # add copyright information if requested
88 3 50       78 if ( $self->copyright ) {
89 3         60 my @copyright = (
90             '',
91             "This file is part of " . $self->zilla->name,
92             '',
93             split(/\n/, $self->zilla->license->notice),
94             '',
95             );
96 3 100       5135 push @prepend, map { length($_) ? "# $_" : '#' } @copyright;
  24         52  
97             }
98              
99             # add hand-written lines to prepend
100 3         115 push @prepend, $self->_lines;
101 3         8 my $prepend = join "\n", @prepend;
102              
103             # insertion point depends if there's a shebang line
104 3         9 my $content = $file->content;
105 3 50       654 if ( $content =~ /\A$re{shebang}\n(?:$re{vimmode}|$re{emacsmode})/ ) {
    100          
106             # skip two lines
107 0         0 $content =~ s/\A([^\n]+\n[^\n]+\n)/$1$prepend\n/;
108             } elsif ( $content =~ /\A(?:$re{shebang}|$re{vimmode}|$re{emacsmode})/ ) {
109             # skip one line
110 2         14 $content =~ s/\n/\n$prepend\n/;
111             } else {
112 1         7 $content =~ s/\A/$prepend\n/;
113             }
114 3         10 $file->content($content);
115             }
116              
117             __PACKAGE__->meta->make_immutable;
118 1     1   4815 no Moose;
  1         1  
  1         5  
119             1;
120              
121             __END__
122              
123             =pod
124              
125             =encoding UTF-8
126              
127             =head1 NAME
128              
129             Dist::Zilla::Plugin::Prepender - Prepend lines at the top of your perl files
130              
131             =head1 VERSION
132              
133             version 2.004
134              
135             =head1 SYNOPSIS
136              
137             In your F<dist.ini>:
138              
139             [Prepender]
140             copyright = 0
141             line = use strict;
142             line = use warnings;
143             skip = t/data/.+\.pl
144             skip = something-else-unnecessary
145              
146             =head1 DESCRIPTION
147              
148             This plugin will prepend the specified lines in each Perl module or
149             program within the distribution. For scripts having a shebang line,
150             lines will be inserted just after it.
151              
152             This is useful to enforce a set of pragmas to your files (since pragmas
153             are lexical, they will be active for the whole file), or to add some
154             copyright comments, as the fsf recommends.
155              
156             The module accepts the following options in its F<dist.ini> section:
157              
158             =over 4
159              
160             =item * copyright - whether to insert a boilerplate copyright comment.
161             defaults to true.
162              
163             =item * line - anything you want to add. may be specified multiple
164             times. no default.
165              
166             =item * skip - regexp of file names to not prepend to.
167             may be specified multiple times. no default.
168              
169             =back
170              
171             =for Pod::Coverage mvp_multivalue_args munge_file
172              
173             =head1 SEE ALSO
174              
175             You can look for information on this module at:
176              
177             =over 4
178              
179             =item * Search CPAN
180              
181             L<http://search.cpan.org/dist/Dist-Zilla-Plugin-Prepender>
182              
183             =item * See open / report bugs
184              
185             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dist-Zilla-Plugin-Prepender>
186              
187             =item * Mailing-list (same as L<Dist::Zilla>)
188              
189             L<http://www.listbox.com/subscribe/?list_id=139292>
190              
191             =item * Git repository
192              
193             L<http://github.com/jquelin/dist-zilla-plugin-prepender>
194              
195             =item * AnnoCPAN: Annotated CPAN documentation
196              
197             L<http://annocpan.org/dist/Dist-Zilla-Plugin-Prepender>
198              
199             =item * CPAN Ratings
200              
201             L<http://cpanratings.perl.org/d/Dist-Zilla-Plugin-Prepender>
202              
203             =back
204              
205             =head1 AUTHOR
206              
207             Jerome Quelin
208              
209             =head1 COPYRIGHT AND LICENSE
210              
211             This software is copyright (c) 2009 by Jerome Quelin.
212              
213             This is free software; you can redistribute it and/or modify it under
214             the same terms as the Perl 5 programming language system itself.
215              
216             =cut