File Coverage

blib/lib/Dist/Zilla/Plugin/Regenerate.pm
Criterion Covered Total %
statement 51 51 100.0
branch n/a
condition n/a
subroutine 16 16 100.0
pod 0 3 0.0
total 67 70 95.7


line stmt bran cond sub pod time code
1 1     1   2797139 use 5.006; # our
  1         22  
2 1     1   7 use strict;
  1         2  
  1         43  
3 1     1   8 use warnings;
  1         2  
  1         74  
4              
5             package Dist::Zilla::Plugin::Regenerate;
6              
7             our $VERSION = '0.001002';
8              
9             # ABSTRACT: Write contents to your source tree explicitly
10              
11             our $AUTHORITY = 'cpan:DBOOK'; # AUTHORITY
12              
13 1     1   7 use Moose qw( with has around );
  1         5  
  1         10  
14 1     1   6627 use Beam::Event qw();
  1         154838  
  1         36  
15 1     1   1310 use Beam::Emitter qw();
  1         53877  
  1         49  
16 1     1   12 use Path::Tiny 0.017 qw( path );
  1         35  
  1         66  
17 1     1   12 use namespace::clean -except => 'meta';
  1         2  
  1         15  
18              
19             with qw/ Dist::Zilla::Role::Plugin Dist::Zilla::Role::Regenerator Beam::Emitter /;
20              
21             has filenames => ( is => 'ro', isa => 'ArrayRef', default => sub { [] }, );
22              
23             around dump_config => sub {
24             my ( $orig, $self, @args ) = @_;
25             my $config = $self->$orig(@args);
26             my $payload = $config->{ +__PACKAGE__ } = {};
27             $payload->{filenames} = $self->filenames;
28              
29             ## no critic (RequireInterpolationOfMetachars)
30             # Self report when inherited
31             $payload->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION unless __PACKAGE__ eq ref $self;
32             return $config;
33             };
34              
35 1     1   571 no Moose;
  1         3  
  1         26  
36             __PACKAGE__->meta->make_immutable;
37              
38              
39              
40              
41              
42 1     1 0 268 sub mvp_multivalue_args { qw( filenames ) }
43 1     1 0 182 sub mvp_aliases { +{ filename => 'filenames' } }
44              
45             sub regenerate {
46 1     1 0 64 my ( $self, $config ) = @_;
47 1         4 $self->emit( 'before_regenerate', class => 'Dist::Zilla::Event::Regenerate::BeforeRegenerate', %{$config} );
  1         11  
48              
49             # Note, that because dzil is build -> dir -> archive -> release
50             # regenerate has to pick files from the "dir" target on disk, and can't go through
51             # dzil IO
52 1         3 for my $file ( @{ $self->filenames } ) {
  1         38  
53 1         6 my $src = path( $config->{build_root}, $file );
54 1         48 my $dest = path( $config->{root}, $file );
55 1         33 $src->copy($dest);
56 1         558 $self->log("Copied $src to $dest");
57             }
58              
59 1         443 $self->emit( 'after_regenerate', class => 'Dist::Zilla::Event::Regenerate::AfterRegenerate', %{$config} );
  1         6  
60             }
61              
62             {
63             package # Hide
64             Dist::Zilla::Event::Regenerate::BeforeRegenerate;
65 1     1   523 use Moose qw( has extends );
  1         6  
  1         11  
66             extends q/Beam::Event/;
67              
68             has 'build_root' => ( isa => 'Str', is => 'ro', required => 1 );
69             has 'root' => ( isa => 'Str', is => 'ro', required => 1 );
70 1     1   6676 no Moose;
  1         3  
  1         14  
71             __PACKAGE__->meta->make_immutable;
72             }
73             {
74             package # Hide
75             Dist::Zilla::Event::Regenerate::AfterRegenerate;
76 1     1   176 use Moose qw( has extends );
  1         2  
  1         5  
77             extends q/Beam::Event/;
78              
79             has 'build_root' => ( isa => 'Str', is => 'ro', required => 1 );
80             has 'root' => ( isa => 'Str', is => 'ro', required => 1 );
81 1     1   5688 no Moose;
  1         10  
  1         4  
82             __PACKAGE__->meta->make_immutable;
83             }
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =encoding UTF-8
92              
93             =head1 NAME
94              
95             Dist::Zilla::Plugin::Regenerate - Write contents to your source tree explicitly
96              
97             =head1 VERSION
98              
99             version 0.001002
100              
101             =head1 SYNOPSIS
102              
103             B<in C<dist.ini>>
104              
105             [Regenerate]
106             ; For example
107             filenames = Makefile.PL
108             filenames = META.json
109             filenames = README.mkdn
110              
111             B<on your command line>
112              
113             dzil regenerate
114             # Makefile.PL updated
115             # META.json updated
116             # ...
117              
118             =head1 DESCRIPTION
119              
120             This plugin, in conjunction with the L<< C<dzil regenerate>|Dist::Zilla::App::Command::regenerate >>
121             command, allows turn-key copying of content from your build tree to your source tree.
122              
123             This is a compatriot of L<< C<[CopyFilesFromBuild]>|Dist::Zilla::Plugin::CopyFilesFromBuild >> and
124             L<< C<[CopyFilesFromRelease]>|Dist::Zilla::Plugin::CopyFilesFromRelease >>, albeit targeted to happen
125             outside your standard work phase, allowing you to copy generated files back into the source tree on demand,
126             and also freeing you from them being updated when you don't require it.
127              
128             =for Pod::Coverage mvp_multivalue_args mvp_aliases regenerate
129              
130             =head1 ATTRIBUTES
131              
132             =head2 C<filenames>
133              
134             An Array of Strings describing files to copy.
135              
136             [Regenerate]
137             filename = Makefile.PL
138             filename = META.json
139              
140             B<aliases:> C<filename>
141              
142             =head1 SEE ALSO
143              
144             =over 4
145              
146             =item * L<< C<[CopyFilesFromBuild]>|Dist::Zilla::Plugin::CopyFilesFromBuild >>
147              
148             This plugin operates only on the C<AfterBuild> phase, and thus will modify files on every
149             C<dzil build>, creating undesired work-flow churn.
150              
151             =item * L<< C<[CopyFilesFromRelease]>|Dist::Zilla::Plugin::CopyFilesFromRelease >>
152              
153             This plugin operates only on the C<AfterRelease> phase, impeding work-flow slightly
154             for people who B<WANT> to update their source tree without actually doing a release.
155              
156             This plugin can be used instead of C<[Regenerate]> though, by using it in conjunction
157             with L<< C<[Regenerate::AfterReleasers]>|Dist::Zilla::Plugin::Regenerate::AfterReleasers >>
158              
159             =back
160              
161             =head1 AUTHOR
162              
163             Kent Fredric <kentnl@cpan.org>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2016-2017 by Kent Fredric <kentfredric@gmail.com>.
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut