| 1 | package Gears::Generator; | ||||||||||
| 2 | 1 | $Gears::Generator::VERSION = '0.105'; | |||||||||
| 3 | 1 | ▶ | use v5.40; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Mooish::Base -standard; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Gears::X::Generator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Path::Tiny qw(path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | 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 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ); | |||||||||
| 20 | |||||||||||
| 21 | has param 'content_filters' => ( | ||||||||||
| 22 | isa => ArrayRef [CodeRef], | ||||||||||
| 23 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ); | |||||||||
| 25 | |||||||||||
| 26 | 3 | ▶ | sub get_template ($self, $name) | ||||||||
|
Subroutine
get_template: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 27 | { | ||||||||||
| 28 | 3 | my $dir = $self->base_dir->child($name); | |||||||||
| 29 | 3 | ▶ | Gears::X::Generator->raise("invalid directory $name") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | unless $dir->is_dir; | ||||||||||
| 31 | |||||||||||
| 32 | 3 | my @all_items; | |||||||||
| 33 | 3 | my $iter = $dir->iterator({recurse => true}); | |||||||||
| 34 | 3 | ▶ | while (my $item = $iter->()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 12 | ▶ | next if $item->is_dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 9 | push @all_items, $item; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 3 | return \@all_items; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 2 | ▶ | sub generate ($self, $name, $target_dir) | ||||||||
|
Subroutine
generate: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 43 | { | ||||||||||
| 44 | 2 | my $template_items = $self->get_template($name); | |||||||||
| 45 | 2 | my $base = $self->base_dir->child($name); | |||||||||
| 46 | 2 | my $target_path = path($target_dir); | |||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | Gears::X::Generator->raise("target directory does not exist: $target_dir") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | unless $target_path->is_dir; | ||||||||||
| 50 | |||||||||||
| 51 | 2 | my @generated; | |||||||||
| 52 | 2 | for my $item ($template_items->@*) { | |||||||||
| 53 | 4 | my $target_file = $target_path->child($item->relative($base)); | |||||||||
| 54 | |||||||||||
| 55 | 4 | my $content = $item->slurp({binmode => ':encoding(UTF-8)'}); | |||||||||
| 56 | 4 | ($target_file, $content) = $self->_process_file($target_file, $content); | |||||||||
| 57 | |||||||||||
| 58 | 4 | $target_file = path($target_file); | |||||||||
| 59 | 4 | push @generated, [$target_file, $content]; | |||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | Gears::X::Generator->raise("file already exists: $target_file, aborting") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | if $target_file->exists; | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 1 | foreach my $item (@generated) { | |||||||||
| 66 | 3 | $item->[0]->parent->mkdir; | |||||||||
| 67 | 3 | $item->[0]->spew({binmode => ':encoding(UTF-8)'}, $item->[1]); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | return [map { $_->[0] } @generated]; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 4 | ▶ | sub _process_file ($self, $name, $content) | ||||||||
|
Subroutine
_process_file: called CC=3 SCAR=11.0
| |||||||||||
| 74 | { | ||||||||||
| 75 | 4 | foreach my $filter ($self->name_filters->@*) { | |||||||||
| 76 | 3 | $name = $filter->($name); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 4 | foreach my $filter ($self->content_filters->@*) { | |||||||||
| 80 | 3 | $content = $filter->($content); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 4 | return ($name, $content); | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | __END__ | ||||||||||