File Coverage

blib/lib/Dist/Zilla/Plugin/Templates.pm
Criterion Covered Total %
statement 42 43 97.6
branch 10 12 83.3
condition 2 3 66.6
subroutine 9 9 100.0
pod 2 2 100.0
total 65 69 94.2


line stmt bran cond sub pod time code
1             # ---------------------------------------------------------------------- copyright and license ---
2             #
3             # file: lib/Dist/Zilla/Plugin/Templates.pm
4             #
5             # Copyright © 2015 Van de Bugger
6             #
7             # This file is part of perl-Dist-Zilla-Plugin-Templates.
8             #
9             # perl-Dist-Zilla-Plugin-Templates is free software: you can redistribute it and/or modify it
10             # under the terms of the GNU General Public License as published by the Free Software Foundation,
11             # either version 3 of the License, or (at your option) any later version.
12             #
13             # perl-Dist-Zilla-Plugin-Templates is distributed in the hope that it will be useful, but WITHOUT
14             # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15             # PURPOSE. See the GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License along with
18             # perl-Dist-Zilla-Plugin-Templates. If not, see <http://www.gnu.org/licenses/>.
19             #
20             # ---------------------------------------------------------------------- copyright and license ---
21              
22             #pod =for :this This is C<Dist::Zilla::Plugin::Templates> module documentation. Read this if you are going to hack or
23             #pod extend C<Dist-Zilla-Plugin-Templates>.
24             #pod
25             #pod =for :those If you want to treat source files as templates, read the L<manual|Dist::Zilla::Plugin::Templates::Manual>. General
26             #pod topics like getting source, building, installing, bug reporting and some others are covered in the
27             #pod F<README>.
28             #pod
29             #pod =head1 DESCRIPTION
30             #pod
31             #pod Implementation of the plugin is trivial. It just consumes few roles which do all the work:
32             #pod C<FileFinderUser> provides a list of files, C<TextTemplater> process them.
33             #pod
34             #pod =head1 SEE ALSO
35             #pod
36             #pod =for :list
37             #pod = L<Dist::Zilla>
38             #pod = L<Dist::Zilla::Role::FileFinderUser>
39             #pod = L<Dist::Zilla::Role::TextTemplater>
40             #pod = L<Text::Template>
41             #pod = L<Dist::Zilla::Plugin::Templates::Manual>
42             #pod
43             #pod =cut
44              
45             # --------------------------------------------------------------------------------------------------
46              
47             package Dist::Zilla::Plugin::Templates;
48              
49 1     1   1860939 use Moose;
  1         2  
  1         8  
50 1     1   4853 use namespace::autoclean;
  1         1  
  1         10  
51 1     1   72 use version 0.77;
  1         41  
  1         10  
52              
53             # ABSTRACT: Treat source files as templates
54             our $VERSION = 'v0.6.2_03'; # TRIAL VERSION
55              
56             with 'Dist::Zilla::Role::FileMunger';
57             with 'Dist::Zilla::Role::FileFinderUser' => {
58             finder_arg_names => [ qw{ template templates } ],
59             default_finders => [ ':NoFiles' ],
60             };
61             with 'Dist::Zilla::Role::TextTemplater' => { -version => 0.007 }; # need nested packages support.
62             with 'Dist::Zilla::Role::ErrorLogger' => { -version => 0.005 };
63              
64 1     1   123 use Carp qw{ croak };
  1         2  
  1         55  
65 1     1   4 use Dist::Zilla::File::OnDisk;
  1         1  
  1         18  
66 1     1   483 use Dist::Zilla::Plugin::Templates::File;
  1         3  
  1         380  
