File Coverage

blib/lib/Dist/Zilla/Role/Regenerator.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1 4     4   36977 use 5.006; # our
  4         13  
2 4     4   13 use strict;
  4         5  
  4         69  
3 4     4   12 use warnings;
  4         5  
  4         223  
4              
5             package Dist::Zilla::Role::Regenerator;
6              
7             our $VERSION = '0.001000';
8              
9             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
10              
11             # ABSTRACT: A package which can regenerate source files
12              
13 4     4   400 use Moose::Role qw( requires around );
  4         296073  
  4         23  
14              
15             requires 'regenerate';
16              
17             around dump_config => sub {
18             my ( $orig, $self, @args ) = @_;
19             my $config = $self->$orig(@args);
20             my $payload = $config->{ +__PACKAGE__ } = {};
21              
22             ## no critic (RequireInterpolationOfMetachars)
23             $payload->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION;
24             return $config;
25             };
26              
27 4     4   10686 no Moose::Role;
  4         8  
  4         15  
28              
29             1;
30              
31             __END__
32              
33             =pod
34              
35             =encoding UTF-8
36              
37             =head1 NAME
38              
39             Dist::Zilla::Role::Regenerator - A package which can regenerate source files
40              
41             =head1 VERSION
42              
43             version 0.001000
44              
45             =head1 SYNOPSIS
46              
47             pacakge Dist::Zilla::Plugin::Regenerate::SomeThing;
48             use Moose;
49             use Path::Tiny qw( path );
50             with "Dist::Zilla::Role::Plugin","Dist::Zilla::Role::Regenerator";
51              
52             ...
53             sub regenerate {
54             my ( $self, $config ) = @_;
55             path($config->{root}, 'CONTRIBUTING.pod')->spew_raw($content);
56             path($config->{build_root}, 'META.json')->copy(path($config->{root}, 'META.json'));
57             }
58              
59             =head1 DESCRIPTION
60              
61             This role is for C<Dist::Zilla> C<Plugin>'s that wish to fire under C<dzil regenerate>
62              
63             It is strongly recommended that this role not be lumped on randomly into packages that
64             will operate at other phases.
65              
66             =head1 IMPLEMENTATION
67              
68             Consumers of this role B<MUST> compose in C<Dist::Zilla::Role::Plugin> at some stage,
69             or at least, C<Dist::Zilla::Role::ConfigDumper>.
70              
71             Consumers of this rule B<MUST> implement a method C<regenerate>.
72              
73             =head2 C<regenerate> implementation
74              
75             C<regenerate> will be called as a method and passed a configuration hash.
76              
77             {
78             build_root => "path/to/where/dzil/build/writes/to"
79             root => "path/to/dzil/source/tree"
80             }
81              
82             You may do with these as you wish, but recommended usage is to employ C<Path::Tiny>
83             to do one of the following:
84              
85             =over 4
86              
87             =item * Write files directly from the plugin
88              
89             path($config->{root}, "CONTRIBUTING.pod")->spew_raw( ... );
90              
91             Keep in mind how file encoding works if you get your data from other parts of C<Dist::Zilla>
92              
93             =item * Copy files from the build tree
94              
95             path($config->{build_root}, "META.json")->copy($path->{root}, "META.json");
96              
97             ( Note: This case is implemented by L<< C<[Regenerate]>|Dist::Zilla::Plugin::Regenerate >> )
98              
99             =item * (DON'T) Write files from C<zilla> to disk
100              
101             How you'd do this is left to your imagination, but the details are ugly.
102              
103             Take note of the calls to C<_write_out_file> in
104             L<< C<Dist::Zilla::Dist::Builder>|Dist::Zilla::Dist::Builder >>
105             and the implementation details of C<_write_out_file> in L<< C<Dist::Zilla>|Dist::Zilla >>
106              
107             I suspect something like
108              
109             $self->zilla->_write_out_file( $self->zilla->files[0] , $config->{root} )
110              
111             Might kinda work, but keep in mind, that will spew if the file already exists.
112              
113             B<G>ood B<L>uck B<H>ave B<F>un.
114              
115             Also, this approach is B<NOT> recommended, because the final release image is B<NOT>
116             drafted from C<< $self->zilla->files >> but from direct read of the C<< $config->{build_root} >>.
117              
118             See C<Dist::Zilla/build_archive> which has the call sequence:
119              
120             =over 4
121              
122             =item * ensure_built_in ...
123              
124             =item * write_out ...
125              
126             =item * C<-AfterBuild>
127              
128             =item * C<-BeforeArchive>
129              
130             =item * C<_build_archive> ( from $write_out_dir )
131              
132             =back
133              
134             =back
135              
136             =head1 AUTHOR
137              
138             Kent Fredric <kentnl@cpan.org>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2016 by Kent Fredric <kentfredric@gmail.com>.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut