File Coverage

blib/lib/Dist/Zilla/Plugin/MungeFile/WithConfigFile.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 20 20 100.0


line stmt bran cond sub pod time code
1 2     2   1567634 use strict;
  2         3  
  2         77  
2 2     2   10 use warnings;
  2         3  
  2         170  
3             package Dist::Zilla::Plugin::MungeFile::WithConfigFile; # git description: v0.003-2-g068a2cc
4             # ABSTRACT: Modify files in the build, with templates and config data from a file
5             # KEYWORDS: plugin file content injection modification template configuration file
6             # vim: set ts=8 sts=4 sw=4 tw=115 et :
7              
8             our $VERSION = '0.004';
9              
10 2     2   14 use Moose;
  2         3  
  2         89  
11             extends 'Dist::Zilla::Plugin::MungeFile';
12             with 'MooseX::SimpleConfig';
13 2     2   15768 use namespace::autoclean;
  2         5  
  2         27  
14              
15             has configfile => (
16             is => 'ro', isa => 'Str',
17             required => 1,
18             );
19              
20             has _config_data => (
21             is => 'ro', isa => 'HashRef',
22             lazy => 1,
23             default => sub {
24             my $self = shift;
25             $self->get_config_from_file($self->configfile);
26             },
27             );
28              
29             around dump_config => sub
30             {
31             my $orig = shift;
32             my $self = shift;
33              
34             my $config = $self->$orig;
35              
36             $config->{'' . __PACKAGE__} = {
37             configfile => $self->configfile,
38             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
39             };
40              
41             return $config;
42             };
43              
44             sub munge_file
45             {
46 2     2 1 151443 my ($self, $file) = @_;
47              
48 2         112 $self->next::method(
49             $file,
50             { config_data => \($self->_config_data) },
51             );
52             }
53              
54             __PACKAGE__->meta->make_immutable;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Dist::Zilla::Plugin::MungeFile::WithConfigFile - Modify files in the build, with templates and config data from a file
65              
66             =head1 VERSION
67              
68             version 0.004
69              
70             =head1 SYNOPSIS
71              
72             In your F<dist.ini>:
73              
74             [MungeFile::WithConfigFile]
75             file = lib/My/Module.pm
76             house = maison
77             configfile = data.json
78              
79             And during the build, F<lib/My/Module.pm>:
80              
81             my $some_string = '{{ expensive_build_time_sub($config_data{some_field}) }}';
82             my ${{ $house }} = 'my castle';
83              
84             Is transformed to:
85              
86             my $some_string = 'something derived from data in config file';
87             my $maison = 'my castle';
88              
89             =head1 DESCRIPTION
90              
91             =for stopwords FileMunger
92              
93             This is a L<FileMunger|Dist::Zilla::Role::FileMunger> plugin for
94             L<Dist::Zilla> that passes a file(s)
95             through a L<Text::Template>, with a variable provided that contains data
96             read from the provided config file.
97              
98             L<Text::Template> is used to transform the file by making the C<< $config_data >>
99             variable available to all code blocks within C<< {{ }} >> sections.
100              
101             This data is extracted from the provided C<configfile> using L<Config::Any>,
102             so a variety of file formats are supported, including C<JSON>, C<YAML> and
103             C<INI>.
104              
105             The L<Dist::Zilla> object (as C<$dist>) and this plugin (as C<$plugin>) are
106             also made available to the template, for extracting other information about
107             the build.
108              
109             Additionally, any extra keys and values you pass to the plugin are passed
110             along in variables named for each key.
111              
112             =for Pod::Coverage munge_files munge_file mvp_aliases
113              
114             =head1 OPTIONS
115              
116             =head2 C<finder>
117              
118             =for stopwords FileFinder
119              
120             This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder> for finding
121             files to modify.
122              
123             Other pre-defined finders are listed in
124             L<Dist::Zilla::Role::FileFinderUser/default_finders>.
125             You can define your own with the
126             L<[FileFinder::ByName]|Dist::Zilla::Plugin::FileFinder::ByName> plugin.
127              
128             There is no default.
129              
130             =head2 C<file>
131              
132             Indicates the filename in the dist to be operated upon; this file can exist on
133             disk, or have been generated by some other plugin. Can be included more than once.
134              
135             B<At least one of the C<finder> or C<file> options is required.>
136              
137             =head2 C<arbitrary option>
138              
139             All other keys/values provided will be passed to the template as is.
140              
141             =head1 CAVEATS
142              
143             Presently, the config file is not read with any sort of file decoding (e.g.
144             UTF-8), so any extracted strings should be appropriately decoded first. This
145             is an issue that needs to be resolved in L<Config::Any> (perhaps by having the
146             caller, in our case L<MooseX::SimpleConfig>, to pass the desired decoding).
147              
148             =head1 SUPPORT
149              
150             =for stopwords irc
151              
152             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-MungeFile-WithConfigFile>
153             (or L<bug-Dist-Zilla-Plugin-MungeFile-WithConfigFile@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-MungeFile-WithConfigFile@rt.cpan.org>).
154             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
155              
156             =head1 SEE ALSO
157              
158             =over 4
159              
160             =item *
161              
162             L<Dist::Zilla::Plugin::Substitute>
163              
164             =item *
165              
166             L<Dist::Zilla::Plugin::GatherDir::Template>
167              
168             =item *
169              
170             L<Dist::Zilla::Plugin::MungeFile>
171              
172             =item *
173              
174             L<Dist::Zilla::Plugin::MungeFile::WithDataSection>
175              
176             =back
177              
178             =head1 AUTHOR
179              
180             Karen Etheridge <ether@cpan.org>
181              
182             =head1 COPYRIGHT AND LICENCE
183              
184             This software is copyright (c) 2014 by Karen Etheridge.
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut