blib/lib/Dist/Data.pm

filter: Statementstmt 91.6% 132 / 144 Branchbran 73.9% 34 / 46 Conditioncond 77.7% 14 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 88.8% 24 / 27 Podpod 0.0% 0 / 9 totaltotal 82.8% 212 / 256 CC 37 SCAR 14.9
CC
37
Cov
87%
_build_dirs
12.0
_build_namespaces
8.1
BUILDARGS
7.1
CRAP 4.4
↑ Summary
1 package Dist::Data;
2 3 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: API to access the data of a Perl distribution file or directory
4 3 $Dist::Data::VERSION = '0.007';
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Archive::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Find::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DateTime::Format::Epoch::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Dist::Metadata ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 has filename => (
15 is => 'ro',
16 predicate => 'has_filename',
17 );
18  
19 3 has archive => (
20 is => 'ro',
21 lazy => 1,
22 builder => '_build_archive',
23 );
24  
25 2 ▶sub _build_archive {
Subroutine _build_archive: called CC=2 SCAR=7.1
26 2 my ( $self ) = @_;
27 2 ▶ die __PACKAGE__.": need a filename" unless $self->has_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_filename 0 2 2
28 2 return Archive::Any->new($self->filename);
29 }
30  
31 3 has cpan_meta => (
32 is => 'ro',
33 lazy => 1,
34 builder => '_build_cpan_meta',
35 handles => [qw(
36 abstract
37 description
38 dynamic_config
39 generated_by
40 name
41 release_status
42 version
43 authors
44 keywords
45 licenses
46 meta_spec
47 resources
48 provides
49 no_index
50 prereqs
51 optional_features
52 )]
53 );
54 3 ▶sub cm { shift->cpan_meta(@_) }
Subroutine cm: called CC=1 SCAR=0.0
Pod undocumented
55  
56 # LEGACY
57 0 ▶sub distmeta { shift->cpan_meta(@_) }
Subroutine distmeta: not called CC=1 SCAR=6.9
Pod undocumented
58  
59 2 ▶sub _build_cpan_meta {
Subroutine _build_cpan_meta: called CC=3 SCAR=14.2
60 2 my ( $self ) = @_;
61 2 ▶ if ($self->files->{'META.json'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->files->{'META.json'}) { } 2 0 2
elsif ($self->files->{'META.yml'}) { } 0 0 0
62 2 CPAN::Meta->load_file($self->files->{'META.json'});
63 } elsif ($self->files->{'META.yml'}) {
64 0 CPAN::Meta->load_file($self->files->{'META.yml'});
65 }
66 }
67  
68 3 has dist_metadata => (
69 is => 'ro',
70 lazy => 1,
71 builder => '_build_dist_metadata',
72 );
73  
74 2 ▶sub _build_dist_metadata {
Subroutine _build_dist_metadata: called CC=1 SCAR=0.0
75 2 my ( $self ) = @_;
76 2 $self->extract_distribution;
77 2 Dist::Metadata->new(dir => $self->dist_dir);
78 }
79  
80 3 has dir => (
81 is => 'ro',
82 predicate => 'has_dir',
83 );
84  
85 3 ▶sub dir_has_dist {
Subroutine dir_has_dist: called CC=2 SCAR=7.0
Pod undocumented
86 3 my ( $self ) = @_;
87 3 my $dir = $self->dist_dir;
88 3 ▶ return unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 3 3
89 3 return -f "$dir/Makefile.PL";
90 }
91  
92 3 has files => (
93 is => 'ro',
94 lazy => 1,
95 builder => '_build_files',
96 );
97  
98 2 ▶sub _build_files {
Subroutine _build_files: called CC=3 SCAR=11.0
99 2 my ( $self ) = @_;
100 2 $self->extract_distribution;
101 2 my %files;
102 2 for ($self->get_directory_tree($self->dist_dir)) {
103 36 ▶ $files{join('/',@{$_->full_components})} = $_->path if $_->is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $_->is_file 24 12 36
104 }
105 2 return \%files;
106 }
107  
108 3 has dirs => (
109 is => 'ro',
110 lazy => 1,
111 builder => '_build_dirs',
112 );
113  
114 0 ▶sub _build_dirs {
Subroutine _build_dirs: not called CC=3 SCAR=24.8
115 0 my ( $self ) = @_;
116 0 $self->extract_distribution;
117 0 my %dirs;
118 0 for ($self->get_directory_tree($self->dist_dir)) {
119 0 ▶ $dirs{join('/',@{$_->full_components})} = $_->path if $_->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $_->is_dir 0 0 0
120 }
121 0 return \%dirs;
122 }
123  
124 3 has dist_dir => (
125 is => 'ro',
126 lazy => 1,
127 builder => '_build_dist_dir',
128 );
129  
130 3 ▶sub _build_dist_dir {
Subroutine _build_dist_dir: called CC=2 SCAR=6.9
131 3 my ( $self ) = @_;
132 3 ▶ return $self->has_dir ? $self->dir : File::Temp->newdir;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_dir ? : 2 1 3
133 }
134  
135 5 ▶sub extract_distribution {
Subroutine extract_distribution: called CC=5 SCAR=16.1
Pod undocumented
136 5 my ( $self ) = @_;
137 5 ▶ return unless $self->has_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_filename 2 3 5
138 3 ▶ return if $self->dir_has_dist;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dir_has_dist 1 2 3
139 2 my $ext_dir = File::Temp->newdir;
140 2 $self->archive->extract($ext_dir);
141 2 for ($self->get_directory_tree($ext_dir)) {
142 38 my @components = @{$_->full_components};
143 38 shift @components;
144 38 ▶ if ($_->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is_dir) { } 14 24 38
145 14 mkdir $self->dist_dir.'/'.join('/',@components);
146 } else {
147 24 rename $_->path, $self->dist_dir.'/'.join('/',@components);
148 }
149 }
150 2 return 1;
151 }
152  
153 3 has packages => (
154 is => 'ro',
155 lazy => 1,
156 builder => '_build_packages',
157 );
158  
159 2 ▶sub _build_packages {
Subroutine _build_packages: called CC=1 SCAR=0.0
160 2 my ( $self ) = @_;
161 2 return $self->dist_metadata->determine_packages($self->cm);
162 # OLD - probably reused later if we introduce behaviour switches
163 # my %packages;
164 # for (keys %{$self->files}) {
165 # my $key = $_;
166 # my @components = split('/',$key);
167 # if ($key =~ /\.pm$/) {
168 # my @namespaces = Module::Extract::Namespaces->from_file($self->files->{$key});
169 # for (@namespaces) {
170 # $packages{$_} = [] unless defined $packages{$_};
171 # push @{$packages{$_}}, $key;
172 # }
173 # } elsif ($key =~ /^lib\// && $key =~ /\.pod$/) {
174 # my $packagename = $key;
175 # $packagename =~ s/^lib\///g;
176 # $packagename =~ s/\.pod$//g;
177 # $packagename =~ s/\//::/g;
178 # $packages{$packagename} = [] unless defined $packages{$packagename};
179 # push @{$packages{$packagename}}, $key;
180 # }
181 # }
182 # return \%packages;
183 }
184  
185 3 has namespaces => (
186 is => 'ro',
187 lazy => 1,
188 builder => '_build_namespaces',
189 );
190  
191 1 ▶sub _build_namespaces {
Subroutine _build_namespaces: called CC=8 SCAR=20.9
192 1 my ( $self ) = @_;
193 1 my %namespaces;
194 1 for (keys %{$self->files}) {
195 12 my $key = $_;
196 12 ▶ if ($key =~ /\.pm$/ || $key =~ /\.pl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\.pm$/ or $key =~ /\.pl$/) 1 11 12
Condition66%2/3
$key =~ /\.pm$/ or $key =~ /\.pl$/
l!l&&r!l&&!r
1011
MC/DC50%1/2
$key =~ /\.pm$/ or $key =~ /\.pl$/
$key =~ /\.pm$/ $key =~ /\.pl$/
Truth table66%2/3
A or B
A= $key =~ /\.pm$/B= $key =~ /\.pl$/
ABresult
1X1
011
000
197 1 my $metadata = Module::Metadata->new_from_file($self->files->{$key});
198 1 my @namespaces = $metadata->packages_inside;
199 1 for (@namespaces) {
200 2 ▶ next unless defined $_ && $_ ne 'main';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and $_ ne "main" 1 1 2
Condition66%2/3
defined $_ and $_ ne "main"
!ll&&!rl&&r
011
MC/DC50%1/2
defined $_ and $_ ne "main"
defined $_ $_ ne "main"
Truth table66%2/3
A and B
A= defined $_B= $_ ne "main"
ABresult
0X0
100
111
201 1 ▶ $namespaces{$_} = [] unless defined $namespaces{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespaces{$_} 1 0 1
202 1 push @{$namespaces{$_}}, $key;
203 }
204 }
205 }
206 1 return \%namespaces;
207 }
208  
209 3 has documentations => (
210 is => 'ro',
211 lazy => 1,
212 builder => '_build_documentations',
213 );
214  
215 1 ▶sub _build_documentations {
Subroutine _build_documentations: called CC=4 SCAR=13.9
216 1 my ( $self ) = @_;
217 1 my %docs;
218 1 for (keys %{$self->files}) {
219 12 my $key = $_;
220 12 ▶ if ($key =~ /^lib\// && $key =~ /\.pod$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ m[^lib/] and $key =~ /\.pod$/) 1 11 12
Condition100%3/3
$key =~ m[^lib/] and $key =~ /\.pod$/
!ll&&!rl&&r
1011
MC/DC100%2/2
$key =~ m[^lib/] and $key =~ /\.pod$/
$key =~ m[^lib/] $key =~ /\.pod$/
Truth table100%3/3
A and B
A= $key =~ m[^lib/]B= $key =~ /\.pod$/
ABresult
0X0
100
111
221 1 my $packagename = $key;
222 1 $packagename =~ s/^lib\///g;
223 1 $packagename =~ s/\.pod$//g;
224 1 $packagename =~ s/\//::/g;
225 1 $docs{$packagename} = $key;
226 }
227 }
228 1 return \%docs;
229 }
230  
231 3 has scripts => (
232 is => 'ro',
233 lazy => 1,
234 builder => '_build_scripts',
235 );
236  
237 1 ▶sub _build_scripts {
Subroutine _build_scripts: called CC=4 SCAR=13.9
238 1 my ( $self ) = @_;
239 1 my %scripts;
240 1 for (keys %{$self->files}) {
241 12 ▶ next unless $_ =~ /^bin\// || $_ =~ /^script\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ m[^bin/] or $_ =~ m[^script/] 11 1 12
Condition66%2/3
$_ =~ m[^bin/] or $_ =~ m[^script/]
l!l&&r!l&&!r
1011
MC/DC50%1/2
$_ =~ m[^bin/] or $_ =~ m[^script/]
$_ =~ m[^bin/] $_ =~ m[^script/]
Truth table66%2/3
A or B
A= $_ =~ m[^bin/]B= $_ =~ m[^script/]
ABresult
1X1
011
000
242 1 my $key = $_;
243 1 my @components = split('/',$key);
244 1 shift @components;
245 1 $scripts{join('/',@components)} = $key;
246 }
247 1 return \%scripts;
248 }
249  
250 4 ▶sub get_directory_tree {
Subroutine get_directory_tree: called CC=2 SCAR=6.9
Pod undocumented
251 4 my ( $self, @dirs ) = @_;
252 4 my $tree = File::Find::Object->new({}, @dirs);
253 4 my @files;
254 4 ▶ while (my $r = $tree->next_obj()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $tree->next_obj) 74 4 78
255 74 push @files, $r;
256 }
257 4 return @files;
258 }
259  
260 4 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
261 4 my ( $self, $file ) = @_;
262 4 return $self->files->{$file};
263 }
264  
265 0 ▶sub modified {
Subroutine modified: not called CC=2 SCAR=17.9
Pod undocumented
266 0 my ( $self ) = @_;
267 0 ▶ my $mtime = stat($self->has_filename ? $self->filename : $self->dir )->mtime;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_filename ? : 0 0 0
268 0 return DateTime::Format::Epoch::Unix->parse_datetime($mtime);
269 }
270  
271 3 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
272 3 my ( $self ) = @_;
273 3 ▶ $self->extract_distribution if $self->has_dir && $self->has_filename;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dir and $self->has_filename 1 2 3
Condition100%3/3
$self->has_dir and $self->has_filename
!ll&&!rl&&r
111
MC/DC100%2/2
$self->has_dir and $self->has_filename
$self->has_dir $self->has_filename
Truth table100%3/3
A and B
A= $self->has_dirB= $self->has_filename
ABresult
0X0
100
111
274 }
275  
276 3 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=7 SCAR=19.6
Pod undocumented
277 3 my ( $class, @args ) = @_;
278 3 ▶ die __PACKAGE__.": please give filename on new" if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 3 3
279 3 ▶ my $arg; $arg = shift @args if @args % 2 == 1 && ref $args[0] ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 and ref $args[0] ne "HASH" 2 1 3
Condition66%2/3
@args % 2 == 1 and ref $args[0] ne "HASH"
!ll&&!rl&&r
012
MC/DC50%1/2
@args % 2 == 1 and ref $args[0] ne "HASH"
@args % 2 == 1 ref $args[0] ne "HASH"
Truth table66%2/3
A and B
A= @args % 2 == 1B= ref $args[0] ne "HASH"
ABresult
0X0
100
111
280 3 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 2 1 3
281 # should support URL also
282 2 ▶ if (-f $arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $arg) { } 1 1 2
elsif (-d $arg) { } 1 0 1
283 1 return { filename => $arg, @args };
284 } elsif (-d $arg) {
285 1 return { dir => $arg, @args };
286 }
287 }
288 1 return $class->SUPER::BUILDARGS(@args);
289 }
290  
291 3 1;
292  
293 __END__
↑ Summary