File Coverage

blib/lib/Dist/Zilla/Plugin/MungeFile/WithDataSection.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 7 9 77.7
subroutine 5 5 100.0
pod 1 1 100.0
total 36 38 94.7


line stmt bran cond sub pod time code
1 5     5   3736001 use strict;
  5         10  
  5         179  
2 5     5   23 use warnings;
  5         10  
  5         404  
3             package Dist::Zilla::Plugin::MungeFile::WithDataSection; # git description: last-commit-with-mungefile-3-g0f25313
4             # ABSTRACT: Modify files in the build, with templates and DATA section
5             # KEYWORDS: plugin file content injection modification template DATA __DATA__ section
6             # vim: set ts=8 sts=4 sw=4 tw=115 et :
7              
8             our $VERSION = '0.009';
9              
10 5     5   31 use Moose;
  5         11  
  5         49  
11             extends 'Dist::Zilla::Plugin::MungeFile';
12 5     5   30438 use namespace::autoclean;
  5         11  
  5         51  
13              
14             # around dump_config => sub ... no additional configs to add
15              
16             sub munge_file
17             {
18 5     5 1 370099 my ($self, $file) = @_;
19              
20 5         33 my $content = $file->content;
21              
22 5   66     4942 my $end_pos = $content =~ m/\n__END__\n/g && pos($content);
23 5         17 pos($content) = undef;
24              
25 5         12 my $data;
26 5 100 100     55 if ($content =~ m/\n__DATA__\n/spg and (not $end_pos or pos($content) < $end_pos))
      66        
27             {
28 3         9 $data = ${^POSTMATCH};
29 3         23 $data =~ s/\n__END__\n.*$/\n/s;
30             }
31              
32             $self->next::method(
33 5         53 $file,
34             { DATA => \$data },
35             );
36             }
37              
38             __PACKAGE__->meta->make_immutable;
39              
40             __END__
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             Dist::Zilla::Plugin::MungeFile::WithDataSection - Modify files in the build, with templates and DATA section
49              
50             =head1 VERSION
51              
52             version 0.009
53              
54             =head1 SYNOPSIS
55              
56             In your F<dist.ini>:
57              
58             [MungeFile::WithDataSection]
59             file = lib/My/Module.pm
60             house = maison
61              
62             And during the build, F<lib/My/Module.pm>:
63              
64             my @stuff = qw(
65             {{
66             join " \n",
67             map { expensive_build_time_sub($_) }
68             split(' ', $DATA) # awk-style whitespace splitting
69             }}
70             );
71             my ${{ $house }} = 'my castle';
72             __DATA__
73             alpha
74             beta
75             gamma
76              
77             Is transformed to:
78              
79             my @stuff = qw(
80             SOMETHING_WITH_ALPHA
81             SOMETHING_WITH_BETA
82             SOMETHING_WITH_GAMMA
83             );
84             my $maison = 'my castle';
85              
86             =head1 DESCRIPTION
87              
88             =for stopwords FileMunger
89              
90             This is a L<FileMunger|Dist::Zilla::Role::FileMunger> plugin for
91             L<Dist::Zilla> that passes a file(s)
92             through a L<Text::Template>, with a variable provided that contains the
93             content from the file's C<__DATA__> section.
94              
95             L<Text::Template> is used to transform the file by making the C<< $DATA >>
96             variable available to all code blocks within C<< {{ }} >> sections.
97              
98             The data section is extracted by scanning the file for C<< qr/^__DATA__$/ >>,
99             so this may pose a problem for you if you include this string in a here-doc or
100             some other construct. However, this method means we do not have to load the
101             file before applying the template, which makes it much easier to construct
102             your templates in F<.pm> files (i.e. not having to put C<{{> after a comment
103             and inside a C<do> block, as was previously required).
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 BACKGROUND
142              
143             =for stopwords syntactual templater
144              
145             This module was originally a part of the L<Acme::CPANAuthors::Nonhuman>
146             distribution, used to transform a C<DATA> section containing a list of PAUSE
147             ids to their corresponding names, as well as embedded HTML with everyone's
148             avatar images. It used to only work on F<.pm> files, by first loading the
149             module and then reading from a filehandle created from C<< \*{"$pkg\::DATA"} >>.
150             This also required the file to jump through some convoluted syntactual hoops
151             to ensure that the file was still compilable B<before> the template was run.
152             (Check it out and roll your eyes:
153             L<https://github.com/karenetheridge/Acme-CPANAuthors-Nonhuman/blob/v0.005/lib/Acme/CPANAuthors/Nonhuman.pm#L18>)
154              
155             Now that we support munging all file types, we are forced to parse the file
156             more dumbly (by scanning for C<qr/^__DATA__/>), which also removes the need
157             for these silly syntax games. The moral of the story is that simple code
158             usually B<is> better!
159              
160             I have also since split off much of this distribution into a superclass
161             plugin, L<Dist::Zilla::Plugin::MungeFile>, which provides a general-use munger
162             and templater without the C<__DATA__> support.
163              
164             =head1 SUPPORT
165              
166             =for stopwords irc
167              
168             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-MungeFile::WithDataSection>
169             (or L<bug-Dist-Zilla-Plugin-MungeFile::WithDataSection@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-MungeFile::WithDataSection@rt.cpan.org>).
170             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
171              
172             =head1 SEE ALSO
173              
174             =over 4
175              
176             =item *
177              
178             L<Dist::Zilla::Plugin::Substitute>
179              
180             =item *
181              
182             L<Dist::Zilla::Plugin::GatherDir::Template>
183              
184             =item *
185              
186             L<Dist::Zilla::Plugin::MungeFile>
187              
188             =item *
189              
190             L<Dist::Zilla::Plugin::MungeFile::WithConfigFile>
191              
192             =back
193              
194             =head1 AUTHOR
195              
196             Karen Etheridge <ether@cpan.org>
197              
198             =head1 COPYRIGHT AND LICENCE
199              
200             This software is copyright (c) 2013 by Karen Etheridge.
201              
202             This is free software; you can redistribute it and/or modify it under
203             the same terms as the Perl 5 programming language system itself.
204              
205             =cut