| 1 | package Catmandu::Fix::include; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.2025'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Catmandu; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Catmandu::Fix; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Cwd qw(realpath); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | with 'Catmandu::Fix::Inlineable'; | |||||||||
| 16 | |||||||||||
| 17 | 1 | has path => (fix_arg => 1); | |||||||||
| 18 | 1 | has _files => (is => 'lazy'); | |||||||||
| 19 | 1 | has _fixer => (is => 'lazy'); | |||||||||
| 20 | |||||||||||
| 21 | 4 | ▶ | sub _build__files { | ||||||||
|
Subroutine
_build__files: called CC=2 SCAR=6.9
| |||||||||||
| 22 | 4 | my ($self) = @_; | |||||||||
| 23 | 4 | my $path = $self->path; | |||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | if ($path =~ /\*/) { # path is glob pattern | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | return $self->_find_glob($path); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 3 | [$self->_find_file($path)]; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 3 | ▶ | sub _find_file { | ||||||||
|
Subroutine
_find_file: called CC=5 SCAR=16.6
| |||||||||||
| 33 | 3 | my ($self, $path) = @_; | |||||||||
| 34 | 3 | my $roots = Catmandu->roots; | |||||||||
| 35 | 3 | my $file; | |||||||||
| 36 | |||||||||||
| 37 | 3 | ▶ | if (File::Spec->file_name_is_absolute($path)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | $file = $path; | |||||||||
| 39 | } | ||||||||||
| 40 | else { | ||||||||||
| 41 | 3 | for my $root (@$roots) { | |||||||||
| 42 | 3 | my $f = File::Spec->catfile($root, $path); | |||||||||
| 43 | 3 | ▶ | if (-r $f) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 3 | $file = $f; | |||||||||
| 45 | 3 | last; | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 3 | ▶ | Catmandu::Error->throw( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | "unable to find $path in " . join(',', @$roots) . ")") | ||||||||||
| 53 | unless defined $file; | ||||||||||
| 54 | |||||||||||
| 55 | 3 | realpath($file); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | ▶ | sub _find_glob { | ||||||||
|
Subroutine
_find_glob: called CC=6 SCAR=18.9
| |||||||||||
| 59 | 1 | my ($self, $path) = @_; | |||||||||
| 60 | 1 | my $roots = Catmandu->roots; | |||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | if (File::Spec->file_name_is_absolute($path)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | return [sort map {realpath($_)} grep {-r $_} glob $path]; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 1 | my %seen; | |||||||||
| 67 | 1 | my $files = []; | |||||||||
| 68 | |||||||||||
| 69 | 1 | for my $root (@$roots) { | |||||||||
| 70 | 1 | my $glob = File::Spec->catfile($root, $path); | |||||||||
| 71 | 1 | for my $file (glob $glob) { | |||||||||
| 72 | 3 | my $rel_path = File::Spec->abs2rel($file, $root); | |||||||||
| 73 | 3 | ▶ | next if $seen{$rel_path}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 3 | ▶ | if (-r $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 3 | push @$files, realpath($file); | |||||||||
| 76 | 3 | $seen{$rel_path} = 1; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 1 | [sort @$files]; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 4 | ▶ | sub _build__fixer { | ||||||||
|
Subroutine
_build__fixer: called CC=2 SCAR=7.0
| |||||||||||
| 85 | 4 | my ($self) = @_; | |||||||||
| 86 | 4 | my $files = $self->_files; | |||||||||
| 87 | 4 | ▶ | return unless @$files; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 4 | Catmandu::Fix->new(fixes => $files); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 4 | ▶ | sub fix { | ||||||||
|
Subroutine
fix: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 92 | 4 | my ($self, $data) = @_; | |||||||||
| 93 | 4 | my $fixer = $self->_fixer; | |||||||||
| 94 | 4 | ▶ | return $data unless $fixer; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 4 | $fixer->fix($data); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 1 | 1; | |||||||||
| 99 | |||||||||||
| 100 | __END__ | ||||||||||