first take at dzp:prepender
[dist-zilla-plugin-prepender.git] / lib / Dist / Zilla / Plugin / Prepender.pm
blob4238366c3c5868e6ff83962f51578afd633a3881
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 _lines => (
14 is => 'ro',
15 isa => 'ArrayRef',
16 lazy => 1,
17 auto_deref => 1,
18 init_arg => 'line',
19 default => sub { [] },
23 # -- public methods
25 sub munge_file {
26 my ($self, $file) = @_;
28 return $self->_munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i;
29 return $self->_munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
30 return;
33 # -- private methods
36 # $self->_munge_perl($file);
38 # munge content of perl $file: add stuff at the top of the file
40 sub _munge_perl {
41 my ($self, $file) = @_;
42 my $stuff = join "\n", $self->_lines;
43 my @lines = split /\n/, $file->content;
45 # if there's a shebang line, insert stuff just after it
46 my $id = ( $lines[0] =~ /^#!(?:.*)perl(?:$|\s)/ ) ? 1 : 0;
48 splice @lines, $id, 0, $stuff;
49 $file->content(join "\n", @lines);
52 __PACKAGE__->meta->make_immutable;
53 no Moose;
56 __END__
58 =begin Pod::Coverage
60 multivalue_args
61 munge_file
63 =end Pod::Coverage
65 =head1 SYNOPSIS
67 In your F<dist.ini>:
69 [Prepender]
70 line = # This file is part of Foo::Bar
71 line = use strict;
72 line = use warnings;
74 =head1 DESCRIPTION
76 This plugin will prepend the specified lines in each Perl module or
77 program within the distribution. For scripts having a shebang line,
78 lines will be inserted just after it.
80 This is useful to enforce a set of pragmas to your files (since pragmas
81 are lexical, they will be active for the whole file), or to add some
82 copyright comments, as the fsf recommends.