File Coverage

blib/lib/Dist/Zilla/Plugin/Substitute.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 0 3 0.0
total 51 54 94.4


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