File Coverage

lib/Dist/Zilla/Plugin/INI/Baked.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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