| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Dist::Zilla::Plugin::Substitute; | 
| 2 |  |  |  |  |  |  | $Dist::Zilla::Plugin::Substitute::VERSION = '0.007'; | 
| 3 | 3 |  |  | 3 |  | 9855698 | use Moose; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 25 |  | 
| 4 | 3 |  |  | 3 |  | 20869 | use Moose::Util::TypeConstraints; | 
|  | 3 |  |  |  |  | 12 |  | 
|  | 3 |  |  |  |  | 41 |  | 
| 5 | 3 |  |  | 3 |  | 7014 | use MooseX::Types::Moose qw/ArrayRef CodeRef Str/; | 
|  | 3 |  |  |  |  | 14 |  | 
|  | 3 |  |  |  |  | 39 |  | 
| 6 | 3 |  |  | 3 |  | 16596 | use List::Util 'first'; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 287 |  | 
| 7 | 3 |  |  | 3 |  | 35 | use Carp 'croak'; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 2492 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | enum 'Mode', [qw/lines whole/]; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | with 'Dist::Zilla::Role::FileMunger', | 
| 12 |  |  |  |  |  |  | 'Dist::Zilla::Role::FileFinderUser' => { | 
| 13 |  |  |  |  |  |  | default_finders => [ ':InstallModules', ':ExecFiles' ], | 
| 14 |  |  |  |  |  |  | }; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my $codeliteral = subtype as CodeRef; | 
| 17 |  |  |  |  |  |  | coerce $codeliteral, from ArrayRef, via { | 
| 18 |  |  |  |  |  |  | my $code = sprintf 'sub { %s }', join "\n", @{$_}; | 
| 19 |  |  |  |  |  |  | eval $code or croak "Couldn't eval: $@"; | 
| 20 |  |  |  |  |  |  | }; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has code => ( | 
| 23 |  |  |  |  |  |  | is       => 'ro', | 
| 24 |  |  |  |  |  |  | isa      => $codeliteral, | 
| 25 |  |  |  |  |  |  | coerce   => 1, | 
| 26 |  |  |  |  |  |  | required => 1, | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  | has filename_code => ( | 
| 29 |  |  |  |  |  |  | is        => 'ro', | 
| 30 |  |  |  |  |  |  | isa       => $codeliteral, | 
| 31 |  |  |  |  |  |  | coerce    => 1, | 
| 32 |  |  |  |  |  |  | predicate => '_has_filename_code', | 
| 33 |  |  |  |  |  |  | ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub mvp_multivalue_args { | 
| 36 |  |  |  |  |  |  | return qw/code filename_code files/; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub mvp_aliases { | 
| 40 |  |  |  |  |  |  | return { | 
| 41 | 4 |  |  | 4 | 0 | 800 | content_code => 'code', | 
| 42 |  |  |  |  |  |  | file         => 'files', | 
| 43 |  |  |  |  |  |  | }; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | has mode => ( | 
| 47 |  |  |  |  |  |  | is => 'ro', | 
| 48 |  |  |  |  |  |  | isa => 'Mode', | 
| 49 |  |  |  |  |  |  | default => 'lines', | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | has files => ( | 
| 53 |  |  |  |  |  |  | isa     => ArrayRef[Str], | 
| 54 |  |  |  |  |  |  | traits  => ['Array'], | 
| 55 |  |  |  |  |  |  | lazy    => 1, | 
| 56 |  |  |  |  |  |  | default => sub { [] }, | 
| 57 |  |  |  |  |  |  | handles => { | 
| 58 |  |  |  |  |  |  | files => 'elements', | 
| 59 |  |  |  |  |  |  | }, | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub munge_files { | 
| 63 | 4 |  |  | 4 | 0 | 251664 | my $self = shift; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 4 | 100 |  |  |  | 184 | if (my @filenames = $self->files) { | 
| 66 | 1 |  |  |  |  | 2 | foreach my $file (@{ $self->zilla->files }) { | 
|  | 1 |  |  |  |  | 30 |  | 
| 67 | 2 | 100 |  | 2 |  | 98 | $self->munge_file($file) if first { $file->name eq $_ } @filenames; | 
|  | 2 |  |  |  |  | 9 |  | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | else { | 
| 71 | 3 |  |  |  |  | 7 | $self->munge_file($_) for @{ $self->found_files }; | 
|  | 3 |  |  |  |  | 18 |  | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 4 |  |  |  |  | 19 | return; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub munge_file { | 
| 78 | 4 |  |  | 4 | 0 | 6483 | my ($self, $file) = @_; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 4 |  |  |  |  | 142 | my $code = $self->code; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 4 | 100 |  |  |  | 141 | if ($self->mode eq 'lines') { | 
| 83 | 3 |  |  |  |  | 22 | my @content = split /^/m, $file->content; | 
| 84 | 3 |  |  |  |  | 3713 | $code->() for @content; | 
| 85 | 3 |  |  |  |  | 29 | $file->content(join '', @content); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | else { | 
| 88 | 1 |  |  |  |  | 4 | my $content = $file->content; | 
| 89 | 1 |  |  |  |  | 808 | $code->() for $content; | 
| 90 | 1 |  |  |  |  | 6 | $file->content($content); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 4 | 100 |  |  |  | 1256 | if ($self->_has_filename_code) { | 
| 94 | 1 |  |  |  |  | 5 | my $filename      = $file->name; | 
| 95 | 1 |  |  |  |  | 81 | my $filename_code = $self->filename_code; | 
| 96 | 1 |  |  |  |  | 26 | $filename_code->() for $filename; | 
| 97 | 1 |  |  |  |  | 5 | $file->name($filename); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 4 |  |  |  |  | 260 | return; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # ABSTRACT: Substitutions for files in dzil | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | __END__ | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =pod | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =encoding UTF-8 | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head1 NAME | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Dist::Zilla::Plugin::Substitute - Substitutions for files in dzil | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 VERSION | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | version 0.007 | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | [Substitute] | 
| 124 |  |  |  |  |  |  | finder = :ExecFiles | 
| 125 |  |  |  |  |  |  | code = s/Foo/Bar/g | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | ; alternatively | 
| 128 |  |  |  |  |  |  | [Substitute] | 
| 129 |  |  |  |  |  |  | file = lib/Buz.pm | 
| 130 |  |  |  |  |  |  | code = s/Buz/Quz/g | 
| 131 |  |  |  |  |  |  | filename_code = s/Buz/Quz/ | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | This module performs substitutions on files in Dist::Zilla. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head2 code (or content_code) | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | An arrayref of lines of code. This is converted into a sub that's called for each line, with C<$_> containing that line. Alternatively, it may be a subref if passed from for example a pluginbundle. Mandatory. | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head2 mode | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | Either C<lines>(the default) or C<whole>. This determines if the substitution is done per line or per whole file. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =head2 filename_code | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Like C<content_code> but the resulting sub is called for the filename. | 
| 150 |  |  |  |  |  |  | Optional. | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head2 finders | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | The finders to use for the substitutions. Defaults to C<:InstallModules, :ExecFiles>. May also be spelled as C<finder> in the dist.ini. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head2 files | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | The files to substitute. It defaults to the files in C<finders>. May also be spelled as C<file> in the dist.ini. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | # vim: ts=4 sts=4 sw=4 noet : | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =head1 AUTHOR | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | Leon Timmermans <leont@cpan.org> | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | This software is copyright (c) 2013 by Leon Timmermans. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 171 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =cut |