blib/lib/Text/Xslate/Runner.pm

filter: Statementstmt 98.4% 186 / 189 Branchbran 83.3% 55 / 66 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 7 totaltotal 90.8% 269 / 296 CC 43 SCAR 20.7
CC
43
Cov
94%
run
15.1
process_file
12.3
_build_getopt_spec
8.0
CRAP 7.9
« blib/lib/Text/Xslate/Compiler.pm ↑ Summary blib/lib/Text/Xslate/PP.pm »
1 package Text::Xslate::Runner;
2 10 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 {
11 package
12 Text::Xslate::Runner::Getopt;
13 10 ▶ use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has cmd_aliases => (
16 is => 'ro',
17 isa => 'ArrayRef[Str]',
18 20 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 10 auto_deref => 1,
20 );
21  
22 10 ▶ no Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 }
24  
25 10 my $getopt = Getopt::Long::Parser->new(
26 config => [qw(
27 no_ignore_case
28 bundling
29 no_auto_abbrev
30 )],
31 );
32  
33 10 my $Pattern = subtype __PACKAGE__ . '.Pattern', as 'RegexpRef';
34 2 ▶coerce $Pattern => from 'Str' => via { qr/$_/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 10 my $getopt_traits = ['Text::Xslate::Runner::Getopt'];
37  
38 10 has cache_dir => (
39 documentation => 'Directory the cache files will be saved in',
40 cmd_aliases => [qw(c)],
41 is => 'ro',
42 isa => 'Str',
43 predicate => 'has_cache_dir',
44 traits => $getopt_traits,
45 );
46  
47 10 has cache => (
48 documentation => 'Cache level',
49 cmd_aliases => [qw(a)],
50 is => 'ro',
51 isa => 'Int',
52 predicate => 'has_cache',
53 traits => $getopt_traits,
54 );
55  
56 10 has module => (
57 documentation => 'Modules templates will use (e.g. name=sub1,sub2)',
58 cmd_aliases => [qw(M)],
59 is => 'ro',
60 isa => 'HashRef[Str]',
61 predicate => 'has_module',
62 traits => $getopt_traits,
63 );
64  
65 10 has input_encoding => (
66 documentation => 'Input encoding (default: UTF-8)',
67 cmd_aliases => [qw(ie)],
68 is => 'rw',
69 isa => 'Str',
70 default => 'UTF-8',
71 predicate => 'has_input_encoding',
72 traits => $getopt_traits,
73 );
74  
75 10 has output_encoding => (
76 documentation => 'Output encoding (default: UTF-8)',
77 cmd_aliases => [qw(oe)],
78 is => 'rw',
79 isa => 'Str',
80 default => 'UTF-8',
81 predicate => 'has_output_encoding',
82 traits => $getopt_traits,
83 );
84  
85  
86 10 has path => (
87 documentation => 'Include paths',
88 cmd_aliases => [qw(I)],
89 is => 'ro',
90 isa => 'ArrayRef[Str]',
91 predicate => 'has_path',
92 traits => $getopt_traits,
93 );
94  
95 10 has syntax => (
96 documentation => 'Template syntax (e.g. TTerse)',
97 cmd_aliases => [qw(s)],
98 is => 'ro',
99 isa => 'Str',
100 predicate => 'has_syntax',
101 traits => $getopt_traits,
102 );
103  
104 10 has type => (
105 documentation => 'Output content type (html | xml | text)',
106 cmd_aliases => [qw(t)],
107 is => 'ro',
108 isa => 'Str',
109 predicate => 'has_type',
110 traits => $getopt_traits,
111 );
112  
113 10 has verbose => (
114 documentation => 'Warning level (default: 2)',
115 cmd_aliases => [qw(w)],
116 is => 'ro',
117 isa => 'Str',
118 default => 2,
119 predicate => 'has_verbose',
120 traits => $getopt_traits,
121 );
122  
123 # --ignore=pattern
124 10 has ignore => (
125 documentation => 'Regular expression the process will ignore',
126 cmd_aliases => [qw(i)],
127 is => 'ro',
128 isa => $Pattern,
129 coerce => 1,
130 traits => $getopt_traits,
131 );
132  
133 # --suffix old=new
134 has suffix => (
135 documentation => 'Output suffix mapping (e.g. tx=html)',
136 cmd_aliases => [qw(x)],
137 is => 'ro',
138 isa => 'HashRef',
139 20 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
140 10 traits => $getopt_traits,
141 );
142  
143 10 has dest => (
144 documentation => 'Destination directory',
145 cmd_aliases => [qw(o)],
146 is => 'ro',
147 isa => 'Str', # Maybe[Str]
148 required => 0,
149 traits => $getopt_traits,
150 );
151  
152 10 has define => (
153 documentation => 'Define template variables (e.g. foo=bar)',
154 cmd_aliases => [qw(D)],
155 is => 'ro',
156 isa => 'HashRef',
157 predicate => 'has_define',
158 traits => $getopt_traits,
159 );
160  
161 10 has eval => (
162 documentation => 'One line of template code',
163 cmd_aliases => [qw(e)],
164 is => 'ro',
165 isa => 'Str',
166 predicate => 'has_eval',
167 traits => $getopt_traits,
168 );
169  
170 10 has engine => (
171 documentation => 'Template engine',
172 cmd_aliases => [qw(E)],
173 is => 'ro',
174 isa => 'Str',
175 default => 'Text::Xslate',
176 traits => $getopt_traits,
177 );
178  
179 10 has debug => (
180 documentation => 'Debugging flags',
181 cmd_aliases => ['d'],
182 is => 'ro',
183 isa => 'Str',
184 predicate => 'has_debug',
185 traits => $getopt_traits,
186 );
187  
188 10 has version => (
189 documentation => 'Print version information',
190 is => 'ro',
191 isa => 'Bool',
192 traits => $getopt_traits,
193 );
194  
195 10 has help => (
196 documentation => 'Print this help',
197 is => 'ro',
198 isa => 'Bool',
199 traits => $getopt_traits,
200 );
201  
202 has targets => (
203 is => 'ro',
204 isa => 'ArrayRef[Str]',
205 10 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
206 10 auto_deref => 1,
207 );
208  
209 10 my @Spec = __PACKAGE__->_build_getopt_spec();
210 16 ▶sub getopt_spec { @Spec }
Subroutine getopt_spec: called CC=1 SCAR=0.0
Pod undocumented
211  
212 10 ▶sub _build_getopt_spec {
Subroutine _build_getopt_spec: called CC=8 SCAR=20.8
213 10 my($self) = @_;
214  
215 10 my @spec;
216 10 foreach my $attr($self->meta->get_all_attributes) {
217 190 ▶ next unless $attr->does('Text::Xslate::Runner::Getopt');
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->does("Text::Xslate::Runner::Getopt") 10 180 190
218  
219 180 my $isa = $attr->type_constraint;
220  
221 180 my $type;
222 180 ▶ if($isa->is_a_type_of('Bool')) {
Branch: 5 branches
BranchTrueFalseTotal
if ($isa->is_a_type_of('Bool')) { } 20 160 180
elsif ($isa->is_a_type_of('Int')) { } 10 150 160
elsif ($isa->is_a_type_of('Num')) { } 0 150 150
elsif ($isa->is_a_type_of('ArrayRef')) { } 10 140 150
elsif ($isa->is_a_type_of('HashRef')) { } 30 110 140
223 20 $type = '';
224 }
225 elsif($isa->is_a_type_of('Int')) {
226 10 $type = '=i';
227 }
228 elsif($isa->is_a_type_of('Num')) {
229 0 $type = '=f';
230 }
231 elsif($isa->is_a_type_of('ArrayRef')) {
232 10 $type = '=s@';
233 }
234 elsif($isa->is_a_type_of('HashRef')) {
235 30 $type = '=s%';
236 }
237 else {
238 110 $type = '=s';
239 }
240  
241 180 my @names = ($attr->name, $attr->cmd_aliases);
242 180 push @spec, join('|', @names) . $type;
243 }
244 10 return @spec;
245 }
246  
247 16 ▶sub new_from {
Subroutine new_from: called CC=2 SCAR=7.0
Pod undocumented
248 16 my $class = shift;
249 16 local @ARGV = @_;
250 16 my %opts;
251 16 ▶ $getopt->getoptions(\%opts, $class->getopt_spec())
Branch: 1 branch
BranchTrueFalseTotal
$getopt->getoptions(\%opts, $class->getopt_spec) or die $class->help_message 16 0 16
252 or die $class->help_message;
253  
254 16 $opts{targets} = [@ARGV];
255 16 return $class->new(\%opts);
256 }
257  
258 32 ▶sub run {
Subroutine run: called CC=15 SCAR=27.1
Pod undocumented
259 32 my($self, @targets) = @_;
260  
261 32 my %args;
262 32 foreach my $field (qw(
263 cache_dir cache path syntax
264 type verbose
265 )) {
266 192 my $method = "has_$field";
267 192 ▶ $args{ $field } = $self->$field if $self->$method;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$method 56 136 192
268 }
269 32 ▶ if($self->has_module) { # re-mapping
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_module) 2 30 32
270 2 my $mod = $self->module;
271 2 my @mods;
272 2 foreach my $name(keys %{$mod}) {
273 2 push @mods, $name, [ split /,/, $mod->{$name} ];
274 }
275 2 $args{module} = \@mods;
276 }
277  
278 32 ▶ if(my $ie = $self->input_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ie = $self->input_encoding) 32 0 32
279 32 $args{input_layer} = ":encoding($ie)";
280 }
281  
282 32 ▶ local $ENV{XSLATE} = $self->debug
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_debug 0 32 32
283 if $self->has_debug;
284  
285 32 require Text::Xslate;
286  
287 32 ▶ if($self->help) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->help) { } 2 30 32
elsif ($self->version) { } 2 28 30
288 2 print $self->help_message();
289 2 return;
290 }
291 elsif($self->version) {
292 2 print $self->version_info();
293 2 return;
294 }
295  
296 28 Mouse::load_class($self->engine);
297 28 my $xslate = $self->engine->new(%args);
298  
299 28 ▶ if($self->has_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_eval) 14 14 28
300 14 my %vars;
301 14 ▶ if($self->has_define){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_define) 12 2 14
302 12 %vars = %{$self->define};
303 }
304 14 $vars{ARGV} = \@targets;
305 14 $vars{ENV} = \%ENV;
306 14 print $xslate->render_string($self->eval, \%vars), "\n";
307 14 return;
308 }
309  
310 14 foreach my $target (@targets) {
311 # XXX if you have a directory, just pushed that into the list of
312 # path in the xslate object
313 14 ▶ if ( -d $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $target) { } 2 12 14
314 2 local $self->{__process_base} = scalar(File::Spec->splitdir($target));
315 2 ▶ local $xslate->{path} = [ $target, @{ $xslate->{path} || [] } ];
Condition50%1/2
$xslate->{'path'} || []
l!l
20
MC/DC0%0/1
$xslate->{'path'} || []
$xslate->{'path'}
Truth table50%1/2
A
A= $xslate->{'path'}
Aresult
11
00
316 2 $self->process_tree( $xslate, $target );
317 } else {
318 12 my $dirname = File::Basename::dirname($target);
319 12 local $self->{__process_base} = scalar(File::Spec->splitdir($dirname));
320 12 ▶ local $xslate->{path} = [ $dirname, @{ $xslate->{path} || [] } ];
Condition50%1/2
$xslate->{'path'} || []
l!l
120
MC/DC0%0/1
$xslate->{'path'} || []
$xslate->{'path'}
Truth table50%1/2
A
A= $xslate->{'path'}
Aresult
11
00
321 12 $self->process_file( $xslate, $target );
322 }
323 }
324 }
325  
326 2 ▶sub process_tree {
Subroutine process_tree: called CC=5 SCAR=16.4
Pod undocumented
327 2 my ($self, $xslate, $dir) = @_;
328  
329 2 ▶ opendir( my $fh, $dir ) or die "Could not opendir '$dir': !";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $fh, $dir or die "Could not opendir '${dir}': !" 2 0 2
330  
331 2 ▶ while (my $e = readdir $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = readdir $fh)) 10 2 12
332 10 ▶ next if $e =~ /^\.+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /^\.+$/ 4 6 10
333 6 my $target = File::Spec->catfile( $dir, $e );
334 6 ▶ if (-d $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $target) { } 0 6 6
335 0 $self->process_tree( $xslate, $target );
336 } else {
337 6 $self->process_file( $xslate, $target );
338 }
339 }
340 }
341  
342 18 ▶sub process_file {
Subroutine process_file: called CC=12 SCAR=25.1
Pod undocumented
343 18 my ($self, $xslate, $file) = @_;
344  
345 18 ▶ if ( my $ignore = $self->ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ignore = $self->ignore) 6 12 18
346 6 ▶ if ($file =~ $ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$ignore/) 2 4 6
347 2 return;
348 }
349 }
350  
351 16 my $suffix_map = $self->suffix;
352 16 my $dest = $self->dest;
353  
354 16 my ($suffix) = ($file =~ /\.([^\.]+)$/);
355  
356 16 my $filearg = $file;
357 16 ▶ if (my $base = $self->{__process_base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $self->{"__process_base"}) 16 0 16
358 16 my @comps = File::Spec->splitdir( File::Basename::dirname($file) );
359 16 splice @comps, 0, $base;
360 16 $filearg = File::Spec->catfile( @comps, File::Basename::basename($file) );
361 }
362  
363 16 my $outfile;
364  
365 16 ▶ if(defined $dest or exists $suffix_map->{$suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest or exists $suffix_map->{$suffix}) 8 8 16
Condition66%2/3
defined $dest or exists $suffix_map->{$suffix}
l!l&&r!l&&!r
808
MC/DC50%1/2
defined $dest or exists $suffix_map->{$suffix}
defined $dest exists $suffix_map->{$suffix}
Truth table66%2/3
A or B
A= defined $destB= exists $suffix_map->{$suffix}
ABresult
1X1
011
000
366 8 $outfile= File::Spec->catfile( $dest, $filearg );
367 8 ▶ if (my $replace = $suffix_map->{ $suffix }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $replace = $suffix_map->{$suffix}) 8 0 8
368 8 $outfile =~ s/$suffix$/$replace/;
369 }
370  
371 8 my $dir = File::Basename::dirname( $outfile );
372 8 ▶ if (! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 4 4 8
373 4 require File::Path;
374 4 ▶ if (! File::Path::mkpath( $dir )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Path::mkpath($dir)) 0 4 4
375 0 die "Could not create directory $dir: $!";
376 }
377 }
378 }
379  
380 16 my $rendered = $xslate->render( $filearg, $self->define );
381 16 $rendered = $self->_encode($rendered);
382  
383 16 ▶ if(defined $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outfile) { } 8 8 16
384 8 my $fh;
385 8 ▶ open( $fh, '>', $outfile )
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $outfile or die "Could not open file $outfile for writing: $!" 8 0 8
386 or die "Could not open file $outfile for writing: $!";
387  
388 8 print $fh $rendered;
389  
390 8 ▶ close $fh or warn "Could not close file $outfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn "Could not close file ${outfile}: $!" 8 0 8
391 }
392 else {
393 8 print $rendered;
394 }
395 }
396  
397 2 ▶sub version_info {
Subroutine version_info: called CC=1 SCAR=0.0
Pod undocumented
398 2 my($self) = @_;
399 2 return sprintf qq{%s (%s) on Text::Xslate/%s, Perl/%vd.\n},
400 File::Basename::basename($0), ref($self),
401 Text::Xslate->VERSION,
402 $^V,
403 ;
404 }
405  
406 2 ▶sub help_message {
Subroutine help_message: called CC=5 SCAR=16.1
Pod undocumented
407 2 my($self) = @_;
408 2 my @options;
409 2 foreach my $attr($self->meta->get_all_attributes) {
410 38 ▶ next unless $attr->does('Text::Xslate::Runner::Getopt');
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->does("Text::Xslate::Runner::Getopt") 2 36 38
411  
412 36 ▶ my $name = join ' ', map { length($_) == 1 ? "-$_": "--$_" }
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 28 40 68
413 ($attr->cmd_aliases, $attr->name);
414  
415 36 push @options, [ $name => $attr->documentation ];
416 }
417 2 my $max_len = List::Util::max( map { length $_->[0] } @options );
418  
419 2 my $message = sprintf "usage: %s [options...] [input-files]\n",
420 File::Basename::basename($0);
421  
422 2 foreach my $opt(@options) {
423 36 $message .= sprintf " %-*s %s\n", $max_len, @{$opt};
424 }
425  
426 2 $message .= <<'EXAMPLE';
427  
428 Examples:
429 xslate -e "Hello, <: $ARGV[0] :> world!" Kolon
430 xslate -s TTerse -e "Hello, [% ARGV.0 %] world!" TTerse
431  
432 EXAMPLE
433 2 return $message;
434 }
435  
436 16 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=6.9
437 16 my($self, $str) = @_;
438 16 my $oe = $self->output_encoding;
439 16 ▶ if($oe ne 'UTF-8') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oe ne 'UTF-8') { } 6 10 16
440 6 require Encode;
441 6 return Encode::encode($oe, $str);
442 }
443 else {
444 10 utf8::encode($str);
445 10 return $str;
446 }
447 }
448  
449 10 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
450 10 ▶no Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
451 10 __PACKAGE__->meta->make_immutable;
452  
453 __END__
« blib/lib/Text/Xslate/Compiler.pm ↑ Summary blib/lib/Text/Xslate/PP.pm »