blib/lib/HTML/Latemp/GenMakeHelpers.pm

filter: Statementstmt 94.7% 182 / 192 Branchbran 87.5% 14 / 16 Conditioncond 70.5% 12 / 17 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 90.9% 30 / 33 Podpod 100% 8 / 8 totaltotal 90.2% 249 / 276 CC 20 SCAR 10.2
CC
20
Cov
92%
place_files_into_buckets
8.3
initialize
3.0
get_rules_template
3.0
CRAP 2.8
↑ Summary
1 package HTML::Latemp::GenMakeHelpers;
2 2 $HTML::Latemp::GenMakeHelpers::VERSION = '0.12.0';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package HTML::Latemp::GenMakeHelpers::Base;
9 2 $HTML::Latemp::GenMakeHelpers::Base::VERSION = '0.12.0';
10 sub new
11 13 ▶{
Subroutine new: called CC=1 SCAR=0.0
12 13 my $class = shift;
13 13 my $self = {};
14 13 bless $self, $class;
15 13 $self->initialize(@_);
16 13 return $self;
17 }
18  
19 package HTML::Latemp::GenMakeHelpers::HostEntry;
20 2 $HTML::Latemp::GenMakeHelpers::HostEntry::VERSION = '0.12.0';
21 2 our @ISA = (qw(HTML::Latemp::GenMakeHelpers::Base));
22  
23 2 ▶use Class::XSAccessor accessors =>
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 { 'dest_dir' => 'dest_dir', 'id' => 'id', 'source_dir' => 'source_dir', };
25  
26 sub initialize
27 7 ▶{
Subroutine initialize: called CC=1 SCAR=0.0
28 7 my $self = shift;
29 7 my %args = (@_);
30  
31 7 $self->id( $args{'id'} );
32 7 $self->source_dir( $args{'source_dir'} );
33 7 $self->dest_dir( $args{'dest_dir'} );
34 }
35  
36 package HTML::Latemp::GenMakeHelpers::Error;
37 2 $HTML::Latemp::GenMakeHelpers::Error::VERSION = '0.12.0';
38 2 our @ISA = (qw(HTML::Latemp::GenMakeHelpers::Base));
39  
40 package HTML::Latemp::GenMakeHelpers::Error::UncategorizedFile;
41 2 $HTML::Latemp::GenMakeHelpers::Error::UncategorizedFile::VERSION = '0.12.0';
42 2 our @ISA = (qw(HTML::Latemp::GenMakeHelpers::Error));
43  
44 2 ▶use Class::XSAccessor accessors => { 'file' => 'file', 'host' => 'host', };
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 sub initialize
47 0 ▶{
Subroutine initialize: not called CC=1 SCAR=6.9
48 0 my $self = shift;
49 0 my $args = shift;
50  
51 0 $self->file( $args->{'file'} );
52 0 $self->host( $args->{'host'} );
53  
54 0 return 0;
55 }
56  
57 package HTML::Latemp::GenMakeHelpers;
58  
59  
60 2 our @ISA = (qw(HTML::Latemp::GenMakeHelpers::Base));
61  
62 2 ▶use File::Find::Rule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use File::Basename qw/ basename /;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶use Class::XSAccessor accessors => {
Subroutine BEGIN: called CC=1 SCAR=0.0
66 '_common_buckets' => '_common_buckets',
67 '_base_dir' => 'base_dir',
68 '_filename_lists_post_filter' => '_filename_lists_post_filter',
69 'hosts' => 'hosts',
70 '_hosts_id_map' => 'hosts_id_map',
71 '_out_dir' => '_out_dir',
72 '_out_docs_ext' => '_out_docs_ext',
73 '_docs_build_command_cb' => '_docs_build_command_cb',
74 '_images_dest_varname_cb' => '_images_dest_varname_cb',
75 2 };
76  
77  
78 sub initialize
79 6 ▶{
Subroutine initialize: called CC=3 SCAR=11.0
Pod documented
80 6 my $self = shift;
81 6 my (%args) = (@_);
82  
83 6 $self->_base_dir("src");
84 $self->_filename_lists_post_filter(
85 12 ▶ $args{filename_lists_post_filter} || sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 12 my ($params) = @_;
87 12 return $params->{filenames};
88 }
89 6 ▶ );
Condition66%2/3
$args{'filename_lists_post_filter'} || sub { my($params) = @_; return $params->{'filenames'}; }
l!l&&r!l&&!r
420
MC/DC0%0/2
$args{'filename_lists_post_filter'} || sub { my($params) = @_; return $params->{'filenames'}; }
$args{'filename_lists_post_filter'} sub { my($params) = @_; return $params->{'filenames'}; }
Truth table66%2/3
A || B
A= $args{'filename_lists_post_filter'}B= sub { my($params) = @_; return $params->{'filenames'}; }
ABresult
1X1
011
000
90 $self->hosts(
91 [
92 7 map { HTML::Latemp::GenMakeHelpers::HostEntry->new(%$_), }
93 6 @{ $args{'hosts'} }
94 ]
95 );
96 6 $self->_hosts_id_map( +{ map { $_->{'id'} => $_ } @{ $self->hosts() } } );
97 6 $self->_common_buckets( {} );
98 6 $self->_out_dir( $args{'out_dir'} );
99 6 ▶ $self->_out_docs_ext( $args{'out_docs_ext'} // '.wml' );
Condition100%2/2
$args{"out_docs_ext"} // ".wml"
l!l
33
MC/DC100%1/1
$args{"out_docs_ext"} // ".wml"
$args{"out_docs_ext"}
Truth table100%2/2
A
A= $args{"out_docs_ext"}
Aresult
11
00
100 6 $self->_docs_build_command_cb( $args{'docs_build_command_cb'} );
101 6 $self->_images_dest_varname_cb( $args{'images_dest_varname_cb'} );
102  
103 6 return;
104 }
105  
106 sub _calc_out_path
107 2 ▶{
Subroutine _calc_out_path: called CC=2 SCAR=7.1
108 2 my ( $self, $bn ) = @_;
109  
110 2 my $out_dir = $self->_out_dir;
111  
112 2 ▶ return $out_dir ? File::Spec->catfile( $out_dir, $bn ) : $bn;
Branch: 1 branch
BranchTrueFalseTotal
$out_dir ? : 2 0 2
113 }
114  
115 sub process_all
116 1 ▶{
Subroutine process_all: called CC=2 SCAR=6.9
Pod documented
117 1 my $self = shift;
118 1 my $dir = $self->_base_dir();
119  
120 1 my @hosts = @{ $self->hosts() };
121  
122 1 open my $file_lists_fh, ">", $self->_calc_out_path("include.mak");
123 1 open my $rules_fh, ">", $self->_calc_out_path("rules.mak");
124  
125 1 print {$rules_fh} "COMMON_SRC_DIR = "
126 1 . $self->_hosts_id_map()->{'common'}->{'source_dir'} . "\n\n";
127  
128 1 foreach my $host (@hosts)
129 {
130 2 my $host_outputs = $self->process_host($host);
131 2 print {$file_lists_fh} $host_outputs->{'file_lists'};
132 2 print {$rules_fh} $host_outputs->{'rules'};
133 }
134  
135 1 print {$rules_fh} "latemp_targets: "
136 . join( " ",
137 1 map { '$(' . uc( $_->{'id'} ) . "_TARGETS)" }
138 1 grep { $_->{'id'} ne "common" } @hosts )
139 . "\n\n";
140  
141 1 close($rules_fh);
142 1 close($file_lists_fh);
143 }
144  
145 sub _make_path
146 283 ▶{
Subroutine _make_path: called CC=1 SCAR=0.0
147 283 my $self = shift;
148  
149 283 my $host = shift;
150 283 my $path = shift;
151  
152 283 return $host->source_dir() . "/" . $path;
153 }
154  
155  
156 sub get_initial_buckets
157 7 ▶{
Subroutine get_initial_buckets: called CC=1 SCAR=0.1
Pod documented
158 7 my $self = shift;
159 7 my $host = shift;
160  
161 return [
162 {
163 'name' => "IMAGES",
164 235 ▶ 'filter' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
165 235 my $fn = shift;
166 235 ▶ return ( $fn !~ /\.(?:tt|w)ml\z/ )
Condition100%3/3
!($fn =~ /\.(?:tt|w)ml\z/u) && -f $self->_make_path($host, $fn)
!ll&&!rl&&r
8448103
MC/DC100%2/2
!($fn =~ /\.(?:tt|w)ml\z/u) && -f $self->_make_path($host, $fn)
!($fn =~ /\.(?:tt|w)ml\z/u) -f $self->_make_path($host, $fn)
Truth table100%3/3
not(A) && B
A= !($fn =~ /\.(?:tt|w)ml\z/u)B= -f $self->_make_path($host, $fn)
ABresult
0X0
100
111
167 && ( -f $self->_make_path( $host, $fn ) );
168 },
169 },
170 {
171 'name' => "DIRS",
172 132 ▶ 'filter' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
173 132 return ( -d $self->_make_path( $host, shift ) );
174 },
175 filter_out_common => 1,
176 },
177 {
178 'name' => "DOCS",
179 84 ▶ 'filter' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 84 return shift =~ /\.x?html\.wml\z/;
181 },
182 84 ▶ 'map' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 84 my $fn = shift;
184 84 $fn =~ s{\.wml\z}{};
185 84 return $fn;
186 },
187 },
188 {
189 'name' => "TTMLS",
190 0 ▶ 'filter' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 return shift =~ /\.ttml\z/;
192 },
193 0 ▶ 'map' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 my $fn = shift;
195 0 $fn =~ s{\.ttml\z}{};
196 0 return $fn;
197 },
198 },
199 7 ];
200 }
201  
202 sub _identity
203 151 ▶{
Subroutine _identity: called CC=1 SCAR=0.0
204 151 return shift;
205 }
206  
207 sub _process_bucket
208 28 ▶{
Subroutine _process_bucket: called CC=2 SCAR=6.9
209 28 my ( $self, $bucket ) = @_;
210 return {
211 %$bucket,
212 'results' => [],
213 (
214 28 ▶ ( !exists( $bucket->{'map'} ) )
Branch: 1 branch
BranchTrueFalseTotal
!exists $bucket->{'map'} ? : 14 14 28
215 ? ( 'map' => \&_identity )
216 : ()
217 ),
218 };
219 }
220  
221  
222 sub get_buckets
223 7 ▶{
Subroutine get_buckets: called CC=1 SCAR=0.0
Pod documented
224 7 my ( $self, $host ) = @_;
225  
226 28 return [ map { $self->_process_bucket($_) }
227 7 @{ $self->get_initial_buckets($host) } ];
228 }
229  
230 sub _filter_out_special_files
231 7 ▶{
Subroutine _filter_out_special_files: called CC=2 SCAR=7.0
232 7 my ( $self, $host, $files_ref ) = @_;
233  
234 7 my @files = @$files_ref;
235  
236 7 @files = ( grep { !m{(\A|/)\.svn(/|\z)} } @files );
237 7 @files = ( grep { !/~\z/ } @files );
238 @files = (
239 grep {
240 7 my $bn = basename($_);
241 235 ▶ not( ( $bn =~ /\A\./ ) && ( $bn =~ /\.swp\z/ ) )
Condition33%1/3
$bn =~ /\A\./u && $bn =~ /\.swp\z/u
!ll&&!rl&&r
23500
MC/DC0%0/2
$bn =~ /\A\./u && $bn =~ /\.swp\z/u
$bn =~ /\A\./u $bn =~ /\.swp\z/u
Truth table33%1/3
A && B
A= $bn =~ /\A\./uB= $bn =~ /\.swp\z/u
ABresult
0X0
100
111
242 } @files
243 );
244  
245 7 return \@files;
246 }
247  
248 sub _sort_files
249 7 ▶{
Subroutine _sort_files: called CC=1 SCAR=0.0
250 7 my ( $self, $host, $files_ref ) = @_;
251  
252 7 return [ sort { $a cmp $b } @$files_ref ];
253 }
254  
255  
256 sub get_non_bucketed_files
257 7 ▶{
Subroutine get_non_bucketed_files: called CC=2 SCAR=6.9
Pod documented
258 7 my ( $self, $host ) = @_;
259  
260 7 my $source_dir_path = $host->source_dir();
261  
262 7 my $files = [ File::Find::Rule->in($source_dir_path) ];
263  
264 7 s!^$source_dir_path/!! for @$files;
265 7 $files = [ grep { $_ ne $source_dir_path } @$files ];
266  
267 7 $files = $self->_filter_out_special_files( $host, $files );
268  
269 7 return $self->_sort_files( $host, $files );
270 }
271  
272  
273 sub place_files_into_buckets
274 7 ▶{
Subroutine place_files_into_buckets: called CC=8 SCAR=21.2
Pod documented
275 7 my ( $self, $host, $files, $buckets ) = @_;
276  
277 FILE_LOOP:
278 7 foreach my $f (@$files)
279 {
280 235 foreach my $bucket (@$buckets)
281 {
282 451 ▶ if ( $bucket->{'filter'}->($f) )
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->{"filter"}->($f)) 235 216 451
283 {
284 235 ▶ if ( $host->{'id'} eq "common" )
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"id"} eq "common") 1 234 235
285 {
286 1 $self->_common_buckets->{ $bucket->{name} }->{$f} = 1;
287 }
288  
289 235 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"id"} eq "common" or not $bucket->{"filter_out_common"} && exists $self->_common_buckets->{$bucket->{"name"}}{$f}) 235 0 235
Condition66%4/6
$host->{"id"} eq "common" or not $bucket->{"filter_out_common"} && exists $self->_common_buckets->{$bucket->{"name"}}{$f}
l!l&&r!l&&!r
12340
$bucket->{'filter_out_common'} && exists $self->_common_buckets->{$bucket->{'name'}}{$f}
!ll&&!rl&&r
186480
MC/DC0%0/3
$host->{"id"} eq "common" or not $bucket->{"filter_out_common"} && exists $self->_common_buckets->{$bucket->{"name"}}{$f}
$host->{"id"} eq "common" $bucket->{'filter_out_common'} exists $self->_common_buckets->{$bucket->{'name'}}{$f}
Truth table75%3/4
A or not(B && C)
A= $host->{"id"} eq "common"B= $bucket->{'filter_out_common'}C= exists $self->_common_buckets->{$bucket->{'name'}}{$f}
ABCresult
1XX1
00X1
0101
0110
290 ( $host->{'id'} eq "common" )
291 || (
292 !(
293 $bucket->{'filter_out_common'} && exists(
294 $self->_common_buckets->{ $bucket->{name} }
295 ->{$f}
296 )
297 )
298 )
299 )
300 {
301 235 push @{ $bucket->{'results'} }, $bucket->{'map'}->($f);
302 }
303  
304 235 next FILE_LOOP;
305 }
306 }
307 0 die HTML::Latemp::GenMakeHelpers::Error::UncategorizedFile->new(
308 {
309 'file' => $f,
310 'host' => $host->id(),
311 }
312 );
313 }
314 }
315  
316  
317 sub get_rules_template
318 6 ▶{
Subroutine get_rules_template: called CC=3 SCAR=11.0
Pod documented
319 6 my ( $self, $host ) = @_;
320  
321 6 my $h_dest_star = "\$(X8X_DEST)/%";
322 6 my $wml_path =
323 qq{WML_LATEMP_PATH="\$\$(perl -MFile::Spec -e 'print File::Spec->rel2abs(shift)' '\$\@')"};
324 6 my $dest_dir = $host->dest_dir();
325 6 my $source_dir_path = $host->source_dir();
326 6 my $out_docs_ext = $self->_out_docs_ext;
327  
328 6 my ( $common_cmd, $no_common_cmd );
329 6 ▶ if ( my $cb = $self->_docs_build_command_cb )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_docs_build_command_cb) { } 2 4 6
330 {
331 2 $common_cmd = $cb->( $self, { host => $host, is_common => 1, } );
332 2 $no_common_cmd = $cb->( $self, { host => $host, is_common => '', } );
333 }
334 else
335 {
336 4 $common_cmd =
337 qq#$wml_path ; ( cd \$(COMMON_SRC_DIR) && wml -o "\$\${WML_LATEMP_PATH}" \$(X8X_WML_FLAGS) -DLATEMP_FILENAME=\$(patsubst $h_dest_star,%,\$(patsubst %${out_docs_ext},%,\$@)) \$(patsubst \$(COMMON_SRC_DIR)/%,%,\$<) )#;
338 4 $no_common_cmd =
339 qq#$wml_path ; ( cd \$(X8X_SRC_DIR) && wml -o "\$\${WML_LATEMP_PATH}" \$(X8X_WML_FLAGS) -DLATEMP_FILENAME=\$(patsubst $h_dest_star,%,\$(patsubst %${out_docs_ext},%,\$@)) \$(patsubst \$(X8X_SRC_DIR)/%,%,\$<) )#;
340 }
341 6 my ( $common_images_dest, $no_common_images_dest );
342 6 ▶ if ( my $cb = $self->_images_dest_varname_cb )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_images_dest_varname_cb) { } 1 5 6
343 {
344 1 $common_images_dest =
345 $cb->( $self, { host => $host, is_common => 1, } );
346 1 $no_common_images_dest =
347 $cb->( $self, { host => $host, is_common => '', } );
348 }
349 else
350 {
351 5 $no_common_images_dest = $common_images_dest = 'X8X_DEST';
352 }
353 6 my $ci_h_dest_star = "\$($common_images_dest)/%";
354 6 my $nci_h_dest_star = "\$($no_common_images_dest)/%";
355 6 return <<"EOF";
356  
357 X8X_SRC_DIR := $source_dir_path
358  
359 X8X_DEST := $dest_dir
360  
361 X8X_WML_FLAGS := \$(WML_FLAGS) -DLATEMP_SERVER=x8x
362  
363 X8X_TTML_FLAGS := \$(TTML_FLAGS) -DLATEMP_SERVER=x8x
364  
365 X8X_DOCS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(X8X_DOCS))
366  
367 X8X_DIRS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(X8X_DIRS))
368  
369 X8X_IMAGES_DEST := \$(patsubst %,\$($no_common_images_dest)/%,\$(X8X_IMAGES))
370  
371 X8X_TTMLS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(X8X_TTMLS))
372  
373 X8X_COMMON_IMAGES_DEST := \$(patsubst %,\$($common_images_dest)/%,\$(COMMON_IMAGES))
374  
375 X8X_COMMON_DIRS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(COMMON_DIRS))
376  
377 X8X_COMMON_TTMLS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(COMMON_TTMLS))
378  
379 X8X_COMMON_DOCS_DEST := \$(patsubst %,\$(X8X_DEST)/%,\$(COMMON_DOCS))
380  
381 X8X_TARGETS := \$(X8X_DEST) \$(X8X_DIRS_DEST) \$(X8X_COMMON_DIRS_DEST) \$(X8X_COMMON_IMAGES_DEST) \$(X8X_COMMON_DOCS_DEST) \$(X8X_COMMON_TTMLS_DEST) \$(X8X_IMAGES_DEST) \$(X8X_DOCS_DEST) \$(X8X_TTMLS_DEST)
382  
383 \$(X8X_DOCS_DEST) : $h_dest_star : \$(X8X_SRC_DIR)/%${out_docs_ext} \$(DOCS_COMMON_DEPS)
384 \t$no_common_cmd
385  
386 \$(X8X_TTMLS_DEST) : $h_dest_star : \$(X8X_SRC_DIR)/%.ttml \$(TTMLS_COMMON_DEPS)
387 \tttml -o \$@ \$(X8X_TTML_FLAGS) -DLATEMP_FILENAME=\$(patsubst $h_dest_star,%,\$(patsubst %.ttml,%,\$@)) \$<
388  
389 \$(X8X_DIRS_DEST) : $h_dest_star :
390 \tmkdir -p \$@
391 \ttouch \$@
392  
393 \$(X8X_IMAGES_DEST) : $nci_h_dest_star : \$(X8X_SRC_DIR)/%
394 \t\$(call LATEMP_COPY)
395  
396 \$(X8X_COMMON_IMAGES_DEST) : $ci_h_dest_star : \$(COMMON_SRC_DIR)/%
397 \t\$(call LATEMP_COPY)
398  
399 \$(X8X_COMMON_TTMLS_DEST) : $h_dest_star : \$(COMMON_SRC_DIR)/%.ttml \$(TTMLS_COMMON_DEPS)
400 \tttml -o \$@ \$(X8X_TTML_FLAGS) -DLATEMP_FILENAME=\$(patsubst $h_dest_star,%,\$(patsubst %.ttml,%,\$@)) \$<
401  
402 \$(X8X_COMMON_DOCS_DEST) : $h_dest_star : \$(COMMON_SRC_DIR)/%${out_docs_ext} \$(DOCS_COMMON_DEPS)
403 \t$common_cmd
404  
405 \$(X8X_COMMON_DIRS_DEST) : $h_dest_star :
406 \tmkdir -p \$@
407 \ttouch \$@
408  
409 \$(X8X_DEST):
410 \tmkdir -p \$@
411 \ttouch \$@
412 EOF
413 }
414  
415  
416 sub process_host
417 7 ▶{
Subroutine process_host: called CC=3 SCAR=11.0
Pod documented
418 7 my $self = shift;
419 7 my $host = shift;
420  
421 7 my $dir = $self->_base_dir();
422  
423 7 my $source_dir_path = $host->source_dir();
424  
425 7 my $file_lists_text = "";
426 7 my $rules_text = "";
427  
428 7 my $files = $self->get_non_bucketed_files($host);
429  
430 7 my $buckets = $self->get_buckets($host);
431  
432 7 $self->place_files_into_buckets( $host, $files, $buckets );
433  
434 7 my $id = $host->id();
435 7 my $host_uc = uc($id);
436 7 foreach my $bucket (@$buckets)
437 {
438 28 my $name = $bucket->{name};
439 $file_lists_text .=
440 $host_uc . "_"
441 . $name . " :="
442 . join(
443 "",
444 227 map { " $_" } @{
445 28 $self->_filename_lists_post_filter->(
446 {
447 28 filenames => $bucket->{'results'},
448 bucket => $name,
449 host => $id,
450 }
451 )
452 }
453 ) . "\n";
454 }
455  
456 7 ▶ if ( $id ne "common" )
Branch: 1 branch
BranchTrueFalseTotal
if ($id ne "common") 6 1 7
457 {
458 6 my $rules = $self->get_rules_template($host);
459  
460 6 $rules =~ s!X8X!$host_uc!g;
461 6 $rules =~ s!x8x!$id!ge;
462 6 $rules_text .= $rules;
463 }
464  
465 return {
466 7 'file_lists' => $file_lists_text,
467 'rules' => $rules_text,
468 };
469 }
470  
471 2 1;
472  
473 __END__
↑ Summary