File Coverage

blib/lib/Path/Maker.pm
Criterion Covered Total %
statement 91 93 97.8
branch 20 28 71.4
condition 3 5 60.0
subroutine 23 23 100.0
pod 5 6 83.3
total 142 155 91.6


line stmt bran cond sub pod time code
1             package Path::Maker;
2 7     7   626016 use 5.008005;
  7         25  
  7         256  
3 7     7   38 use strict;
  7         14  
  7         248  
4 7     7   47 use warnings;
  7         11  
  7         278  
5 7     7   33 use Carp qw(croak);
  7         12  
  7         462  
6 7     7   36 use Cwd qw(getcwd);
  7         13  
  7         410  
7 7     7   40 use File::Path qw(mkpath);
  7         11  
  7         330  
8 7     7   40 use File::Basename qw(dirname);
  7         11  
  7         581  
9 7     7   1060 use File::Spec::Functions qw(catdir catfile file_name_is_absolute);
  7         1168  
  7         876  
10              
11             our $VERSION = "0.004";
12              
13             {
14             package
15             Text::MicroTemplate::DataSection::WithHeader;
16 7     7   43 use Carp 'croak';
  7         9  
  7         332  
17 7     7   6483 use Data::Section::Simple;
  7         4062  
  7         320  
18 7     7   40 use File::Spec::Functions qw(catfile);
  7         13  
  7         304  
19 7     7   985 use parent 'Text::MicroTemplate::File';
  7         361  
  7         51  
20              
21             sub new {
22 7     7   28 my ($class, @arg) = @_;
23 7         89 my $self = $class->SUPER::new(@arg);
24 7   50     480 $self->{package} ||= scalar caller;
25 7         87 $self->{section} = Data::Section::Simple->new( $self->{package} );
26 7         71 $self;
27             }
28             sub _find_data {
29 16     16   27 my ($self, $file) = @_;
30 16 100       78 if (my $data = $self->{section}->get_data_section($file)) {
    100          
31 13         1458 $data =~ s{\n{2}$}{\n};
32 13         36 return $data;
33             } elsif ($self->{template_dir}) {
34 1         153 return $self->_slurp( catfile($self->{template_dir}, $file) );
35             } else {
36 2         225 return;
37             }
38             }
39             # taken from Text::MicroTemplate::DataSection::build_file()
40             sub build_file {
41 16     16   99 my ($self, $file) = @_;
42 16 50       61 if (my $e = $self->{cache}{$file}) {
43 0         0 return $e;
44             }
45 16         52 my $data = $self->_find_data($file);
46 16 100       114 if (!$data) {
47 2         5 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
48 2 50       429 croak "could not find template file '$file'"
49             . " in __DATA__ section of $self->{package}"
50             . ($self->{template_dir} ? ' nor in ' . $self->{template_dir} : '');
51             }
52 14 100       50 $data = $self->{template_header} . $data if $self->{template_header};
53 14         169 $self->parse($data); # XXX assume $data is decoded
54 14         1994 local $Text::MicroTemplate::_mt_setter = 'my $_mt = shift;';
55 14         91 my $f = $self->build;
56 14 50       6112 $self->{cache}{$file} = $f if $self->{use_cache};
57 14         393 return $f;
58             }
59             sub _slurp {
60 1     1   3 my (undef, $file) = @_;
61 1 50       88 open my $fh, "<:utf8", $file or return;
62 1         6 local $/;
63 1         55 scalar <$fh>;
64             }
65             }
66              
67             sub new {
68 7     7 0 6670 my $class = shift;
69 7 50       53 my %opt = ref $_[0] ? %{ $_[0] } : @_;
  0         0  
70 7         26 my $base_dir = delete $opt{base_dir};
71 7         124 my $data_section = Text::MicroTemplate::DataSection::WithHeader->new(
72             package => scalar(caller),
73             %opt,
74             );
75 7         94 bless {
76             base_dir => $base_dir,
77             data_section => $data_section
78             }, $class;
79             }
80              
81             sub _abs_path {
82 24     24   30 my ($self, $path) = @_;
83 24 100       66 return $path if file_name_is_absolute $path;
84 12   66     190 return catdir( $self->{base_dir} || getcwd, $path );
85             }
86              
87             sub render {
88 16     16 1 6237 my ($self, $template_name, @arg) = @_;
89 16         130 $self->{data_section}->render_file($template_name, @arg);
90             }
91             sub chmod : method {
92 2     2 1 2181 my ($self, $path, $mod) = @_;
93 2         7 $path = $self->_abs_path($path);
94 2         9 my $oct_mod = sprintf '%lo', $mod;
95 2 50       75 chmod $mod, $path or croak "chmod $oct_mod $path: $!";
96             }
97             sub create_dir {
98 8     8 1 532 my ($self, $dir) = @_;
99 8         29 $dir = $self->_abs_path($dir);
100 8 100       290 return if -d $dir;
101 2 50       336 mkpath $dir or croak "mkpath $dir: $!";
102             }
103             sub render_to_file {
104 6     6 1 1985 my ($self, $template_name, $file, @arg) = @_;
105 6         20 $file = $self->_abs_path($file);
106 6         278 my $dir = dirname $file;
107 6         22 $self->create_dir($dir);
108 6         20 $self->write_file( $file, $self->render($template_name, @arg) );
109             }
110             sub write_file {
111 8     8 1 211 my ($self, $file, $string) = @_;
112 8         17 $file = $self->_abs_path($file);
113 8 50       802 open my $fh, ">:utf8", $file or croak "open $file: $!";
114 8         11 print {$fh} $string;
  8         114  
115             }
116              
117             1;
118             __END__