Module/Faker/Dist.pm

filter: Statementstmt 91.2% 259 / 284 Branchbran 51.5% 34 / 66 Conditioncond 65.3% 17 / 26 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 94.5% 52 / 55 Podpod 55.5% 5 / 9 totaltotal 81.3% 371 / 456 CC 55 SCAR 21.5
CC
55
Cov
82%
from_struct
72.0
_meta_file_content
9.3
_build__cpan_meta
7.1
CRAP 8.6
↑ Summary Module/Faker/Package.pm »
1 package Module::Faker::Dist 0.027;
2 # ABSTRACT: a fake CPAN distribution
3  
4 8 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Module::Faker::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Module::Faker::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Module::Faker::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Module::Faker::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Archive::Any::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use CPAN::DistnameInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use CPAN::Meta 2.130880; # github issue #9
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use CPAN::Meta::Converter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use CPAN::Meta::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use CPAN::Meta::Requirements;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Data::OptList ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Parse::CPAN::Meta 1.4401;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #pod =head1 SYNOPSIS
27 #pod
28 #pod Building one dist at a time makes plenty of sense, so Module::Faker::Dist makes
29 #pod it easy. Building dists from definitions in files is also useful for doing
30 #pod things in bulk (see L<CPAN::Faker>), so there are a bunch of ways to build
31 #pod dists from a definition in a file.
32 #pod
33 #pod # Build from a META.yml or META.json file, or the delightful
34 #pod # AUTHOR_Foo-Bar-1.234.tar.gz.dist file, which can be zero bytes and gets
35 #pod # all the relevant data from the filename.
36 #pod my $dist = Module::Faker::Dist->from_file($filename);
37 #pod
38 #pod META files can contain a key called X_Module_Faker that contains attributes to
39 #pod use in constructing the dist. C<dist> files can contain anything you want, but
40 #pod the contents won't do a thing.
41 #pod
42 #pod You can use the C<new> method on Module::Faker::Dist, of course, but it's a bit
43 #pod of a pain. You might, instead, want to use C<from_struct>, which is very close
44 #pod to C<new>, but with more sugar.
45 #pod
46 #pod =cut
47  
48 #pod =attr name
49 #pod
50 #pod This is the name of the dist. It will usually look like C<Foo-Bar>.
51 #pod
52 #pod =attr version
53 #pod
54 #pod This is the version of the dist, usually some kind of versiony string like
55 #pod C<1.234> or maybe C<1.2.3>.
56 #pod
57 #pod =attr abstract
58 #pod
59 #pod The abstract! This is a short, pithy description of the distribution, usually
60 #pod less than a sentence.
61 #pod
62 #pod =attr release_status
63 #pod
64 #pod This is the dist's release status. (See L<CPAN::Meta::Spec>.) It defaults to
65 #pod C<stable> but C<unstable> and C<testing> are valid values.
66 #pod
67 #pod =cut
68  
69 8 my $DEFAULT_VERSION;
70  
71 # required by CPAN::Meta::Spec
72 8 has name => (is => 'ro', isa => 'Str', required => $DEFAULT_VERSION);
73 8 has version => (is => 'ro', isa => 'Maybe[Str]', default => '0.01');
74 8 has abstract => (is => 'ro', isa => 'Str', default => 'a great new dist');
75 8 has release_status => (is => 'ro', isa => 'Str', default => 'stable');
76  
77 #pod =attr cpan_author
78 #pod
79 #pod This is the PAUSE id of the author, like C<RJBS>.
80 #pod
81 #pod =attr archive_ext
82 #pod
83 #pod This is the extension of the archive to build, when you build an archive. This
84 #pod defaults to C<tar.gz>. C<zip> works. Other things might. Try it and find
85 #pod out.
86 #pod
87 #pod =attr append
88 #pod
89 #pod This is an arrayref of hashrefs, each of which looks like:
90 #pod
91 #pod { file => $filename, content => $character_string }
92 #pod
93 #pod The content will be UTF-8 encoded and put into a file with the given name.
94 #pod
95 #pod This feature is a bit weird. Maybe it will go away eventually.
96 #pod
97 #pod =attr mtime
98 #pod
99 #pod If given, this is the epoch seconds to which to set the mtime of the generated
100 #pod file. This is useful in rare occasions.
101 #pod
102 #pod =cut
103  
104 # Module::Faker options
105 8 has cpan_author => (is => 'ro', isa => 'Maybe[Str]', default => 'LOCAL');
106 8 has archive_ext => (is => 'ro', isa => 'Str', default => 'tar.gz');
107 20 ▶has append => (is => 'ro', isa => 'ArrayRef[HashRef]', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
108 8 has mtime => (is => 'ro', isa => 'Int', predicate => 'has_mtime');
109  
110 #pod =attr x_authority
111 #pod
112 #pod This is the C<X_Authority> header that gets put into the META files.
113 #pod
114 #pod =cut
115  
116 8 has x_authority => (is => 'ro', isa => 'Str');
117  
118 #pod =attr license
119 #pod
120 #pod This is the meta spec license string for the distribution. It defaults to
121 #pod C<perl_5>.
122 #pod
123 #pod =cut
124  
125 has license => (
126 is => 'ro',
127 isa => 'ArrayRef[Str]',
128 24 ▶ default => sub { [ 'perl_5' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 8 );
130  
131 #pod =attr authors
132 #pod
133 #pod This is an array of strings who are used as the authors in the dist metadata.
134 #pod The default is:
135 #pod
136 #pod [ "AUTHOR <AUTHOR@cpan.local>" ]
137 #pod
138 #pod ...where C<AUTHOR> is the C<cpan_author> of the dist.
139 #pod
140 #pod =cut
141  
142 has authors => (
143 isa => 'ArrayRef[Str]',
144 lazy => 1,
145 traits => [ 'Array' ],
146 handles => { authors => 'elements' },
147 21 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 21 my ($self) = @_;
149 21 return [ sprintf '%s <%s@cpan.local>', ($self->cpan_author) x 2 ];
150 },
151 8 );
152  
153 #pod =attr include_provides_in_meta
154 #pod
155 #pod This is a bool. If true, the produced META files will include a C<provides>
156 #pod key based on the packages in the dist. It defaults to false, to match the
157 #pod most common behavior of dists in the wild.
158 #pod
159 #pod =cut
160  
161 8 has include_provides_in_meta => (
162 is => 'ro',
163 isa => 'Bool',
164 default => 0,
165 );
166  
167 #pod =attr provides
168 #pod
169 #pod This is a hashref that gets used as the C<provides> in the metadata.
170 #pod
171 #pod If not provided, it is built from the C<packages> provided in construction.
172 #pod
173 #pod If no packages were provided, for a dist named Foo-Bar, it defaults to:
174 #pod
175 #pod { 'Foo::Bar' => { version => $DIST_VERSION, file => "lib/Foo/Bar.pm" } }
176 #pod
177 #pod =cut
178  
179 8 has provides => (
180 is => 'ro',
181 isa => 'HashRef',
182 lazy_build => 1,
183 );
184  
185 15 ▶sub _build_provides {
Subroutine _build_provides: called CC=3 SCAR=14.2
186 15 my ($self) = @_;
187  
188 15 ▶ if ($self->has_packages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_packages) 0 15 15
189 return {
190 0 ▶ map {; $_->name => {
Branch: 1 branch
BranchTrueFalseTotal
defined $_->version ? : 0 0 0
191 file => $_->in_file,
192 (defined $_->version ? (version => $_->version) : ()),
193 } } $self->packages
194 }
195 }
196  
197 15 my $pkg = __dist_to_pkg($self->name);
198 return {
199 15 $pkg => {
200 version => $self->version,
201 file => __pkg_to_file($pkg),
202 }
203 };
204 };
205  
206 22 ▶sub __dor { defined $_[0] ? $_[0] : $_[1] }
Subroutine __dor: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 21 1 22
207  
208 58 ▶sub append_for {
Subroutine append_for: called CC=1 SCAR=0.0
Pod undocumented
209 58 my ($self, $filename) = @_;
210 return [
211 # YAML and JSON should both be in utf8 (if not plain ascii)
212 14 map { encode_utf8($_->{content}) }
213 30 grep { $filename eq $_->{file} }
214 58 @{ $self->append }
215 ];
216 }
217  
218 #pod =attr archive_basename
219 #pod
220 #pod If written to disk, the archive will be written to...
221 #pod
222 #pod $dist->archive_basename . '.' . $dist->archive_ext
223 #pod
224 #pod The default is:
225 #pod
226 #pod $dist->name . '.' . ($dist->version // 'undef')
227 #pod
228 #pod =cut
229  
230 has archive_basename => (
231 is => 'ro',
232 isa => 'Str',
233 lazy => 1,
234 22 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
235 22 my ($self) = @_;
236 22 return sprintf '%s-%s', $self->name, __dor($self->version, 'undef');
237 },
238 8 );
239  
240 #pod =attr omitted_files
241 #pod
242 #pod If given, this is an arrayref of filenames that shouldn't be automatically
243 #pod generated and included.
244 #pod
245 #pod =cut
246  
247 has omitted_files => (
248 isa => 'ArrayRef[Str]',
249 traits => [ 'Array' ],
250 handles => { omitted_files => 'elements' },
251 lazy => 1,
252 19 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
253 8 );
254  
255 15 ▶sub __dist_to_pkg { my $str = shift; $str =~ s/-/::/g; return $str; }
Subroutine __dist_to_pkg: called CC=1 SCAR=0.0
256 19 ▶sub __pkg_to_file { my $str = shift; $str =~ s{::}{/}g; return "lib/$str.pm"; }
Subroutine __pkg_to_file: called CC=1 SCAR=0.0
257  
258 # This is stupid, but copes with MakeMaker wanting to have a module name as its
259 # NAME parameter. Ugh! -- rjbs, 2008-03-13
260 21 ▶sub _pkgy_name {
Subroutine _pkgy_name: called CC=1 SCAR=0.0
261 21 my $name = shift->name;
262 21 $name =~ s/-/::/g;
263  
264 21 return $name;
265 }
266  
267 #pod =attr packages
268 #pod
269 #pod This is an array of L<Module::Faker::Package> objects. It's built by
270 #pod C<provides> if needed, but you might want to look at using the
271 #pod C<L</from_struct>> method to set it up.
272 #pod
273 #pod =cut
274  
275 8 has packages => (
276 isa => 'Module::Faker::Type::Packages',
277 lazy => 1,
278 builder => '_build_packages',
279 traits => [ 'Array' ],
280 handles => { packages => 'elements' },
281 predicate => 'has_packages',
282 );
283  
284 21 ▶sub _build_packages {
Subroutine _build_packages: called CC=4 SCAR=13.9
285 21 my ($self) = @_;
286  
287 21 my $provides = $self->provides;
288  
289 # do this dance so we don't autovivify X_Module_Faker in provides
290 21 my %package_order = map {;
291 41 ▶ $_ => (exists $provides->{$_}{X_Module_Faker} ? $provides->{$_}{X_Module_Faker}{order} : 0 )
Branch: 1 branch
BranchTrueFalseTotal
exists $provides->{$_}{'X_Module_Faker'} ? : 12 29 41
292 } keys %$provides;
293  
294 21 my @pkg_names = do {
295 8 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
296 21 sort { $package_order{$a} <=> $package_order{$b} } keys %package_order;
297 };
298  
299 21 my @packages;
300 21 for my $name (@pkg_names) {
301 push @packages, Module::Faker::Package->new({
302 name => $name,
303 version => $provides->{$name}{version},
304 in_file => $provides->{$name}{file},
305 41 ▶ ($provides->{$name}{style} ? (style => $provides->{$name}{style}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$provides->{$name}{'style'} ? : 4 37 41
306 });
307 }
308  
309 21 return \@packages;
310 }
311  
312 #pod =method modules
313 #pod
314 #pod This produces and returns a list of L<Module::Faker::Module> objects,
315 #pod representing modules. Modules, if you're not as steeped in CPAN toolchain
316 #pod nonsense, are the C<.pm> files in which packages are defined.
317 #pod
318 #pod These are produced by combining the packages from C<L</packages>> into files
319 #pod based on their C<in_file> attributes.
320 #pod
321 #pod =cut
322  
323 42 ▶sub modules {
Subroutine modules: called CC=3 SCAR=11.0
Pod documented
324 42 my ($self) = @_;
325  
326 42 my %module;
327 42 for my $pkg ($self->packages) {
328 82 my $filename = $pkg->in_file;
329  
330 82 ▶ push @{ $module{ $filename } ||= [] }, $pkg;
Condition100%2/2
$module{$filename} ||= []
l!l
2854
MC/DC100%1/1
$module{$filename} ||= []
$module{$filename}
Truth table100%2/2
A
A= $module{$filename}
Aresult
11
00
331 }
332  
333 my @modules = map {
334 42 Module::Faker::Module->new({
335 54 packages => $module{$_},
336 filename => $_,
337 append => $self->append_for($_)
338 });
339 } keys %module;
340  
341 42 return @modules;
342 }
343  
344 63 ▶sub _mk_container_path {
Subroutine _mk_container_path: called CC=1 SCAR=0.0
345 63 my ($self, $filename) = @_;
346  
347 63 my (@parts) = File::Spec->splitdir($filename);
348 63 my $leaf_filename = pop @parts;
349 63 File::Path::mkpath(File::Spec->catdir(@parts));
350 }
351  
352 #pod =method C<make_dist_dir>
353 #pod
354 #pod my $directory_name = $dist->make_dist_dir(\%arg);
355 #pod
356 #pod This returns the name of a directory into which the dist's contents have been
357 #pod written. If a C<dir> argument is provided, the dist will be written to a
358 #pod directory beneath that dir. Otherwise, it will be written below a temporary
359 #pod directory.
360 #pod
361 #pod =cut
362  
363 8 ▶sub make_dist_dir {
Subroutine make_dist_dir: called CC=6 SCAR=18.1
Pod documented
364 8 my ($self, $arg) = @_;
365 8 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
62
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
366  
367 8 ▶ my $dir = $arg->{dir} || File::Temp::tempdir;
Condition66%2/3
$arg->{'dir'} || File::Temp::tempdir()
l!l&&r!l&&!r
620
MC/DC0%0/2
$arg->{'dir'} || File::Temp::tempdir()
$arg->{'dir'} File::Temp::tempdir()
Truth table66%2/3
A || B
A= $arg->{'dir'}B= File::Temp::tempdir()
ABresult
1X1
011
000
368 8 my $dist_dir = File::Spec->catdir($dir, $self->archive_basename);
369  
370 8 for my $file ($self->files) {
371 50 my $fqfn = File::Spec->catfile($dist_dir, $file->filename);
372 50 $self->_mk_container_path($fqfn);
373  
374 50 ▶ open my $fh, '>', $fqfn or die "couldn't open $fqfn for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fqfn or die "couldn't open $fqfn for writing: $!" 50 0 50
375 50 print $fh $file->as_string;
376 50 ▶ close $fh or die "error when closing $fqfn: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "error when closing ${fqfn}: $!" 50 0 50
377 }
378  
379 8 return $dist_dir;
380 }
381  
382 0 ▶sub _author_dir_infix {
Subroutine _author_dir_infix: not called CC=2 SCAR=17.9
383 0 my ($self) = @_;
384  
385 0 ▶ Carp::croak "can't put archive in author dir with no author defined"
Branch: 1 branch
BranchTrueFalseTotal
unless my $pauseid = $self->cpan_author 0 0 0
386 unless my $pauseid = $self->cpan_author;
387  
388 # Sorta like pow- pow- power-wheels! -- rjbs, 2008-03-14
389 0 my ($pa, $p) = $pauseid =~ /^((.).)/;
390 0 return ($p, $pa, $pauseid);
391 }
392  
393 13 ▶sub archive_filename {
Subroutine archive_filename: called CC=2 SCAR=7.0
Pod undocumented
394 13 my ($self, $arg) = @_;
395  
396 13 my $base = $self->archive_basename;
397 13 my $ext = $self->archive_ext;
398  
399 return File::Spec->catfile(
400 13 ▶ ($arg->{author_prefix} ? $self->_author_dir_infix : ()),
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'author_prefix'} ? : 0 13 13
401 "$base.$ext",
402 );
403 }
404  
405 #pod =method make_archive
406 #pod
407 #pod my $archive_filename = $dist->make_archive(\%arg);
408 #pod
409 #pod This writes the dist archive file, like a tarball or zip file. If a C<dir>
410 #pod argument is given, it will be written in that directory. Otherwise, it will be
411 #pod written to a temporary directory. If the C<author_prefix> argument is given
412 #pod and true, it will be written under a hashed author dir, like:
413 #pod
414 #pod U/US/USERID/Foo-Bar-1.23.tar.gz
415 #pod
416 #pod =cut
417  
418 package
419 8 Module::Faker::Dist::ZipCreator {
420  
421 8 ▶ use parent 'Archive::Any::Create::Zip';
Subroutine BEGIN: called CC=1 SCAR=0.0
422  
423 0 ▶ sub add_file {
Subroutine add_file: not called CC=1 SCAR=6.9
424 0 my $self = shift;
425 0 my($file, $data) = @_;
426  
427 0 my $member = $self->SUPER::add_file($file, $data);
428 0 $member->unixFileAttributes(0644);
429  
430 0 return $member;
431 }
432 }
433  
434 13 ▶sub make_archive {
Subroutine make_archive: called CC=5 SCAR=16.2
Pod documented
435 13 my ($self, $arg) = @_;
436 13 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
130
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
437  
438 13 ▶ my $dir = $arg->{dir} || File::Temp::tempdir;
Condition33%1/3
$arg->{'dir'} || File::Temp::tempdir()
l!l&&r!l&&!r
1300
MC/DC0%0/2
$arg->{'dir'} || File::Temp::tempdir()
$arg->{'dir'} File::Temp::tempdir()
Truth table33%1/3
A || B
A= $arg->{'dir'}B= File::Temp::tempdir()
ABresult
1X1
011
000
439  
440 # This is, admittedly, sort of bananas. We're doing this because by default,
441 # Archive::Any::Create would make the files a+w. PAUSE will reject uploaded
442 # archives with files like that, and we want these archives to be useful for
443 # testing PAUSE.
444 local $Archive::Any::Create::Type2Class{zip} = [
445 13 'Module::Faker::Dist::ZipCreator'
446 ];
447  
448 13 local $INC{'Module/Faker/Dist/ZipCreator.pm'} = 1;
449  
450 13 my $archive = Archive::Any::Create->new;
451 13 my $container = $self->archive_basename;
452  
453 13 $archive->container($container);
454  
455 13 for my $file ($self->files) {
456 84 $archive->add_file($file->filename, $file->as_string);
457 }
458  
459 my $archive_filename = File::Spec->catfile(
460 $dir,
461 $self->archive_filename({ author_prefix => $arg->{author_prefix} })
462 13 );
463  
464 13 $self->_mk_container_path($archive_filename);
465 13 $archive->write_file($archive_filename);
466 13 ▶ utime time, $self->mtime, $archive_filename if $self->has_mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_mtime 1 12 13
467 13 return $archive_filename;
468 }
469  
470 21 ▶sub files {
Subroutine files: called CC=3 SCAR=11.0
Pod undocumented
471 21 my ($self) = @_;
472 21 my @files = ($self->modules, $self->_extras, $self->_manifest_file);
473 21 for my $file (@{$self->append}) {
474 10 ▶ next if grep { $_->filename eq $file->{file} } @files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->filename eq $file->{"file"};} @files 6 4 10
475 push(@files,
476 $self->_file_class->new(
477 filename => $file->{file},
478 content => '',
479 4 append => $self->append_for($file->{file}),
480 ) );
481 }
482 21 return @files;
483 }
484  
485 107 ▶sub _file_class { 'Module::Faker::File' }
Subroutine _file_class: called CC=1 SCAR=0.0
486  
487 24 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
488 24 my ($orig, $self, @rest) = @_;
489 24 my $arg = $self->$orig(@rest);
490  
491 confess "can't supply both requires and prereqs"
492 24 ▶ if $arg->{prereqs} && $arg->{requires};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"prereqs"} and $arg->{"requires"} 0 24 24
Condition66%2/3
$arg->{"prereqs"} and $arg->{"requires"}
!ll&&!rl&&r
2220
MC/DC0%0/2
$arg->{"prereqs"} and $arg->{"requires"}
$arg->{"prereqs"} $arg->{"requires"}
Truth table66%2/3
A and B
A= $arg->{"prereqs"}B= $arg->{"requires"}
ABresult
0X0
100
111
493  
494 24 ▶ if ($arg->{requires}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"requires"}) 2 22 24
495 $arg->{prereqs} = {
496 runtime => { requires => delete $arg->{requires} }
497 2 };
498 }
499  
500 24 return $arg;
501 8 };
502  
503 24 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.1
Pod undocumented
504 24 my ($self) = @_;
505 24 my $provides = $self->provides;
506  
507 24 ▶ $provides->{$_}{file} //= __pkg_to_file($_) for keys %$provides;
Condition66%2/3
$provides->{$_}{"file"} //= __pkg_to_file($_)
l!l&&r!l&&!r
4040
MC/DC0%0/2
$provides->{$_}{"file"} //= __pkg_to_file($_)
$provides->{$_}{"file"} __pkg_to_file($_)
Truth table66%2/3
A //= B
A= $provides->{$_}{"file"}B= __pkg_to_file($_)
ABresult
1X1
011
000
508 }
509  
510 has prereqs => (
511 is => 'ro',
512 isa => 'HashRef',
513 20 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
514 8 );
515  
516 has _manifest_file => (
517 is => 'ro',
518 isa => 'Module::Faker::File',
519 lazy => 1,
520 21 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
521 21 my ($self) = @_;
522 21 my @files = ($self->modules, $self->_extras);
523  
524 return $self->_file_class->new({
525 filename => 'MANIFEST',
526 content => join("\n",
527 'MANIFEST',
528 21 map { $_->filename } @files
529 ),
530 });
531 },
532 8 );
533  
534 #pod =attr more_metadata
535 #pod
536 #pod This can be given as a hashref of data to merge into the CPAN::Meta files.
537 #pod
538 #pod =cut
539  
540 8 has more_metadata => (
541 is => 'ro',
542 isa => 'HashRef',
543 predicate => 'has_more_metadata',
544 );
545  
546 #pod =attr meta_munger
547 #pod
548 #pod If given, this is a coderef that's called just before the CPAN::Meta data for
549 #pod the dist is written to disk, an can be used to change things, especially into
550 #pod invalid data. It is expected to return the new content to serialize.
551 #pod
552 #pod It's called like this:
553 #pod
554 #pod $coderef->($struct, { format => $format, version => $version });
555 #pod
556 #pod ...where C<$struct> is the result of C<< $cpan_meta->as_struct >>.
557 #pod C<$version> is the version number of the target metafile. Normally, both
558 #pod version 1.4 and 2 are requested. C<$format> is either C<yaml> or C<json>.
559 #pod
560 #pod If the munger returns a string instead of a structure, it will be used as the
561 #pod content of the file being written. This lets you put all kinds of nonsense in
562 #pod those meta files. Have fun, go nuts!
563 #pod
564 #pod =cut
565  
566 8 has meta_munger => (
567 isa => 'CodeRef',
568 predicate => 'has_meta_munger',
569 traits => [ 'Code' ],
570 handles => { munge_meta => 'execute' },
571 );
572  
573 8 has _cpan_meta => (
574 is => 'ro',
575 isa => 'CPAN::Meta',
576 lazy_build => 1,
577 );
578  
579 21 ▶sub _build__cpan_meta {
Subroutine _build__cpan_meta: called CC=7 SCAR=19.7
580 21 my ($self) = @_;
581 21 my $meta = {
582 'meta-spec' => { version => '2' },
583 dynamic_config => 0,
584 author => [ $self->authors ], # plural attribute that derefs
585 };
586 # required fields
587 21 for my $key ( qw/abstract license name release_status version/ ) {
588 105 $meta->{$key} = $self->$key;
589 }
590 # optional fields
591 21 for my $key ( qw/prereqs x_authority/ ) {
592 42 my $value = $self->$key;
593 42 ▶ $meta->{$key} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 21 21 42
594 }
595  
596 21 ▶ if ($self->provides && $self->include_provides_in_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->provides and $self->include_provides_in_meta) 1 20 21
Condition66%2/3
$self->provides and $self->include_provides_in_meta
!ll&&!rl&&r
0201
MC/DC50%1/2
$self->provides and $self->include_provides_in_meta
$self->provides $self->include_provides_in_meta
Truth table66%2/3
A and B
A= $self->providesB= $self->include_provides_in_meta
ABresult
0X0
100
111
597 1 $meta->{provides} = $self->provides;
598 }
599  
600 21 my $cpanmeta = CPAN::Meta->new( $meta, {lazy_validation => 1} );
601 21 ▶ return $cpanmeta unless $self->has_more_metadata;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_more_metadata 21 0 21
602  
603 0 return CPAN::Meta->new(
604 CPAN::Meta::Merge->new(default_version => 2)->merge(
605 $cpanmeta,
606 $self->more_metadata,
607 ),
608 { lazy_validation => 1 }
609 );
610 }
611  
612 has _extras => (
613 isa => 'ArrayRef[Module::Faker::File]',
614 lazy => 1,
615 traits => [ 'Array' ],
616 handles => { _extras => 'elements' },
617 21 ▶ default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
618 21 my ($self) = @_;
619 21 my @files;
620  
621 21 for my $filename (qw(Makefile.PL t/00-nop.t)) {
622 42 ▶ next if grep { $_ eq $filename } $self->omitted_files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $filename;} $self->omitted_files 0 42 42
623 42 push @files, $self->_file_class->new({
624 filename => $filename,
625 content => Module::Faker::Heavy->_render(
626 $filename,
627 { dist => $self },
628 ),
629 });
630 }
631  
632 21 ▶ unless ( grep { $_ eq 'META.json' } $self->omitted_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "META.json";} $self->omitted_files) 21 0 21
633 21 push @files, $self->_file_class->new({
634 filename => 'META.json',
635 content => $self->_meta_file_content(json => 2),
636 });
637 }
638  
639 21 ▶ unless ( grep { $_ eq 'META.yml' } $self->omitted_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "META.yml";} $self->omitted_files) 19 2 21
640 19 push @files, $self->_file_class->new({
641 filename => 'META.yml',
642 content => $self->_meta_file_content(yaml => 1.4),
643 });
644 }
645  
646 21 return \@files;
647 },
648 8 );
649  
650 # This code is based on the code in CPAN::Meta v2.150010
651 # -- rjbs, 2019-04-28
652 40 ▶sub _meta_file_content {
Subroutine _meta_file_content: called CC=8 SCAR=22.3
653 40 my ($self, $format, $version) = @_;
654  
655 40 my $meta = $self->_cpan_meta;
656  
657 40 my $struct;
658 40 ▶ if ($meta->meta_spec_version ne $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->meta_spec_version ne $version) { } 19 21 40
659 19 $struct = CPAN::Meta::Converter->new($meta->as_struct)
660 ->convert(version => $version);
661 } else {
662 21 $struct = $meta->as_struct;
663 }
664  
665 40 ▶ if ($self->has_meta_munger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_meta_munger) 0 40 40
666 # Is that dclone() paranoia? Maybe. -- rjbs, 2019-04-28
667 0 $struct = $self->munge_meta(
668 dclone($struct),
669 {
670 format => $format,
671 version => $version
672 },
673 );
674  
675 0 ▶ return $struct unless ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct 0 0 0
676 }
677  
678 40 my ($data, $backend);
679 40 ▶ if ($format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'json') { } 21 19 40
elsif ($format eq 'yaml') { } 19 0 19
680 21 $backend = Parse::CPAN::Meta->json_backend();
681 21 local $struct->{x_serialization_backend} = sprintf '%s version %s',
682 $backend, $backend->VERSION;
683 21 $data = $backend->new->pretty->canonical->encode($struct);
684 } elsif ($format eq 'yaml') {
685 19 $backend = Parse::CPAN::Meta->yaml_backend();
686 19 local $struct->{x_serialization_backend} = sprintf '%s version %s',
687 $backend, $backend->VERSION;
688 8 ▶ $data = eval { no strict 'refs'; &{"$backend\::Dump"}($struct) };
Subroutine BEGIN: called CC=1 SCAR=0.0
689 19 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
690 0 ▶ croak($backend->can('errstr') ? $backend->errstr : $@);
Branch: 1 branch
BranchTrueFalseTotal
$backend->can('errstr') ? : 0 0 0
691 }
692 } else {
693 0 confess "unknown meta format: $format"
694 }
695  
696 40 return $data;
697 }
698  
699 #pod =method from_file
700 #pod
701 #pod my $dist = Module::Faker::Dist->from_file($filename);
702 #pod
703 #pod Given a filename with dist configuration, this builds the dist described by the
704 #pod file.
705 #pod
706 #pod Given a file ending in C<yaml> or C<yml> or C<json>, it's treated as a
707 #pod CPAN::Meta file and interpreted as such. The key C<X_Module_Faker> can be
708 #pod present to provide attributes that don't match data found in a meta file.
709 #pod
710 #pod Given a file ending in C<dist>, all the configuration comes from the filename,
711 #pod which should look like this:
712 #pod
713 #pod AUTHOR_Dist-Name-1.234.tar.gz.dist
714 #pod
715 #pod =cut
716  
717 # TODO: make this a registry -- rjbs, 2008-03-12
718 8 my %HANDLER_FOR = (
719 yaml => '_from_meta_file',
720 yml => '_from_meta_file',
721 json => '_from_meta_file',
722 dist => '_from_distnameinfo'
723 );
724  
725 22 ▶sub from_file {
Subroutine from_file: called CC=3 SCAR=12.0
Pod documented
726 22 my ($self, $filename) = @_;
727  
728 22 my ($ext) = $filename =~ /.*\.(.+?)\z/;
729  
730 Carp::croak "don't know how to handle file $filename"
731 22 ▶ unless $ext and my $method = $HANDLER_FOR{$ext};
Branch: 1 branch
BranchTrueFalseTotal
unless $ext and my $method = $HANDLER_FOR{$ext} 0 22 22
Condition33%1/3
$ext and my $method = $HANDLER_FOR{$ext}
!ll&&!rl&&r
0022
MC/DC0%0/2
$ext and my $method = $HANDLER_FOR{$ext}
$ext my $method = $HANDLER_FOR{$ext}
Truth table33%1/3
A and B
A= $extB= my $method = $HANDLER_FOR{$ext}
ABresult
0X0
100
111
732  
733 22 $self->$method($filename);
734 }
735  
736 2 ▶sub _from_distnameinfo {
Subroutine _from_distnameinfo: called CC=1 SCAR=0.0
737 2 my ($self, $filename) = @_;
738 2 $filename = file($filename)->basename;
739 2 $filename =~ s/\.dist$//;
740  
741 2 my ($author, $path) = split /_/, $filename, 2;
742  
743 2 my $dni = CPAN::DistnameInfo->new($path);
744  
745 2 return $self->new({
746 name => $dni->dist,
747 version => $dni->version,
748 abstract => sprintf('the %s dist', $dni->dist),
749 archive_ext => $dni->extension,
750 cpan_author => $author,
751 });
752 }
753  
754 20 ▶sub _from_meta_file {
Subroutine _from_meta_file: called CC=2 SCAR=6.9
755 20 my ($self, $filename) = @_;
756  
757 20 my $data = Parse::CPAN::Meta->load_file($filename);
758 20 ▶ my $extra = (delete $data->{X_Module_Faker}) || {};
Condition100%2/2
delete $data->{'X_Module_Faker'} || {}
l!l
812
MC/DC100%1/1
delete $data->{'X_Module_Faker'} || {}
delete $data->{'X_Module_Faker'}
Truth table100%2/2
A
A= delete $data->{'X_Module_Faker'}
Aresult
11
00
759 20 my $dist = $self->new({ %$data, %$extra });
760 }
761  
762 21 ▶sub _flat_prereqs {
Subroutine _flat_prereqs: called CC=2 SCAR=6.9
763 21 my ($self) = @_;
764 21 my $prereqs = $self->_cpan_meta->effective_prereqs;
765 21 my $req = CPAN::Meta::Requirements->new;
766 21 for my $phase ( qw/runtime build test/ ) {
767 63 $req->add_requirements( $prereqs->requirements_for( $phase, 'requires' ) );
768 }
769 21 return %{ $req->as_string_hash };
770 }
771  
772 #pod =method from_struct
773 #pod
774 #pod my $dist = Module::Faker::Dist->from_struct(\%arg);
775 #pod
776 #pod This is sugar over C<new>, working like this:
777 #pod
778 #pod =for :list
779 #pod * packages version defaults to the dist version unless specified
780 #pod * packages for dist Foo-Bar defaults to Foo::Bar unless specified
781 #pod * if specified, packages is an L<optlist|Data::OptList>
782 #pod
783 #pod =cut
784  
785 0 ▶sub from_struct {
Subroutine from_struct: not called CC=8 SCAR=42.8
Pod documented
786 0 my ($self, $arg) = @_;
787  
788 0 ▶ my $version = exists $arg->{version} ? $arg->{version} : $DEFAULT_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{'version'} ? : 0 0 0
789  
790 my $specs = Data::OptList::mkopt(
791 ! exists $arg->{packages} ? [ __dist_to_pkg($arg->{name}) ]
792 : ref $arg->{packages} ? $arg->{packages}
793 0 ▶ : defined $arg->{packages} ? [ $arg->{packages} ]
Branch: 3 branches
BranchTrueFalseTotal
!exists $arg->{'packages'} ? : 0 0 0
ref $arg->{'packages'} ? : 0 0 0
defined $arg->{'packages'} ? : 0 0 0
794 : ()
795 );
796  
797 0 my @packages;
798 0 for my $spec (@$specs) {
799 0 ▶ my %spec = $spec->[1] ? %{ $spec->[1] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$spec->[1] ? : 0 0 0
800  
801 push @packages, Module::Faker::Package->new({
802 name => $spec->[0],
803 in_file => __pkg_to_file($spec->[0]), # to be overridden below if needed
804 %spec,
805 0 ▶ version => (exists $spec{version} ? $spec{version} : $version),
Branch: 1 branch
BranchTrueFalseTotal
exists $spec{'version'} ? : 0 0 0
806 });
807 }
808  
809 0 return $self->new({
810 %$arg,
811 version => $version,
812 packages => \@packages,
813 });
814 }
815  
816 8 1;
817  
818 # vim: ts=2 sts=2 sw=2 et:
819  
820 __END__
↑ Summary Module/Faker/Package.pm »