lib/HTML/FormFu/Deploy.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 126 SCAR 48.8
CC
11
Cov
0%
deploy
42.0
file_list
20.0
file_source
12.0
CRAP 132.0
1 package HTML::FormFu::Deploy;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '2.05'; # VERSION
7  
8 use HTML::FormFu::Constants qw( $EMPTY_STR );
9 use Cwd qw( getcwd );
10 use Fatal qw( open binmode close mkdir );
11 use File::Copy qw( move );
12 use File::Find qw( find );
13 use File::ShareDir qw( dist_file );
14 use File::Spec;
15 use Carp qw( croak );
16  
17 our $SHARE_DIR;
18  
19 if ( -f 'MANIFEST.SKIP' && -d 'share/templates/tt/xhtml' ) {
20 warn
21 "Running as a developer, using the local, not installed templates\n\n"
22 unless ( $ENV{HARNESS_ACTIVE} );
23  
24 my $cwd = getcwd();
25  
26 $SHARE_DIR = File::Spec->catfile( $cwd, 'share/templates/tt/xhtml' );
27 }
28 else {
29  
30 # dist_dir() doesn't reliably return the directory our files are in.
31 # find the path of one of our files, then get the directory from that
32  
33 my $path = dist_file( 'HTML-FormFu', 'templates/tt/xhtml/form' );
34  
35 my ( $volume, $dirs, $file ) = File::Spec->splitpath($path);
36  
37 $SHARE_DIR = File::Spec->catpath( $volume, $dirs, '' );
38 }
39  
40 sub file_list {
41 my @dir;
42  
43 my $wanted = sub {
44 return if /^\./; # skip files beginning with "."
45 return if !-f $File::Find::name; # skip non-files
46  
47 # necessary when using dev files
48 return if $File::Find::name =~ m|/\.svn|;
49  
50 push @dir, $_;
51 };
52  
53 find( $wanted, $SHARE_DIR );
54  
55 return @dir;
56 }
57  
58 sub file_source {
59 my $filename = shift
60 or croak "filename argument required";
61  
62 my $path = File::Spec->catfile( $SHARE_DIR, $filename );
63  
64 croak "unknown filename: '$path'" if !-f $path;
65  
66 open my $fh, '<', $path;
67  
68 my $data = do { local $/; <$fh> };
69  
70 $data = $EMPTY_STR if !defined $data;
71  
72 close $fh;
73  
74 return $data;
75 }
76  
77 sub deploy {
78 my ($dir) = @_;
79  
80 croak "directory argument required" if !defined $dir;
81  
82 if ( !-d $dir ) {
83 warn "creating directory '$dir'\n";
84 mkdir $dir;
85 }
86  
87 for my $filename ( file_list() ) {
88 my $path = File::Spec->catfile( $dir, $filename );
89  
90 if ( -f $path ) {
91 my $bck = $path . ".bck";
92 warn "file '$path' already exists, backing up to $bck\n";
93 my $ok = move( $path, $bck );
94 if ( !$ok ) {
95 warn "failed to backup, skipping file\n$@\n";
96 next;
97 }
98 }
99  
100 my $fh;
101 eval { open $fh, '>', $path };
102 if ($@) {
103 warn "failed to open '$path' for writing, skipping file\n$@\n";
104 next;
105 }
106 binmode $fh;
107 print $fh file_source($filename);
108 close $fh;
109 }
110 return;
111 }
112  
113 1;