File Coverage

blib/lib/Dist/Zilla/Plugin/Prepender.pm
Criterion Covered Total %
statement 39 42 92.8
branch 14 18 77.7
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 63 74 85.1


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