File Coverage

blib/lib/Dist/Zilla/Plugin/InsertCopyright.pm
Criterion Covered Total %
statement 49 49 100.0
branch 7 8 87.5
condition 3 3 100.0
subroutine 11 11 100.0
pod 0 1 0.0
total 70 72 97.2


line stmt bran cond sub pod time code
1 2     2   1390230 use 5.008001;
  2         5  
  2         71  
2 2     2   8 use strict;
  2         4  
  2         68  
3 2     2   8 use warnings;
  2         3  
  2         55  
4 2     2   8 use utf8;
  2         3  
  2         20  
5              
6             package Dist::Zilla::Plugin::InsertCopyright;
7             # ABSTRACT: Insert copyright statement into source code files
8             our $VERSION = '0.005'; # VERSION
9              
10 2     2   1209 use PPI::Document;
  2         179817  
  2         62  
11 2     2   13 use Moose;
  2         2  
  2         16  
12 2     2   10178 use Carp qw/croak/;
  2         3  
  2         870  
13              
14             #<<< No perltidy
15             with(
16             'Dist::Zilla::Role::FileMunger',
17             'Dist::Zilla::Role::PPI',
18             );
19             #>>>
20              
21             has copyright_lines => (
22             is => 'ro',
23             isa => 'ArrayRef',
24             lazy_build => 1,
25             );
26              
27             sub _build_copyright_lines {
28 2     2   5 my ($self) = @_;
29 2         53 my @copyright = (
30             '', "This file is part of " . $self->zilla->name,
31             '', split( /\n/, $self->zilla->license->notice ), '',
32             );
33              
34 2 100       9441 my @copyright_comment = map { length($_) ? "# $_" : '#' } @copyright;
  16         39  
35              
36 2         90 return \@copyright_comment;
37             }
38              
39             # -- public methods
40              
41             sub munge_file {
42 9     9 0 92495 my ( $self, $file ) = @_;
43              
44 9 100 100     216 if ( $file->name =~ /\.(?:pm|pl|t)$/i
45             || $file->content =~ /^#!(?:.*)perl(?:$|\s)/ )
46             {
47 7         2577 $self->_munge_perl($file);
48             }
49              
50 9         2589 return;
51             }
52              
53             # -- private methods
54              
55             #
56             # $self->_munge_perl($file, $lines);
57             #
58             # munge content of perl $file: add stuff at a #COPYRIGHT comment
59             #
60              
61             sub _munge_perl {
62 7     7   10 my ( $self, $file ) = @_;
63              
64 7         25 my $doc = $self->ppi_document_for_file($file);
65              
66 7         15344 my $comments = $doc->find('PPI::Token::Comment');
67              
68 7         3366 my $lines = $self->copyright_lines;
69              
70 7 50       16 if ( ref($comments) eq 'ARRAY' ) {
71 7         7 foreach my $c ( @{$comments} ) {
  7         12  
72 15 100       59 if ( $c =~ /^(\s*)(\#\s+COPYRIGHT\b)$/xms ) {
73 5         35 my ( $ws, $comment ) = ( $1, $2 );
74 5         15 my $code = join( "\n", map { "$ws$_" } @$lines );
  40         66  
75 5         37 $c->set_content("$code\n");
76 5         166 $self->log_debug( "Added copyright to " . $file->name );
77 5         325 last;
78             }
79             }
80 7         34 $self->save_ppi_document_to_file( $doc, $file );
81             }
82              
83 7         3021 return;
84             }
85              
86             __PACKAGE__->meta->make_immutable;
87 2     2   10 no Moose;
  2         3  
  2         9  
88             1;
89              
90             # COPYRIGHT
91              
92             __END__
93              
94             =pod
95              
96             =encoding UTF-8
97              
98             =head1 NAME
99              
100             Dist::Zilla::Plugin::InsertCopyright - Insert copyright statement into source code files
101              
102             =head1 VERSION
103              
104             version 0.005
105              
106             =head1 SYNOPSIS
107              
108             In your F<dist.ini>:
109              
110             [InsertCopyright]
111              
112             In your source files (before C<__END__>):
113              
114             # COPYRIGHT
115              
116             =head1 DESCRIPTION
117              
118             This module replaces a special C<# COPYRIGHT> comment in your Perl source
119             files with a short notice appropriate to your declared copyright. The
120             special comment B<must> be placed before C<__END__>. Only the first such
121             comment will be replaced.
122              
123             It is inspired by excellent L<Dist::Zilla::Plugin::Prepender> but gives control
124             of the copyright notice placement instead of always adding it at the start of a
125             file.
126              
127             I wrote this to let me put copyright statements at the end of my code to keep
128             line numbers of code consistent between the generated distribution and the
129             repository source. See L<Dist::Zilla::Plugin::OurPkgVersion> for another
130             useful plugin that preserves line numbering.
131              
132             =for Pod::Coverage munge_file
133              
134             =head1 ACKNOWLEDGMENTS
135              
136             Code in this module is based heavily on Dist::Zilla::Plugin::OurPkgVersion
137             by Caleb Cushing and Dist::Zilla::Plugin::Prepender by Jérôme Quelin. Thank
138             you to both of them for their work and for releasing it as open source for
139             reuse.
140              
141             =head1 SEE ALSO
142              
143             =over 4
144              
145             =item *
146              
147             L<Dist::Zilla> and L<dzil.org|http://dzil.org/>
148              
149             =item *
150              
151             L<Dist::Zilla::Plugin::OurPkgVersion>
152              
153             =item *
154              
155             L<Dist::Zilla::Plugin::Prepender>
156              
157             =back
158              
159             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
160              
161             =head1 SUPPORT
162              
163             =head2 Bugs / Feature Requests
164              
165             Please report any bugs or feature requests through the issue tracker
166             at L<https://github.com/dagolden/Dist-Zilla-Plugin-InsertCopyright/issues>.
167             You will be notified automatically of any progress on your issue.
168              
169             =head2 Source Code
170              
171             This is open source software. The code repository is available for
172             public review and contribution under the terms of the license.
173              
174             L<https://github.com/dagolden/Dist-Zilla-Plugin-InsertCopyright>
175              
176             git clone https://github.com/dagolden/Dist-Zilla-Plugin-InsertCopyright.git
177              
178             =head1 AUTHOR
179              
180             David Golden <dagolden@cpan.org>
181              
182             =head1 CONTRIBUTORS
183              
184             =over 4
185              
186             =item *
187              
188             Jean-Damien Durand <jeandamiendurand@free.fr>
189              
190             =item *
191              
192             Keedi Kim <keedi.k@gmail.com>
193              
194             =back
195              
196             =head1 COPYRIGHT AND LICENSE
197              
198             This software is Copyright (c) 2011 by David Golden.
199              
200             This is free software, licensed under:
201              
202             The Apache License, Version 2.0, January 2004
203              
204             =cut