File Coverage

blib/lib/Dist/Zilla/Plugin/MungeFile.pm
Criterion Covered Total %
statement 35 35 100.0
branch 3 4 75.0
condition n/a
subroutine 11 11 100.0
pod 1 3 33.3
total 50 53 94.3


line stmt bran cond sub pod time code
1 4     4   1960243 use strict;
  4         8  
  4         120  
2 4     4   16 use warnings;
  4         5  
  4         215  
3             package Dist::Zilla::Plugin::MungeFile; # git description: last-commit-with-mungefile-1-gdc3b25c
4             # ABSTRACT: Modify files in the build, with templates and arbitrary extra variables
5             # KEYWORDS: plugin file content injection modification template
6             # vim: set ts=8 sts=4 sw=4 tw=115 et :
7              
8             our $VERSION = '0.009';
9              
10 4     4   15 use Moose;
  4         5  
  4         36  
11             with (
12             'Dist::Zilla::Role::FileMunger',
13             'Dist::Zilla::Role::TextTemplate',
14             'Dist::Zilla::Role::FileFinderUser' => { default_finders => [ ] },
15             );
16 4     4   21965 use MooseX::SlurpyConstructor 1.2;
  4         72420  
  4         22  
17 4     4   120306 use List::Util 'first';
  4         8  
  4         381  
18 4     4   23 use Scalar::Util 'blessed';
  4         12  
  4         220  
19 4     4   23 use namespace::autoclean;
  4         6  
  4         37  
20              
21             sub mvp_multivalue_args { qw(files) }
22 4     4 0 799 sub mvp_aliases { { file => 'files' } }
23              
24             has files => (
25             isa => 'ArrayRef[Str]',
26             lazy => 1,
27             default => sub { [] },
28             traits => ['Array'],
29             handles => { files => 'sort' },
30             );
31              
32             has _extra_args => (
33             isa => 'HashRef[Str]',
34             init_arg => undef,
35             lazy => 1,
36             default => sub { {} },
37             traits => ['Hash'],
38             handles => { _extra_args => 'elements' },
39             slurpy => 1,
40             );
41              
42             around dump_config => sub
43             {
44             my $orig = shift;
45             my $self = shift;
46              
47             my $config = $self->$orig;
48              
49             $config->{'' . __PACKAGE__} = {
50             finder => $self->finder,
51             files => [ $self->files ],
52             $self->_extra_args,
53             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
54             };
55              
56             return $config;
57             };
58              
59             sub munge_files
60             {
61 4     4 0 294190 my $self = shift;
62              
63             my @files = map {
64 4         258 my $filename = $_;
  1         3  
65 1     2   7 my $file = first { $_->name eq $filename } @{ $self->zilla->files };
  2         114  
  1         45  
66 1 50       65 defined $file ? $file : ()
67             } $self->files;
68              
69 4         11 $self->munge_file($_) for @files, @{ $self->found_files };
  4         37  
70             }
71              
72             sub munge_file
73             {
74 4     4 1 9401 my ($self, $file, $more_args) = @_;
75              
76 4         57 $self->log_debug([ 'updating contents of %s in memory', $file->name ]);
77              
78             $file->content(
79             $self->fill_in_string(
80             $file->content,
81             {
82             $self->_extra_args, # must be first
83             dist => \($self->zilla),
84             plugin => \$self,
85 4 100       1483 %{ $more_args || {} },
  4         75  
86             },
87             )
88             );
89             }
90              
91             __PACKAGE__->meta->make_immutable;
92              
93             __END__
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             Dist::Zilla::Plugin::MungeFile - Modify files in the build, with templates and arbitrary extra variables
102              
103             =head1 VERSION
104              
105             version 0.009
106              
107             =head1 SYNOPSIS
108              
109             In your F<dist.ini>:
110              
111             [MungeFile]
112             file = lib/My/Module.pm
113             house = maison
114              
115             And during the build, F<lib/My/Module.pm>:
116              
117             my @stuff = qw(
118             {{
119             expensive_build_time_sub($house)
120             }}
121             );
122             my ${{ $house }} = 'my castle';
123              
124             Is transformed to:
125              
126             my @stuff = qw(
127             ...list generated from "maison"
128             );
129             my $maison = 'my castle';
130              
131             =head1 DESCRIPTION
132              
133             =for stopwords FileMunger
134              
135             This is a L<FileMunger|Dist::Zilla::Role::FileMunger> plugin for
136             L<Dist::Zilla> that passes a file(s)
137             through a L<Text::Template>.
138              
139             The L<Dist::Zilla> object (as C<$dist>) and this plugin (as C<$plugin>) are
140             also made available to the template, for extracting other information about
141             the build.
142              
143             Additionally, any extra keys and values you pass to the plugin are passed
144             along in variables named for each key.
145              
146             =for Pod::Coverage munge_files munge_file mvp_aliases
147              
148             =head1 OPTIONS
149              
150             =head2 C<finder>
151              
152             =for stopwords FileFinder
153              
154             This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder> for finding
155             files to modify.
156              
157             Other pre-defined finders are listed in
158             L<Dist::Zilla::Role::FileFinderUser/default_finders>.
159             You can define your own with the
160             L<[FileFinder::ByName]|Dist::Zilla::Plugin::FileFinder::ByName> plugin.
161              
162             There is no default.
163              
164             =head2 C<file>
165              
166             Indicates the filename in the dist to be operated upon; this file can exist on
167             disk, or have been generated by some other plugin. Can be included more than once.
168              
169             B<At least one of the C<finder> or C<file> options is required.>
170              
171             =head2 C<arbitrary option>
172              
173             All other keys/values provided will be passed to the template as is.
174              
175             =head1 METHODS
176              
177             =head2 munge_file
178              
179             $plugin->munge_file($file, { key => val, ... });
180              
181             In addition to the standard C<$file> argument, a hashref is accepted which
182             contains additional data to be passed through to C<fill_in_string>.
183              
184             =head1 BACKGROUND
185              
186             =for stopwords refactored
187              
188             This module has been refactored out of
189             L<Dist:Zilla::Plugin::MungeFile::WithDataSection> and
190             L<Dist::Zilla::Plugin::MungeFile::WithConfigFile> to make it more visible as a
191             general template-runner file-munging plugin.
192              
193             =head1 SUPPORT
194              
195             =for stopwords irc
196              
197             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-MungeFile>
198             (or L<bug-Dist-Zilla-Plugin-MungeFile@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-MungeFile@rt.cpan.org>).
199             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
200              
201             =head1 SEE ALSO
202              
203             =over 4
204              
205             =item *
206              
207             L<Dist::Zilla::Plugin::Substitute>
208              
209             =item *
210              
211             L<Dist::Zilla::Plugin::GatherDir::Template>
212              
213             =item *
214              
215             L<Dist::Zilla::Plugin::MungeFile::WithDataSection>
216              
217             =item *
218              
219             L<Dist::Zilla::Plugin::MungeFile::WithConfigFile>
220              
221             =back
222              
223             =head1 AUTHOR
224              
225             Karen Etheridge <ether@cpan.org>
226              
227             =head1 COPYRIGHT AND LICENCE
228              
229             This software is copyright (c) 2013 by Karen Etheridge.
230              
231             This is free software; you can redistribute it and/or modify it under
232             the same terms as the Perl 5 programming language system itself.
233              
234             =cut