lib/HTML/FormFu/Deploy.pm untested

filter: Statementstmt 0.0% 0 / 82 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 / 127 SCAR 48.8
CC
11
Cov
0%
deploy
42.0
file_list
20.0
file_source
12.0
CRAP 132.0
1 use strict;
2  
3 package HTML::FormFu::Deploy;
4 $HTML::FormFu::Deploy::VERSION = '2.06';
5 use warnings;
6  
7 use HTML::FormFu::Constants qw( $EMPTY_STR );
8 use Cwd qw( getcwd );
9 use Fatal qw( open binmode close mkdir );
10 use File::Copy qw( move );
11 use File::Find qw( find );
12 use File::ShareDir qw( dist_file );
13 use File::Spec;
14 use Carp qw( croak );
15  
16 our $SHARE_DIR;
17  
18 if ( -f 'MANIFEST.SKIP' && -d 'share/templates/tt/xhtml' ) {
19 warn "Running as a developer, using the local, not installed templates\n\n"
20 unless ( $ENV{HARNESS_ACTIVE} );
21  
22 my $cwd = getcwd();
23  
24 $SHARE_DIR = File::Spec->catfile( $cwd, 'share/templates/tt/xhtml' );
25 }
26 else {
27  
28 # dist_dir() doesn't reliably return the directory our files are in.
29 # find the path of one of our files, then get the directory from that
30  
31 my $path = dist_file( 'HTML-FormFu', 'templates/tt/xhtml/form' );
32  
33 my ( $volume, $dirs, $file ) = File::Spec->splitpath($path);
34  
35 $SHARE_DIR = File::Spec->catpath( $volume, $dirs, '' );
36 }
37  
38 sub file_list {
39 my @dir;
40  
41 my $wanted = sub {
42 return if /^\./; # skip files beginning with "."
43 return if !-f $File::Find::name; # skip non-files
44  
45 # necessary when using dev files
46 return if $File::Find::name =~ m|/\.svn|;
47  
48 push @dir, $_;
49 };
50  
51 find( $wanted, $SHARE_DIR );
52  
53 return @dir;
54 }
55  
56 sub file_source {
57 my $filename = shift
58 or croak "filename argument required";
59  
60 my $path = File::Spec->catfile( $SHARE_DIR, $filename );
61  
62 croak "unknown filename: '$path'" if !-f $path;
63  
64 open my $fh, '<', $path;
65  
66 my $data = do { local $/; <$fh> };
67  
68 $data = $EMPTY_STR if !defined $data;
69  
70 close $fh;
71  
72 return $data;
73 }
74  
75 sub deploy {
76 my ($dir) = @_;
77  
78 croak "directory argument required" if !defined $dir;
79  
80 if ( !-d $dir ) {
81 warn "creating directory '$dir'\n";
82 mkdir $dir;
83 }
84  
85 for my $filename ( file_list() ) {
86 my $path = File::Spec->catfile( $dir, $filename );
87  
88 if ( -f $path ) {
89 my $bck = $path . ".bck";
90 warn "file '$path' already exists, backing up to $bck\n";
91 my $ok = move( $path, $bck );
92 if ( !$ok ) {
93 warn "failed to backup, skipping file\n$@\n";
94 next;
95 }
96 }
97  
98 my $fh;
99 eval { open $fh, '>', $path };
100 if ($@) {
101 warn "failed to open '$path' for writing, skipping file\n$@\n";
102 next;
103 }
104 binmode $fh;
105 print $fh file_source($filename);
106 close $fh;
107 }
108 return;
109 }
110  
111 1;
112  
113 __END__
114  
115 =pod
116  
117 =encoding UTF-8
118  
119 =head1 NAME
120  
121 HTML::FormFu::Deploy
122  
123 =head1 VERSION
124  
125 version 2.06
126  
127 =head1 AUTHOR
128  
129 Carl Franks <cpan@fireartist.com>
130  
131 =head1 COPYRIGHT AND LICENSE
132  
133 This software is copyright (c) 2018 by Carl Franks.
134  
135 This is free software; you can redistribute it and/or modify it under
136 the same terms as the Perl 5 programming language system itself.
137  
138 =cut