File Coverage

lib/Templer/Site/New.pm
Criterion Covered Total %
statement 43 45 95.5
branch 13 20 65.0
condition 7 9 77.7
subroutine 5 5 100.0
pod 2 2 100.0
total 70 81 86.4


line stmt bran cond sub pod time code
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 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
46              
47             =cut
48              
49             =head1 COPYRIGHT AND LICENSE
50              
51             Copyright (C) 2012-2015 Steve Kemp .
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     1   73109 use strict;
  1         10  
  1         23  
65 1     1   4 use warnings;
  1         2  
  1         31  
66              
67              
68             package Templer::Site::New;
69              
70 1     1   4 use File::Path qw(mkpath);
  1         1  
  1         370  
71              
72              
73             =head2 new
74              
75             The constructor. No arguments are required/recognized.
76              
77             =cut
78              
79             sub new
80             {
81 1     1 1 557 my $class = shift;
82 1         3 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             {
97 1     1 1 3591 my ( $self, $base, $force ) = (@_);
98              
99             #
100             # Forced defaults to false, if not specified.
101             #
102 1 50       4 $force = 0 if ( !defined($force) );
103              
104              
105             #
106             # Files we created
107             #
108 1         3 my $created = 0;
109              
110 1         2 my $name = undef;
111 1         1 my $marker = undef;
112 1         1 my $tmp = undef;
113              
114             #
115             # Process our data-section.
116             #
117 1         5 while ( my $line = )
118             {
119 241         212 chomp($line);
120              
121             #
122             # Making a directory?
123             #
124 241 100 66     479 if ( $line =~ /^mkdir(.*)/ )
    100 100        
125             {
126 5         10 my $dir = $1;
127 5         23 $dir =~ s/^\s+|\s+$//g;
128 5         12 $dir = $base . "/" . $dir;
129              
130 5 50       50 if ( !-d $dir )
131             {
132 5         544 File::Path::mkpath( $dir, { verbose => 0 } );
133             }
134              
135             }
136             elsif ( !$name &&
137             !$marker &&
138             ( $line =~ /file\s+([^\s]+)\s+([^\s]+)/ ) )
139             {
140              
141             #
142             # Writing to a file?
143             #
144 5         24 $name = $1;
145 5         9 $marker = $2;
146 5         11 $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 100 66     397 if ( $name && $marker )
157             {
158 221 100       211 if ( $line eq $marker )
159             {
160 5         6 my $create = 1;
161 5 50       71 if ( -e $base . "/" . $name )
162             {
163 0 0       0 $create = 0 unless ($force);
164             }
165              
166 5 50       12 if ($create)
167             {
168 5         12 my $dst = $base . "/" . $name;
169              
170 5 50       256 open my $handle, ">:utf8", $dst or
171             die "Failed to write to '$dst' - $!";
172 5         75 print $handle $tmp;
173 5         134 close($handle);
174              
175 5         26 $created += 1;
176             }
177             else
178             {
179 0         0 print "WARNING: Refusing to over-write $base/$name\n";
180             }
181              
182 5         5 $name = undef;
183 5         5 $marker = undef;
184 5         13 $tmp = undef;
185             }
186             else
187             {
188 216         356 $tmp .= $line . "\n";
189             }
190             }
191             }
192              
193             }
194 1         6 $created;
195             }
196              
197              
198             1;
199              
200              
201             __DATA__