blib/lib/App/DuckPAN/Template.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 98.1% 53 / 54 CC 4 SCAR 5.1
CC
4
Cov
100%
generate
3.0
_build_output_directory
2.0
BEGIN
1.0
CRAP 1.7
« bin/duckpan ↑ Summary blib/lib/App/DuckPAN/Cmd/Env/Cmd.pm »
1 package App::DuckPAN::Template;
2 2 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Template to generate one file of an Instant Answer
4 2 $App::DuckPAN::Template::VERSION = '1021';
5 # An Instant Answer has multiple templates, each of which can be used
6 # to generate one output file.
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Text::Xslate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 has name => (
17 is => 'ro',
18 required => 1,
19 doc => 'Name of the template',
20 );
21  
22 2 has label => (
23 is => 'ro',
24 required => 1,
25 doc => 'Label of the template',
26 );
27  
28 2 has input_file => (
29 is => 'ro',
30 required => 1,
31 doc => 'Path of the input file for the template',
32 );
33  
34 2 has output_file => (
35 is => 'ro',
36 required => 1,
37 doc => 'Path of the output file for the template. ' .
38 'This string is rendered through Text::Xslate to get the final path.',
39 );
40  
41 2 has output_directory => (
42 is => 'ro',
43 init_arg => undef,
44 lazy => 1,
45 builder => 1,
46 doc => 'Directory known to contain all of the generated template output files and subdirectories',
47 );
48  
49 2 ▶sub _build_output_directory {
Subroutine _build_output_directory: called CC=2 SCAR=6.9
50 2 my ($self) = @_;
51 2 my $out_dir = path($self->output_file);
52  
53 # Get the directory that is certain to be the closest ancestor of the
54 # output file i.e., keep removing directory parts from the right till the
55 # path does not contain any Text::Xslate syntax.
56 2 ▶ $out_dir = $out_dir->parent while $out_dir =~ /<:/;
Branch: 1 branch
BranchTrueFalseTotal
$out_dir =~ /<:/ and do { $out_dir = $out_dir->parent } 4 2 6
57  
58 2 return $out_dir;
59 }
60  
61 # Create the output file from the input file
62 11 ▶sub generate {
Subroutine generate: called CC=3 SCAR=11.0
Pod undocumented
63 11 my ($self, $vars) = @_;
64  
65 # Increased verbosity to help while writing templates
66 11 my $tx = Text::Xslate->new(type => 'text', verbose => 2);
67 11 my $input_file = path($self->input_file);
68  
69 # (should not occur for users)
70 11 ▶ die "Template input file '$input_file' not found" unless $input_file->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_file->exists 2 9 11
71  
72 # The output file path is a Text::Xslate template, so we generate the
73 # actual path here
74 9 my $output_file = path($tx->render_string($self->output_file, $vars));
75  
76 9 ▶ die "Template output file '" . $output_file . "' already exists" if $output_file->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $output_file->exists 1 8 9
77  
78 8 my $content = $tx->render($input_file, $vars);
79  
80 8 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 8 path($output_file)->touchpath->spew_utf8($content);
82 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 die "Error creating output file '$output_file' from template: $_";
84 8 };
85  
86 7 return $output_file;
87 }
88  
89 2 1;
90  
91 __END__
« bin/duckpan ↑ Summary blib/lib/App/DuckPAN/Cmd/Env/Cmd.pm »