File Coverage

blib/lib/Gears/Generator.pm
Criterion Covered Total %
statement 55 55 100.0
branch 6 8 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 70 72 97.2


line stmt bran cond sub pod time code
1             package Gears::Generator;
2             $Gears::Generator::VERSION = '0.101';
3 1     1   161447 use v5.40;
  1         3  
4 1     1   391 use Mooish::Base -standard;
  1         85923  
  1         17  
5              
6 1     1   286544 use Gears::X::Generator;
  1         113  
  1         52  
7 1     1   1049 use Path::Tiny qw(path);
  1         18500  
  1         865  
8              
9             has param 'base_dir' => (
10             coerce => (InstanceOf ['Path::Tiny'])
11             ->plus_coercions(
12             Str, q{ Path::Tiny::path($_) },
13             ),
14             );
15              
16             has param 'name_filters' => (
17             isa => ArrayRef [CodeRef],
18             default => sub { [] },
19             );
20              
21             has param 'content_filters' => (
22             isa => ArrayRef [CodeRef],
23             default => sub { [] },
24             );
25              
26 3         6 sub get_template ($self, $name)
27 3     3 1 308629 {
  3         7  
  3         6  
28 3         24 my $dir = $self->base_dir->child($name);
29 3 50       238 Gears::X::Generator->raise("invalid directory $name")
30             unless $dir->is_dir;
31              
32 3         106 my @all_items;
33 3         33 my $iter = $dir->iterator({recurse => true});
34 3         125 while (my $item = $iter->()) {
35 12 100       2002 next if $item->is_dir;
36 9         162 push @all_items, $item;
37             }
38              
39 3         157 return \@all_items;
40             }
41              
42 2         6 sub generate ($self, $name, $target_dir)
  2         5  
43 2     2 1 30410 {
  2         4  
  2         6  
44 2         11 my $template_items = $self->get_template($name);
45 2         15 my $base = $self->base_dir->child($name);
46 2         132 my $target_path = path($target_dir);
47              
48 2 50       94 Gears::X::Generator->raise("target directory does not exist: $target_dir")
49             unless $target_path->is_dir;
50              
51 2         48 my @generated;
52 2         7 for my $item ($template_items->@*) {
53 4         118 my $target_file = $target_path->child($item->relative($base));
54              
55 4         2029 my $content = $item->slurp({binmode => ':encoding(UTF-8)'});
56 4         3377 ($target_file, $content) = $self->_process_file($target_file, $content);
57              
58 4         21 $target_file = path($target_file);
59 4         134 $target_file->parent->mkdir;
60 4         1014 push @generated, [$target_file, $content];
61              
62 4 100       20 Gears::X::Generator->raise("file already exists: $target_file, aborting")
63             if $target_file->exists;
64             }
65              
66 1         65 foreach my $item (@generated) {
67 3         6782 $item->[0]->spew({binmode => ':encoding(UTF-8)'}, $item->[1]);
68             }
69              
70 1         887 return [map { $_->[0] } @generated];
  3         36  
71             }
72              
73 4         8 sub _process_file ($self, $name, $content)
  4         9  
74 4     4   11 {
  4         8  
  4         8  
75 4         19 foreach my $filter ($self->name_filters->@*) {
76 3         12 $name = $filter->($name);
77             }
78              
79 4         103 foreach my $filter ($self->content_filters->@*) {
80 3         11 $content = $filter->($content);
81             }
82              
83 4         48 return ($name, $content);
84             }
85              
86             __END__