support for automatic copyright prepending
[dist-zilla-plugin-prepender.git] / lib / Dist / Zilla / Plugin / Prepender.pm
blobd8e4b49dd4f50f8bbb231d15defcc09bd43bbed1
1 package Dist::Zilla::Plugin::Prepender;
2 # ABSTRACT: prepend lines at the top of your perl files
4 use Moose;
5 with 'Dist::Zilla::Role::FileMunger';
8 # -- attributes
10 # accept some arguments multiple times.
11 sub multivalue_args { qw{ line } }
13 has copyright => (
14 is => 'ro',
15 default => 0,
17 has _lines => (
18 is => 'ro',
19 isa => 'ArrayRef',
20 lazy => 1,
21 auto_deref => 1,
22 init_arg => 'line',
23 default => sub { [] },
27 # -- public methods
29 sub munge_file {
30 my ($self, $file) = @_;
32 return $self->_munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i;
33 return $self->_munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
34 return;
37 # -- private methods
40 # $self->_munge_perl($file);
42 # munge content of perl $file: add stuff at the top of the file
44 sub _munge_perl {
45 my ($self, $file) = @_;
46 my @prepend;
47 my @lines = split /\n/, $file->content;
49 # add copyright information if requested
50 if ( $self->copyright ) {
51 my @copyright = (
52 '',
53 "This file is part of " . $self->zilla->name,
54 '',
55 split(/\n/, $self->zilla->license->notice),
56 '',
58 push @prepend, map { "# $_" } @copyright;
61 # add hand-written lines to prepend
62 push @prepend, $self->_lines;
64 # insertion point depends if there's a shebang line
65 my $id = ( $lines[0] =~ /^#!(?:.*)perl(?:$|\s)/ ) ? 1 : 0;
66 splice @lines, $id, 0, @prepend;
67 $file->content(join "\n", @lines);
70 __PACKAGE__->meta->make_immutable;
71 no Moose;
74 __END__
76 =begin Pod::Coverage
78 multivalue_args
79 munge_file
81 =end Pod::Coverage
83 =head1 SYNOPSIS
85 In your F<dist.ini>:
87 [Prepender]
88 line = # This file is part of Foo::Bar
89 line = # Foo::Bar is copyright...
90 line = use strict;
91 line = use warnings;
93 =head1 DESCRIPTION
95 This plugin will prepend the specified lines in each Perl module or
96 program within the distribution. For scripts having a shebang line,
97 lines will be inserted just after it.
99 This is useful to enforce a set of pragmas to your files (since pragmas
100 are lexical, they will be active for the whole file), or to add some
101 copyright comments, as the fsf recommends.
103 =head1 BUGS
105 Please report any bugs or feature request to
106 C<< <bug-dist-zilla-plugin-prepender@rt.cpan.org> >>, or through the web interface
107 at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dist-Zilla-Plugin-Prepender>.