File Coverage

blib/lib/Dist/Zilla/Plugin/CopyFilesFromBuild/Filtered.pm
Criterion Covered Total %
statement 33 34 97.0
branch 2 4 50.0
condition n/a
subroutine 10 11 90.9
pod 0 2 0.0
total 45 51 88.2


line stmt bran cond sub pod time code
1 1     1   635080 use 5.008001;
  1         2  
2 1     1   4 use strict;
  1         2  
  1         19  
3 1     1   4 use warnings;
  1         1  
  1         50  
4              
5             package Dist::Zilla::Plugin::CopyFilesFromBuild::Filtered;
6             # ABSTRACT: Copy files from build directory, but filter out lines
7              
8             our $VERSION = '0.002';
9              
10 1     1   4 use Moose;
  1         1  
  1         6  
11 1     1   4078 use Path::Tiny;
  1         2  
  1         82  
12 1     1   4 use Try::Tiny;
  1         1  
  1         330  
13              
14             with qw/ Dist::Zilla::Role::AfterBuild /;
15              
16             #pod =attr copy
17             #pod
18             #pod Specifies files to be copied from the build directory back to the
19             #pod distribution source. May be given multiple times.
20             #pod
21             #pod =cut
22              
23             has copy => (
24             is => 'ro',
25             isa => 'ArrayRef[Str]',
26             default => sub { [] },
27             );
28              
29             #pod =attr filter
30             #pod
31             #pod A regular expression given as string. Any matching lines in copied files
32             #pod will be filtered out of the copy. It will be applied as follows:
33             #pod
34             #pod s{^$filter\n}{}mg
35             #pod
36             #pod Thus, it is anchored at the start of a line to the newline at the end.
37             #pod
38             #pod The default matches a comment with "generated by" as found in typical
39             #pod generated *.PL files: C<#.*generated by.*>
40             #pod
41             #pod =cut
42              
43             has filter => (
44             is => 'ro',
45             isa => 'Str',
46             default => '#.*generated by.*',
47             );
48              
49 3     3 0 82460 sub mvp_multivalue_args { 'copy' }
50              
51             sub after_build {
52 3     3 0 164749 my ( $self, $stash ) = @_;
53              
54 3         5 my $build_root = $stash->{build_root};
55              
56 3         4 for my $file ( @{ $self->copy } ) {
  3         115  
57 3 50       252 next unless length $file;
58 3         8 my $from = path($build_root)->child($file);
59 3         281 my $to = path( $self->zilla->root )->child($file);
60 3 50       207 $self->_copy_filtered( $from, $to ) if -e $from;
61             }
62             }
63              
64             sub _copy_filtered {
65 3     3   57 my ( $self, $from, $to ) = @_;
66              
67             try {
68 3     3   238 my $filter = $self->filter;
69 3         7 my $guts = path($from)->slurp_raw;
70 3         320 $guts =~ s{^$filter\n}{}mg;
71 3         8 path($to)->spew_raw($guts);
72 3         711 $self->log("Copied $from to $to");
73             }
74             catch {
75 0     0     $self->log_fatal("Unable to copy $from to $to: $_");
76             }
77 3         21 }
78              
79             __PACKAGE__->meta->make_immutable;
80              
81             1;
82              
83              
84             # vim: ts=4 sts=4 sw=4 et tw=75:
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Dist::Zilla::Plugin::CopyFilesFromBuild::Filtered - Copy files from build directory, but filter out lines
95              
96             =head1 VERSION
97              
98             version 0.002
99              
100             =head1 SYNOPSIS
101              
102             [CopyFilesFromBuild::Filtered]
103             copy = Makefile.PL
104              
105             [CopyFilesFromBuild::Filtered]
106             copy = whatever.txt
107             filter = #.*whatever.*
108              
109             =head1 DESCRIPTION
110              
111             This module copies files from the build directory to the source directory,
112             but filters out lines from the copied file. It is designed in particular
113             for generated *.PL files, which often have unwanted "generated by" comments
114             that cause unwanted file churn under source control.
115              
116             =head1 ATTRIBUTES
117              
118             =head2 copy
119              
120             Specifies files to be copied from the build directory back to the
121             distribution source. May be given multiple times.
122              
123             =head2 filter
124              
125             A regular expression given as string. Any matching lines in copied files
126             will be filtered out of the copy. It will be applied as follows:
127              
128             s{^$filter\n}{}mg
129              
130             Thus, it is anchored at the start of a line to the newline at the end.
131              
132             The default matches a comment with "generated by" as found in typical
133             generated *.PL files: C<#.*generated by.*>
134              
135             =for Pod::Coverage mvp_multivalue_args after_build
136              
137             =head1 SEE ALSO
138              
139             =over 4
140              
141             =item *
142              
143             L<Dist::Zilla::Plugin::CopyFilesFromBuild>
144              
145             =item *
146              
147             L<Dist::Zilla::Plugin::CopyReadmeFromBuild>
148              
149             =item *
150              
151             L<Dist::Zilla::Plugin::CopyFilesFromRelease>
152              
153             =item *
154              
155             L<Dist::Zilla::Plugin::CopyMakefilePLFromBuild>
156              
157             =back
158              
159             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
160              
161             =head1 SUPPORT
162              
163             =head2 Bugs / Feature Requests
164              
165             Please report any bugs or feature requests through the issue tracker
166             at L<https://github.com/dagolden/Dist-Zilla-Plugin-CopyFilesFromBuild-Filtered/issues>.
167             You will be notified automatically of any progress on your issue.
168              
169             =head2 Source Code
170              
171             This is open source software. The code repository is available for
172             public review and contribution under the terms of the license.
173              
174             L<https://github.com/dagolden/Dist-Zilla-Plugin-CopyFilesFromBuild-Filtered>
175              
176             git clone https://github.com/dagolden/Dist-Zilla-Plugin-CopyFilesFromBuild-Filtered.git
177              
178             =head1 AUTHOR
179              
180             David Golden <dagolden@cpan.org>
181              
182             =head1 CONTRIBUTOR
183              
184             =for stopwords David Golden
185              
186             David Golden <xdg@xdg.me>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is Copyright (c) 2015 by David Golden.
191              
192             This is free software, licensed under:
193              
194             The Apache License, Version 2.0, January 2004
195              
196             =cut