File Coverage

blib/lib/Dist/Zilla/Plugin/INI/Baked.pm
Criterion Covered Total %
statement 55 56 98.2
branch 1 2 50.0
condition n/a
subroutine 20 20 100.0
pod 1 1 100.0
total 77 79 97.4


line stmt bran cond sub pod time code
1 2     2   3256585 use 5.006;
  2         8  
2 2     2   12 use strict;
  2         4  
  2         68  
3 2     2   23 use warnings;
  2         2  
  2         234  
4              
5             package Dist::Zilla::Plugin::INI::Baked;
6              
7             our $VERSION = '0.002002';
8              
9             # ABSTRACT: Add a baked version of your configuration to tree automatically
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 2     2   696 use Moose qw( with has around );
  2         322118  
  2         21  
14 2     2   11848 use MooX::Lsub qw( lsub );
  2         2687  
  2         9  
15 2     2   1292 use Dist::Zilla::File::FromCode;
  2         337689  
  2         111  
16 2     2   1255 use Dist::Zilla::Util::CurrentCmd 0.002000 qw( as_cmd );
  2         18653  
  2         12  
17 2     2   428 use Path::Tiny qw( path );
  2         5  
  2         199  
18 2     2   526 use Dist::Zilla::Util::ExpandINI 0.001001;
  2         47473  
  2         1184  
19              
20             with 'Dist::Zilla::Role::FileGatherer';
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32 2     2   87 lsub 'filename' => sub { 'dist.ini.baked' };
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44 2     2   374 lsub 'source_filename' => sub { 'dist.ini' };
45              
46 2     2   87 lsub '_root' => sub { path( $_[0]->zilla->root ) };
47 2     2   91 lsub '_source_file' => sub { $_[0]->_root->child( $_[0]->source_filename ) };
48              
49             around dump_config => sub {
50             my ( $orig, $self, @args ) = @_;
51             my $config = $self->$orig(@args);
52             my $localconf = $config->{ +__PACKAGE__ } = {};
53              
54             $localconf->{filename} = $self->filename;
55             $localconf->{source_filename} = $self->source_filename;
56              
57             $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION
58             unless __PACKAGE__ eq ref $self;
59              
60             return $config;
61             };
62              
63             sub _gen_preamble {
64 2     2   5 my ($self) = @_;
65 2         5 my $out = q[];
66 2         70 $out .= sprintf qq[; This file is generated from %s ( in the source repository ) by %s.\n], $self->filename, $self->meta->name;
67 2         145 $out .= qq[; It exists for your convenience for development if you need it.\n];
68 2         71 $out .= sprintf qq[; You should edit %s in the source repository for any long term changes\n], $self->filename;
69 2         21 return $out;
70             }
71              
72             sub _inflate_ini {
73 2     2   5 my ($self) = @_;
74 2         35 my $state = Dist::Zilla::Util::ExpandINI->new();
75 2         2757 $state->_load_file( $self->_source_file );
76 2         5806 $state->_expand();
77 2         17570 return $state->_store_string;
78             }
79              
80             sub _gen_ini {
81 2     2   6 my ($self) = @_;
82 2         9 my $out = $self->_gen_preamble;
83 2     2   19 as_cmd( 'bakeini' => sub { $out .= $self->_inflate_ini } );
  2         35  
84 2         12356 return $out;
85              
86             }
87              
88              
89              
90              
91              
92              
93              
94              
95              
96             sub gather_files {
97 2     2 1 138328 my ($self) = @_;
98              
99 2 50       71 if ( not -e $self->_source_file ) {
100 0         0 return $self->log_fatal( [ 'Can\'t bake %s, it does not exist', $self->_source_file->stringify ] );
101             }
102             my $file = Dist::Zilla::File::FromCode->new(
103             {
104             name => $self->filename,
105             added_by => $self->meta->name,
106             code_return_type => 'text',
107 2     2   108368 code => sub { $self->_gen_ini },
108             },
109 2         320 );
110 2         525 $self->add_file($file);
111 2         1247 return;
112             }
113             __PACKAGE__->meta->make_immutable;
114 2     2   14 no Moose;
  2         3  
  2         17  
115              
116             1;
117              
118             __END__
119              
120             =pod
121              
122             =encoding UTF-8
123              
124             =head1 NAME
125              
126             Dist::Zilla::Plugin::INI::Baked - Add a baked version of your configuration to tree automatically
127              
128             =head1 VERSION
129              
130             version 0.002002
131              
132             =head1 SYNOPSIS
133              
134             # somewhere in dist.ini or even your bundle
135             [INI::Baked]
136             ; filename = dist.ini.baked
137             ; source_filename = dist.ini
138              
139             # and and
140             dzil build
141              
142             # and and
143             cat $MYDIST/dist.ini.baked # yay
144              
145             Whether you wish to
146              
147             =over 4
148              
149             =item * Copy that file back to C<root/>
150              
151             =item * Name that file C<dist.ini>
152              
153             =item * Add/Not add the original C<dist.ini> to your built code.
154              
155             =back
156              
157             All these choices are your discretion, and are presently expected to master other dzil plugins to make this possible.
158              
159             I recommend:
160              
161             =over 4
162              
163             =item * L<< C<[CopyFilesFromBuild]>|Dist::Zilla::Plugin::CopyFilesFromBuild >>
164              
165             =item * L<< C<[CopyFilesFromRelease]>|Dist::Zilla::Plugin::CopyFilesFromRelease >>
166              
167             =item * Passing exclude rules to L<< C<[Git::GatherDir]>|Dist::Zilla::Plugin::Git::GatherDir >>
168              
169             =item * Passing exclude rules to L<< C<[GatherDir]>|Dist::Zilla::Plugin::GatherDir >>
170              
171             =back
172              
173             These will of course all still work, because C<source_filename> is read directly from C<< $zilla->root >>
174              
175             Patches to make it read from C<< $zilla->files >> will be accepted, but YAGNI for now.
176              
177             =head1 METHODS
178              
179             =head2 C<gather_files>
180              
181             This module subscribes to the L<< C<-FileGatherer>|Dist::Zilla::Role::FileGatherer >> role.
182              
183             As such, this module injects a L<< C<FromCode>|Dist::Zilla::File::FromCode >> object during the gather phase.
184              
185             =head1 ATTRIBUTES
186              
187             =head2 C<filename>
188              
189             The name of the file to emit.
190              
191             B<DEFAULT>:
192              
193             dist.ini.baked
194              
195             =head2 C<source_filename>
196              
197             The name of the file to read
198              
199             B<DEFAULT:>
200              
201             dist.ini
202              
203             =head1 AUTHOR
204              
205             Kent Fredric <kentnl@cpan.org>
206              
207             =head1 COPYRIGHT AND LICENSE
208              
209             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
210              
211             This is free software; you can redistribute it and/or modify it under
212             the same terms as the Perl 5 programming language system itself.
213              
214             =cut