blib/lib/Any/Template/ProcessDir.pm

filter: Statementstmt 96.1% 99 / 103 Branchbran 75.0% 21 / 28 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 84.0% 21 / 25 Podpod 20.0% 1 / 5 totaltotal 86.3% 146 / 169 CC 18 SCAR 11.1
CC
18
Cov
90%
generate_dest_file
7.1
process_dir
5.0
BUILD
4.1
CRAP 3.0
↑ Summary
1 package Any::Template::ProcessDir;
2 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use File::Find::Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 our $VERSION = '0.08'; #VERSION
14  
15 3 has 'dest_dir' => ( is => 'ro' );
16 3 has 'dir' => ( is => 'ro' );
17 3 has 'dir_create_mode' => ( is => 'ro', isa => 'Int', default => oct(775) );
18 3 has 'file_create_mode' => ( is => 'ro', isa => 'Int', default => oct(444) );
19 0 ▶has 'ignore_files' => ( is => 'ro', isa => 'CodeRef', default => sub { sub { 0 } } );
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
20 3 has 'process_file' => ( is => 'ro', isa => 'CodeRef', lazy_build => 1 );
21 3 has 'process_text' => ( is => 'ro', isa => 'CodeRef', lazy_build => 1 );
22 3 has 'readme_filename' => ( is => 'ro', default => 'README' );
23 3 has 'same_dir' => ( is => 'ro', init_arg => undef );
24 3 has 'source_dir' => ( is => 'ro' );
25 3 has 'template_file_regex' => ( is => 'ro', lazy_build => 1 );
26 3 has 'template_file_suffix' => ( is => 'ro', default => '.src' );
27  
28 4 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.0
Pod undocumented
29 4 my ( $self, $params ) = @_;
30  
31 4 ▶ die "you must pass one of dir and source_dir/dest_dir"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->dir == (defined $self->source_dir && defined $self->dest_dir) 0 4 4
Condition66%2/3
defined $self->source_dir && defined $self->dest_dir
!ll&&!rl&&r
202
MC/DC50%1/2
defined $self->source_dir && defined $self->dest_dir
defined $self->source_dir defined $self->dest_dir
Truth table66%2/3
A && B
A= defined $self->source_dirB= defined $self->dest_dir
ABresult
0X0
100
111
32 if (
33 defined( $self->dir ) ==
34 ( defined( $self->source_dir ) && defined( $self->dest_dir ) ) );
35 4 ▶ if ( defined( $self->dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->dir) 2 2 4
36 2 $self->{same_dir} = 1;
37 2 $self->{source_dir} = $self->{dest_dir} = $self->dir;
38 }
39 }
40  
41 4 ▶sub _build_template_file_regex {
Subroutine _build_template_file_regex: called CC=2 SCAR=7.1
42 4 my $self = shift;
43 4 my $template_file_suffix = $self->template_file_suffix;
44 return
45 4 ▶ defined($template_file_suffix) ? qr/\Q$template_file_suffix\E$/ : qr/.|/;
Branch: 1 branch
BranchTrueFalseTotal
defined $template_file_suffix ? : 4 0 4
46 }
47  
48 8 ▶sub process_dir {
Subroutine process_dir: called CC=5 SCAR=16.1
Pod documented
49 8 my ($self) = @_;
50  
51 8 my $source_dir = $self->source_dir;
52 8 my $dest_dir = $self->dest_dir;
53  
54 8 ▶ if ( !$self->same_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->same_dir) 4 4 8
55 4 remove_tree($dest_dir);
56 4 ▶ die "could not remove '$dest_dir'" if -d $dest_dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest_dir 0 4 4
57 }
58  
59 8 my $ignore_files = $self->ignore_files;
60 my @source_files =
61 56 ▶ find_wanted( sub { -f && !$ignore_files->($File::Find::name) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and not &$ignore_files($File::Find::name) 40 16 56
62 8 $source_dir );
63 8 my $template_file_suffix = $self->template_file_suffix;
64  
65 8 foreach my $source_file (@source_files) {
66 36 $self->generate_dest_file($source_file);
67 }
68  
69 8 ▶ if ( !$self->same_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->same_dir) 4 4 8
70 4 $self->generate_readme();
71 4 ▶ try { $self->generate_source_symlink() };
Subroutine __ANON__: called CC=1 SCAR=0.0
72 }
73 }
74  
75 36 ▶sub generate_dest_file {
Subroutine generate_dest_file: called CC=7 SCAR=19.5
Pod undocumented
76 36 my ( $self, $source_file ) = @_;
77  
78 36 my $template_file_regex = $self->template_file_regex;
79 36 substr( ( my $dest_file = $source_file ), 0, length( $self->source_dir ) ) =
80 $self->dest_dir;
81  
82 36 my $dest_text;
83 36 ▶ if ( $source_file =~ $template_file_regex ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source_file =~ /$template_file_regex/) { } 14 22 36
elsif (not $self->same_dir) { } 6 16 22
84 14 ▶ $dest_file =
Condition50%1/2
$self->template_file_suffix || ''
l!l
140
MC/DC0%0/1
$self->template_file_suffix || ''
$self->template_file_suffix
Truth table50%1/2
A
A= $self->template_file_suffix
Aresult
11
00
85 substr( $dest_file, 0,
86 -1 * length( $self->template_file_suffix || '' ) );
87 14 my $code = $self->process_file;
88 14 $dest_text = $code->( $source_file, $self );
89 }
90 elsif ( !$self->same_dir ) {
91 6 $dest_text = read_file($source_file);
92 }
93 else {
94 16 return;
95 }
96  
97 20 ▶ if ( $self->same_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->same_dir) { } 8 12 20
98 8 unlink($dest_file);
99 }
100 else {
101 12 make_path( dirname($dest_file) );
102 12 ▶ chmod( $self->dir_create_mode(), dirname($dest_file) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->dir_create_mode 12 0 12
103 if defined( $self->dir_create_mode() );
104 }
105  
106 20 write_file( $dest_file, $dest_text );
107 20 ▶ chmod( $self->file_create_mode(), $dest_file )
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->file_create_mode 20 0 20
108 if defined( $self->file_create_mode() );
109 }
110  
111 2 ▶sub _build_process_file {
Subroutine _build_process_file: called CC=1 SCAR=0.0
112 7 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 7 my ( $file, $self ) = @_;
114  
115 7 my $code = $self->process_text;
116 7 return $code->( read_file($file), $self );
117 }
118 2 }
119  
120 0 ▶sub _build_process_text {
Subroutine _build_process_text: not called CC=1 SCAR=6.9
121 0 ▶ return sub { die "must specify one of process_file or process_text" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 }
123  
124 4 ▶sub generate_readme {
Subroutine generate_readme: called CC=2 SCAR=7.0
Pod undocumented
125 4 my $self = shift;
126  
127 4 ▶ if ( defined( $self->readme_filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->readme_filename) 4 0 4
128 4 my $readme_file = catfile( $self->dest_dir, $self->readme_filename );
129 4 unlink($readme_file);
130 4 write_file(
131 $readme_file,
132 "Files in this directory generated from "
133 . $self->source_dir . ".\n",
134 "Do not edit files here, as they will be overwritten. Edit the source instead!"
135 );
136 }
137 }
138  
139 4 ▶sub generate_source_symlink {
Subroutine generate_source_symlink: called CC=2 SCAR=7.0
Pod undocumented
140 4 my $self = shift;
141  
142 # Create symlink from dest dir back to source dir.
143 #
144 4 my $source_link = catdir( $self->dest_dir, "source" );
145 4 ▶ unlink($source_link) if -e $source_link;
Branch: 1 branch
BranchTrueFalseTotal
if -e $source_link 0 4 4
146 4 symlink( $self->source_dir, $source_link );
147 }
148  
149 3 1;
150  
151 __END__
↑ Summary