| 1 | package Catmandu::Importer::Modules; | ||
| 2 | |||
| 3 | 2 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 2 | our $VERSION = '1.2025'; | |
| 6 | |||
| 7 | 2 | ▶ | use Module::Info; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Path::Iterator::Rule; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Catmandu::Util qw(array_split pod_section read_file); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 2 | with 'Catmandu::Importer'; | |
| 15 | |||
| 16 | has inc => ( | ||
| 17 | is => 'ro', | ||
| 18 | lazy => 1, | ||
| 19 | 1 | ▶ | default => sub {[@INC]}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 20 | 2 | coerce => \&array_split, | |
| 21 | ); | ||
| 22 | |||
| 23 | has namespace => | ||
| 24 | 1 | ▶ | (is => 'ro', default => sub {[""]}, coerce => \&array_split,); |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 25 | |||
| 26 | 2 | has max_depth => (is => 'ro', predicate => 1,); | |
| 27 | |||
| 28 | 2 | has pattern => (is => 'ro',); | |
| 29 | |||
| 30 | 2 | has primary => (is => 'ro',); | |
| 31 | |||
| 32 | 2 | ▶ | has about => (is => 'ro', default => sub {1}); |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | 10 | ▶ | sub generator { |
|
Subroutine
generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 35 | 10 | my ($self) = @_; | |
| 36 | |||
| 37 | sub { | ||
| 38 | state $pattern = $self->pattern; | ||
| 39 | state $files = {}; | ||
| 40 | state $names = {}; | ||
| 41 | |||
| 42 | # array of [ $directory => $namespace ] | ||
| 43 | state $search = [ | ||
| 44 | map { | ||
| 45 | my $ns = $_; | ||
| 46 | my $parts = [map {grep length, split(/::/, $_)} $ns]; | ||
| 47 | map {[File::Spec->catdir($_, @$parts) => $ns]} @{$self->inc}; | ||
| 48 | } @{$self->namespace} | ||
| 49 | ]; | ||
| 50 | |||
| 51 | state $cur = shift(@$search) // return; | ||
| 52 | |||
| 53 | state $iter = do { | ||
| 54 | my $rule = Path::Iterator::Rule->new; | ||
| 55 | $rule->file->name('*.pm'); | ||
| 56 | $rule->max_depth($self->max_depth) if $self->has_max_depth; | ||
| 57 | $rule->iter($cur->[0], {depthfirst => 1}); | ||
| 58 | }; | ||
| 59 | |||
| 60 | while (1) { | ||
| 61 | my ($dir, $ns) = @$cur; | ||
| 62 | |||
| 63 | if (defined(my $file = $iter->())) { | ||
| 64 | my $path = File::Spec->abs2rel($file, $dir); | ||
| 65 | my $name = join('::', File::Spec->splitdir($path)); | ||
| 66 | $name =~ s/\.pm$//; | ||
| 67 | $name = join('::', $ns, $name) if $ns; | ||
| 68 | |||
| 69 | next if defined $pattern && $name !~ $pattern; | ||
| 70 | |||
| 71 | my $info = Module::Info->new_from_file($file); | ||
| 72 | my $file = File::Spec->rel2abs($file); | ||
| 73 | |||
| 74 | next if $files->{$file}; | ||
| 75 | $files->{$file} = 1; | ||
| 76 | |||
| 77 | if ($self->primary) { | ||
| 78 | next if $names->{$name}; | ||
| 79 | $names->{$name} = 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $data = {file => $file, name => $name, path => $dir,}; | ||
| 83 | |||
| 84 | if (defined $info->version && $info->version ne 'undef') { | ||
| 85 | $data->{version} = "" . $info->version; | ||
| 86 | } | ||
| 87 | elsif (open(my $fh, '<:encoding(UTF-8)', $file)) { | ||
| 88 | while (my $line = <$fh>) { | ||
| 89 | if (my ($version) | ||
| 90 | = $line | ||
| 91 | =~ /^\s*our\s+\$VERSION\s*=\s*['"]([^'"]+)['"]\s*;/ | ||
| 92 | ) | ||
| 93 | { | ||
| 94 | $data->{version} = $version; | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | close($fh); | ||
| 99 | } | ||
| 100 | |||
| 101 | if ($self->about) { | ||
| 102 | my $about = pod_section($file, 'NAME'); | ||
| 103 | $about =~ s/[^-]+(\s*-?\s*)?//; | ||
| 104 | $about =~ s/\n/ /mg; | ||
| 105 | $about =~ s/ *$//; | ||
| 106 | $data->{about} = $about if $about ne ''; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $data; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | $cur = shift(@$search) // return; | ||
| 113 | my $rule = Path::Iterator::Rule->new; | ||
| 114 | $rule->file->name('*.pm'); | ||
| 115 | $rule->max_depth($self->max_depth) if $self->has_max_depth; | ||
| 116 | $iter = $rule->iter($cur->[0], {depthfirst => 1}); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | 10 | }; | |
| 120 | } | ||
| 121 | |||
| 122 | 2 | 1; | |
| 123 | |||
| 124 | __END__ | ||