lib/Templer/Site/New.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 68.1% 15 / 22 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 85.3% 76 / 89 CC 15 SCAR 25.6
CC
15
Cov
86%
create
16.1
BEGIN
1.0
BEGIN
1.0
CRAP 12.9
« lib/Templer/Plugin/FileGlob.pm ↑ Summary lib/Templer/Site/Page.pm »
1  
2 =head1 NAME
3  
4 Templer::Site::New - Create a new templer site
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 use strict;
11 use warnings;
12  
13 use Templer::Site::New;
14  
15 my $site = Templer::Site::New->new();
16 $site->create( "/tmp/foo" );
17  
18 =cut
19  
20 =head1 DESCRIPTION
21  
22 This class allows a new C<templer> site to be created on-disk. This
23 involves creating a new input tree, stub configuration file, etc.
24  
25 The content of the new site, and the directory names, are taken from
26 the DATA section of this class.
27  
28 =cut
29  
30 =head1 LICENSE
31  
32 This module is free software; you can redistribute it and/or modify it
33 under the terms of either:
34  
35 a) the GNU General Public License as published by the Free Software
36 Foundation; either version 2, or (at your option) any later version,
37 or
38  
39 b) the Perl "Artistic License".
40  
41 =cut
42  
43 =head1 AUTHOR
44  
45 Steve Kemp <steve@steve.org.uk>
46  
47 =cut
48  
49 =head1 COPYRIGHT AND LICENSE
50  
51 Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
52  
53 This library is free software. You can modify and or distribute it under
54 the same terms as Perl itself.
55  
56 =cut
57  
58 =head1 METHODS
59  
60 =cut
61  
62  
63  
64 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67  
68 package Templer::Site::New;
69  
70 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72  
73 =head2 new
74  
75 The constructor. No arguments are required/recognized.
76  
77 =cut
78  
79 sub new
80 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
81 1 my $class = shift;
82 1 bless {}, $class;
83 }
84  
85  
86 =head2 create
87  
88 Create a new site in the given directory.
89  
90 This method parses and processes the DATA section of this very module,
91 to know which files/directories to create.
92  
93 =cut
94  
95 sub create
96 1 ▶{
Subroutine create: called CC=15 SCAR=27.8
Pod documented
97 1 my ( $self, $base, $force ) = (@_);
98  
99 #
100 # Forced defaults to false, if not specified.
101 #
102 1 ▶ $force = 0 if ( !defined($force) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $force 1 0 1
103  
104  
105 #
106 # Files we created
107 #
108 1 my $created = 0;
109  
110 1 my $name = undef;
111 1 my $marker = undef;
112 1 my $tmp = undef;
113  
114 #
115 # Process our data-section.
116 #
117 1 ▶ while ( my $line = <DATA> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DATA)) 241 1 242
118 {
119 241 chomp($line);
120  
121 #
122 # Making a directory?
123 #
124 241 ▶ if ( $line =~ /^mkdir(.*)/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^mkdir(.*)/) { } 5 236 241
elsif (not $name || $marker and $line =~ /file\s+([^\s]+)\s+([^\s]+)/) { } 5 231 236
125 {
126 5 my $dir = $1;
127 5 $dir =~ s/^\s+|\s+$//g;
128 5 $dir = $base . "/" . $dir;
129  
130 5 ▶ if ( !-d $dir )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 5 0 5
131 {
132 5 File::Path::mkpath( $dir, { verbose => 0 } );
133 }
134  
135 }
136 ▶ elsif ( !$name &&
Condition83%5/6
not $name || $marker and $line =~ /file\s+([^\s]+)\s+([^\s]+)/
!ll&&!rl&&r
221105
$name || $marker
l!l&&r!l&&!r
221015
MC/DC66%2/3
not $name || $marker and $line =~ /file\s+([^\s]+)\s+([^\s]+)/
$name $marker $line =~ /file\s+([^\s]+)\s+([^\s]+)/
Truth table75%3/4
not(A || B) and C
A= $nameB= $markerC= $line =~ /file\s+([^\s]+)\s+([^\s]+)/
ABCresult
1XX0
01X0
0000
0011
137 !$marker &&
138 ( $line =~ /file\s+([^\s]+)\s+([^\s]+)/ ) )
139 {
140  
141 #
142 # Writing to a file?
143 #
144 5 $name = $1;
145 5 $marker = $2;
146 5 $tmp = undef;
147  
148 }
149 else
150 {
151  
152 #
153 # If we have a filename to write to, then append to the temporary
154 # contents - unless we've found the EOF marker.
155 #
156 231 ▶ if ( $name && $marker )
Branch: 1 branch
BranchTrueFalseTotal
if ($name and $marker) 221 10 231
Condition66%2/3
$name and $marker
!ll&&!rl&&r
100221
MC/DC50%1/2
$name and $marker
$name $marker
Truth table66%2/3
A and B
A= $nameB= $marker
ABresult
0X0
100
111
157 {
158 221 ▶ if ( $line eq $marker )
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $marker) { } 5 216 221
159 {
160 5 my $create = 1;
161 5 ▶ if ( -e $base . "/" . $name )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $base . '/' . $name) 0 5 5
162 {
163 0 ▶ $create = 0 unless ($force);
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
164 }
165  
166 5 ▶ if ($create)
Branch: 1 branch
BranchTrueFalseTotal
if ($create) { } 5 0 5
167 {
168 5 my $dst = $base . "/" . $name;
169  
170 5 ▶ open my $handle, ">:utf8", $dst or
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, ">:utf8", $dst or die "Failed to write to '${dst}' - $!" 5 0 5
171 die "Failed to write to '$dst' - $!";
172 5 print $handle $tmp;
173 5 close($handle);
174  
175 5 $created += 1;
176 }
177 else
178 {
179 0 print "WARNING: Refusing to over-write $base/$name\n";
180 }
181  
182 5 $name = undef;
183 5 $marker = undef;
184 5 $tmp = undef;
185 }
186 else
187 {
188 216 $tmp .= $line . "\n";
189 }
190 }
191 }
192  
193 }
194 1 $created;
195 }
196  
197  
198 1 1;
199  
200  
201 __DATA__
« lib/Templer/Plugin/FileGlob.pm ↑ Summary lib/Templer/Site/Page.pm »