67              
68             # --------------------------------------------------------------------------------------------------
69              
70             #pod =method munge_files
71             #pod
72             #pod This is the primary method of the plugin. It is called by C<Dist::Zilla> during build. The method
73             #pod iterates through the files provided by C<< $self->found_files >> (a method defined in
74             #pod C<FileFinderUser> role) and process each file with C<< $self->fill_in_file >> (a method defined in
75             #pod C<TextTemplater> role). That's all, folks.
76             #pod
77             #pod =cut
78              
79             sub munge_files {
80 18     18 1 280995 my ( $self ) = @_;
81 18         37 for my $file ( @{ $self->found_files } ) {
  18         96  
82             $self->fill_in_file(
83             $file,
84             {
85 43         30799 include => \ &{ sub { # `Text::Template` wants double reference.
86 25     25   318 return $self->include( @_ );
87 43         304 } },
88             },
89             );
90             };
91 16         10823 return;
92             };
93              
94             # --------------------------------------------------------------------------------------------------
95              
96             #pod =method include
97             #pod
98             #pod This method implements same-name template function. Normally, templates should call the function,
99             #pod not method:
100             #pod
101             #pod {{ include( 'filename' ); }}
102             #pod
103             #pod However, if something wrong with the function, file inclusion can be made through the method call:
104             #pod
105             #pod {{ $plugin->include( 'filename' ); }}
106             #pod
107             #pod =cut
108              
109             sub include {
110 25     25 1 41 my ( $self, $arg ) = @_;
111 25 100       178 defined( $arg ) or croak "Can't include undefined file";
112 24         60 my $class = blessed( $arg );
113 24         27 my $file;
114 24 100       77 if ( $class ) {
115 2 100 66     31 $arg->isa( 'Moose::Object' ) and $arg->does( 'Dist::Zilla::Role::File' )
116             or croak "Can't include object of $class class";
117 1         69 $file = $arg; # `$arg` is a file object.
118             } else {
119 22         50 my $name = "$arg"; # `$arg` is a file name.
120 22 50       63 $name ne '' or croak "Can't include file with empty name";
121 22         31 my @files = grep( { $_->name eq $name } @{ $self->zilla->files } );
  110         3474  
  22         723  
122 22 50       708 if ( @files > 1 ) {
123 0         0 croak "Oops: Can't include $name file: more than one file found";
124             };
125 22 100       57 if ( @files ) {
126 21         36 $file = $files[ 0 ];
127             } else {
128 1         24 $file = Dist::Zilla::File::OnDisk->new( { name => $name } );
129             };
130             };
131 23         283 return Dist::Zilla::Plugin::Templates::File->new( {
132             name => $file->name,
133             content => $file->content,
134             _plugin => $self,
135             } );
136             };
137              
138             # --------------------------------------------------------------------------------------------------
139              
140             __PACKAGE__->meta->make_immutable();
141              
142             1;
143              
144             # --------------------------------------------------------------------------------------------------
145              
146             #pod =head1 COPYRIGHT AND LICENSE
147             #pod
148             #pod Copyright (C) 2015 Van de Bugger
149             #pod
150             #pod License GPLv3+: The GNU General Public License version 3 or later
151             #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
152             #pod
153             #pod This is free software: you are free to change and redistribute it. There is
154             #pod NO WARRANTY, to the extent permitted by law.
155             #pod
156             #pod
157             #pod =cut
158              
159             # ------------------------------------------------------------------------------------------------
160             #
161             # file: doc/what.pod
162             #
163             # This file is part of perl-Dist-Zilla-Plugin-Templates.
164             #
165             # ------------------------------------------------------------------------------------------------
166              
167             #pod =encoding UTF-8
168             #pod
169             #pod =head1 WHAT?
170             #pod
171             #pod C<Dist-Zilla-Plugin-Templates> (or just C<Templates> for brevity) is a C<Dist-Zilla> plugin allowing developers
172             #pod to insert Perl code fragments into arbitrary source text files, which become I<templates>. When
173             #pod C<Dist::Zilla> builds the distribution each code fragment is evaluated and replaced with result of
174             #pod evaluation.
175             #pod
176             #pod =cut
177              
178             # end of file #
179              
180              
181             # end of file #
182              
183             __END__
184              
185             =pod
186              
187             =encoding UTF-8
188              
189             =head1 NAME
190              
191             Dist::Zilla::Plugin::Templates - Treat source files as templates
192              
193             =head1 VERSION
194              
195             Version v0.6.2_03, released on 2016-12-20 22:44 UTC.
196             This is a B<trial release>.
197              
198             =head1 WHAT?
199              
200             C<Dist-Zilla-Plugin-Templates> (or just C<Templates> for brevity) is a C<Dist-Zilla> plugin allowing developers
201             to insert Perl code fragments into arbitrary source text files, which become I<templates>. When
202             C<Dist::Zilla> builds the distribution each code fragment is evaluated and replaced with result of
203             evaluation.
204              
205             This is C<Dist::Zilla::Plugin::Templates> module documentation. Read this if you are going to hack or
206             extend C<Dist-Zilla-Plugin-Templates>.
207              
208             If you want to treat source files as templates, read the L<manual|Dist::Zilla::Plugin::Templates::Manual>. General
209             topics like getting source, building, installing, bug reporting and some others are covered in the
210             F<README>.
211              
212             =head1 DESCRIPTION
213              
214             Implementation of the plugin is trivial. It just consumes few roles which do all the work:
215             C<FileFinderUser> provides a list of files, C<TextTemplater> process them.
216              
217             =head1 OBJECT METHODS
218              
219             =head2 munge_files
220              
221             This is the primary method of the plugin. It is called by C<Dist::Zilla> during build. The method
222             iterates through the files provided by C<< $self->found_files >> (a method defined in
223             C<FileFinderUser> role) and process each file with C<< $self->fill_in_file >> (a method defined in
224             C<TextTemplater> role). That's all, folks.
225              
226             =head2 include
227              
228             This method implements same-name template function. Normally, templates should call the function,
229             not method:
230              
231             {{ include( 'filename' ); }}
232              
233             However, if something wrong with the function, file inclusion can be made through the method call:
234              
235             {{ $plugin->include( 'filename' ); }}
236              
237             =head1 SEE ALSO
238              
239             =over 4
240              
241             =item L<Dist::Zilla>
242              
243             =item L<Dist::Zilla::Role::FileFinderUser>
244              
245             =item L<Dist::Zilla::Role::TextTemplater>
246              
247             =item L<Text::Template>
248              
249             =item L<Dist::Zilla::Plugin::Templates::Manual>
250              
251             =back
252              
253             =head1 AUTHOR
254              
255             Van de Bugger <van.de.bugger@gmail.com>
256              
257             =head1 COPYRIGHT AND LICENSE
258              
259             Copyright (C) 2015 Van de Bugger
260              
261             License GPLv3+: The GNU General Public License version 3 or later
262             <http://www.gnu.org/licenses/gpl-3.0.txt>.
263              
264             This is free software: you are free to change and redistribute it. There is
265             NO WARRANTY, to the extent permitted by law.
266              
267             =cut