v0.2.0
[dist-zilla-plugin-prepender.git] / lib / Dist / Zilla / Plugin / Prepender.pm
blob73ee8f2f88c9fdba403a7cfd92361acf4a65c9e5
1 package Dist::Zilla::Plugin::Prepender;
2 # ABSTRACT: prepend lines at the top of your perl files
4 use strict;
5 use warnings;
7 use Moose;
8 with 'Dist::Zilla::Role::FileMunger';
11 # -- attributes
13 # accept some arguments multiple times.
14 sub multivalue_args { qw{ line } }
16 has copyright => (
17 is => 'ro',
18 default => 0,
20 has _lines => (
21 is => 'ro',
22 isa => 'ArrayRef',
23 lazy => 1,
24 auto_deref => 1,
25 init_arg => 'line',
26 default => sub { [] },
30 # -- public methods
32 sub munge_file {
33 my ($self, $file) = @_;
35 return $self->_munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i;
36 return $self->_munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
37 return;
40 # -- private methods
43 # $self->_munge_perl($file);
45 # munge content of perl $file: add stuff at the top of the file
47 sub _munge_perl {
48 my ($self, $file) = @_;
49 my @prepend;
50 my @lines = split /\n/, $file->content;
52 # add copyright information if requested
53 if ( $self->copyright ) {
54 my @copyright = (
55 '',
56 "This file is part of " . $self->zilla->name,
57 '',
58 split(/\n/, $self->zilla->license->notice),
59 '',
61 push @prepend, map { "# $_" } @copyright;
64 # add hand-written lines to prepend
65 push @prepend, $self->_lines;
67 # insertion point depends if there's a shebang line
68 my $id = ( $lines[0] =~ /^#!(?:.*)perl(?:$|\s)/ ) ? 1 : 0;
69 splice @lines, $id, 0, @prepend;
70 $file->content(join "\n", @lines);
73 __PACKAGE__->meta->make_immutable;
74 no Moose;
77 __END__
79 =begin Pod::Coverage
81 multivalue_args
82 munge_file
84 =end Pod::Coverage
86 =head1 SYNOPSIS
88 In your F<dist.ini>:
90 [Prepender]
91 copyright = 1
92 line = use strict;
93 line = use warnings;
95 =head1 DESCRIPTION
97 This plugin will prepend the specified lines in each Perl module or
98 program within the distribution. For scripts having a shebang line,
99 lines will be inserted just after it.
101 This is useful to enforce a set of pragmas to your files (since pragmas
102 are lexical, they will be active for the whole file), or to add some
103 copyright comments, as the fsf recommends.
105 The module accepts the following options in its F<dist.ini> section:
107 =over 4
109 =item * copyright - whether to insert a boilerplate copyright comment.
110 defaults to false.
112 =item * line - anything you want to add. may be specified multiple
113 times. no default.
115 =back
117 =head1 BUGS
119 Please report any bugs or feature request to
120 C<< <bug-dist-zilla-plugin-prepender@rt.cpan.org> >>, or through the web interface
121 at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dist-Zilla-Plugin-Prepender>.