Templates.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 93.5% 73 / 78 CC 9 SCAR 15.0
CC
9
Cov
94%
include
8.1
munge_files
2.0
BEGIN
1.0
CRAP 4.5
↑ Summary Templates/File.pm »
1 # ---------------------------------------------------------------------- copyright and license ---
2 #
3 # file: lib/Dist/Zilla/Plugin/Templates.pm
4 #
5 # Copyright © 2015, 2016, 2018 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 # SPDX-License-Identifier: GPL-3.0-or-later
21 #
22 # ---------------------------------------------------------------------- copyright and license ---
23  
24 #pod =for :this This is C<Dist::Zilla::Plugin::Templates> module documentation. Read this if you are going to hack or
25 #pod extend C<Dist-Zilla-Plugin-Templates>.
26 #pod
27 #pod =for :those If you want to treat source files as templates, read the L<manual|Dist::Zilla::Plugin::Templates::Manual>. General
28 #pod topics like getting source, building, installing, bug reporting and some others are covered in the
29 #pod F<README>.
30 #pod
31 #pod =head1 DESCRIPTION
32 #pod
33 #pod Implementation of the plugin is trivial. It just consumes few roles which do all the work:
34 #pod C<FileFinderUser> provides a list of files, C<TextTemplater> process them.
35 #pod
36 #pod =head1 SEE ALSO
37 #pod
38 #pod =for :list
39 #pod = L<Dist::Zilla>
40 #pod = L<Dist::Zilla::Role::FileFinderUser>
41 #pod = L<Dist::Zilla::Role::TextTemplater>
42 #pod = L<Text::Template>
43 #pod = L<Dist::Zilla::Plugin::Templates::Manual>
44 #pod
45 #pod =cut
46  
47 # --------------------------------------------------------------------------------------------------
48  
49 package Dist::Zilla::Plugin::Templates;
50  
51 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # ABSTRACT: Treat source files as templates
56 2 our $VERSION = 'v0.6.5'; # VERSION
57  
58 2 with 'Dist::Zilla::Role::FileMunger';
59 2 with 'Dist::Zilla::Role::FileFinderUser' => {
60 finder_arg_names => [ qw{ template templates } ],
61 default_finders => [ ':NoFiles' ],
62 };
63 2 with 'Dist::Zilla::Role::TextTemplater' => { -version => 0.007 }; # need nested packages support.
64 2 with 'Dist::Zilla::Role::ErrorLogger' => { -version => 0.005 };
65  
66 2 ▶use Carp qw{ croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use Dist::Zilla::File::OnDisk;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Dist::Zilla::Plugin::Templates::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 # --------------------------------------------------------------------------------------------------
71  
72 #pod =method munge_files
73 #pod
74 #pod This is the primary method of the plugin. It is called by C<Dist::Zilla> during build. The method
75 #pod iterates through the files provided by C<< $self->found_files >> (a method defined in
76 #pod C<FileFinderUser> role) and process each file with C<< $self->fill_in_file >> (a method defined in
77 #pod C<TextTemplater> role). That's all, folks.
78 #pod
79 #pod =cut
80  
81 18 ▶sub munge_files {
Subroutine munge_files: called CC=2 SCAR=6.9
Pod documented
82 18 my ( $self ) = @_;
83 18 for my $file ( @{ $self->found_files } ) {
84 $self->fill_in_file(
85 $file,
86 {
87 43 ▶ include => \ &{ sub { # `Text::Template` wants double reference.
Subroutine __ANON__: called CC=1 SCAR=0.0
88 25 return $self->include( @_ );
89 43 } },
90 },
91 );
92 };
93 16 return;
94 };
95  
96 # --------------------------------------------------------------------------------------------------
97  
98 #pod =method include
99 #pod
100 #pod This method implements same-name template function. Normally, templates should call the function,
101 #pod not method:
102 #pod
103 #pod {{ include( 'filename' ); }}
104 #pod
105 #pod However, if something wrong with the function, file inclusion can be made through the method call:
106 #pod
107 #pod {{ $plugin->include( 'filename' ); }}
108 #pod
109 #pod =cut
110  
111 25 ▶sub include {
Subroutine include: called CC=8 SCAR=20.9
Pod documented
112 25 my ( $self, $arg ) = @_;
113 25 ▶ defined( $arg ) or croak "Can't include undefined file";
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or croak("Can't include undefined file") 24 1 25
114 24 my $class = blessed( $arg );
115 24 my $file;
116 24 ▶ if ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 2 22 24
117 2 ▶ $arg->isa( 'Moose::Object' ) and $arg->does( 'Dist::Zilla::Role::File' )
Branch: 1 branch
BranchTrueFalseTotal
$arg->isa("Moose::Object") and $arg->does("Dist::Zilla::Role::File") or croak("Can't include object of $class class") 1 1 2
Condition66%2/3
$arg->isa("Moose::Object") and $arg->does("Dist::Zilla::Role::File")
!ll&&!rl&&r
011
MC/DC50%1/2
$arg->isa("Moose::Object") and $arg->does("Dist::Zilla::Role::File")
$arg->isa("Moose::Object") $arg->does("Dist::Zilla::Role::File")
Truth table66%2/3
A and B
A= $arg->isa("Moose::Object")B= $arg->does("Dist::Zilla::Role::File")
ABresult
0X0
100
111
118 or croak "Can't include object of $class class";
119 1 $file = $arg; # `$arg` is a file object.
120 } else {
121 22 my $name = "$arg"; # `$arg` is a file name.
122 22 ▶ $name ne '' or croak "Can't include file with empty name";
Branch: 1 branch
BranchTrueFalseTotal
$name ne "" or croak("Can't include file with empty name") 22 0 22
123 22 my @files = grep( { $_->name eq $name } @{ $self->zilla->files } );
124 22 ▶ if ( @files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files > 1) 0 22 22
125 0 croak "Oops: Can't include $name file: more than one file found";
126 };
127 22 ▶ if ( @files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) { } 21 1 22
128 21 $file = $files[ 0 ];
129 } else {
130 1 $file = Dist::Zilla::File::OnDisk->new( { name => $name } );
131 };
132 };
133 23 return Dist::Zilla::Plugin::Templates::File->new( {
134 name => $file->name,
135 content => $file->content,
136 _plugin => $self,
137 } );
138 };
139  
140 # --------------------------------------------------------------------------------------------------
141  
142 2 __PACKAGE__->meta->make_immutable();
143  
144 2 1;
145  
146 # --------------------------------------------------------------------------------------------------
147  
148 #pod =head1 COPYRIGHT AND LICENSE
149 #pod
150 #pod Copyright (C) 2015, 2016, 2018 Van de Bugger
151 #pod
152 #pod License GPLv3+: The GNU General Public License version 3 or later
153 #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
154 #pod
155 #pod This is free software: you are free to change and redistribute it. There is
156 #pod NO WARRANTY, to the extent permitted by law.
157 #pod
158 #pod
159 #pod =cut
160  
161 # ------------------------------------------------------------------------------------------------
162 #
163 # file: doc/what.pod
164 #
165 # Copyright © 2015, 2016, 2018 Van de Bugger.
166 #
167 # This file is part of perl-Dist-Zilla-Plugin-Templates.
168 #
169 # SPDX-License-Identifier: GPL-3.0-or-later
170 #
171 # ------------------------------------------------------------------------------------------------
172  
173 #pod =encoding UTF-8
174 #pod
175 #pod =head1 WHAT?
176 #pod
177 #pod C<Dist-Zilla-Plugin-Templates> (or just C<Templates> for brevity) is a C<Dist-Zilla> plugin allowing developers
178 #pod to insert Perl code fragments into arbitrary source text files, which become I<templates>. When
179 #pod C<Dist::Zilla> builds the distribution each code fragment is evaluated and replaced with result of
180 #pod evaluation.
181 #pod
182 #pod =cut
183  
184 # end of file #
185  
186  
187 # end of file #
188  
189 __END__
↑ Summary Templates/File.pm »