2 package Module::Install::Metadata;
5 use Module::Install::Base ();
7 use vars qw{$VERSION @ISA $ISCORE};
10 @ISA = 'Module::Install::Base';
14 my @boolean_keys = qw{
38 my @resource_keys = qw{
49 sub Meta_BooleanKeys { @boolean_keys }
50 sub Meta_ScalarKeys { @scalar_keys }
51 sub Meta_TupleKeys { @tuple_keys }
52 sub Meta_ResourceKeys { @resource_keys }
53 sub Meta_ArrayKeys { @array_keys }
55 foreach my $key ( @boolean_keys ) {
58 if ( defined wantarray and not @_ ) {
59 return $self->{values}->{$key};
61 $self->{values}->{$key} = ( @_ ? $_[0] : 1 );
66 foreach my $key ( @scalar_keys ) {
69 return $self->{values}->{$key} if defined wantarray and !@_;
70 $self->{values}->{$key} = shift;
75 foreach my $key ( @array_keys ) {
78 return $self->{values}->{$key} if defined wantarray and !@_;
79 $self->{values}->{$key} ||= [];
80 push @{$self->{values}->{$key}}, @_;
85 foreach my $key ( @resource_keys ) {
89 return () unless $self->{values}->{resources};
90 return map { $_->[1] }
91 grep { $_->[0] eq $key }
92 @{ $self->{values}->{resources} };
94 return $self->{values}->{resources}->{$key} unless @_;
95 my $uri = shift or die(
96 "Did not provide a value to $key()"
98 $self->resources( $key => $uri );
103 foreach my $key ( grep { $_ ne "resources" } @tuple_keys) {
106 return $self->{values}->{$key} unless @_;
109 my $module = shift or last;
110 my $version = shift || 0;
111 push @added, [ $module, $version ];
113 push @{ $self->{values}->{$key} }, @added;
114 return map {@$_} @added;
119 my %lc_resource = map { $_ => 1 } qw{
129 my $name = shift or last;
130 my $value = shift or next;
131 if ( $name eq lc $name and ! $lc_resource{$name} ) {
132 die("Unsupported reserved lowercase resource '$name'");
134 $self->{values}->{resources} ||= [];
135 push @{ $self->{values}->{resources} }, [ $name, $value ];
137 $self->{values}->{resources};
140 # Aliases for build_requires that will have alternative
141 # meanings in some future version of META.yml.
142 sub test_requires { shift->build_requires(@_) }
143 sub install_requires { shift->build_requires(@_) }
145 # Aliases for installdirs options
146 sub install_as_core { $_[0]->installdirs('perl') }
147 sub install_as_cpan { $_[0]->installdirs('site') }
148 sub install_as_site { $_[0]->installdirs('site') }
149 sub install_as_vendor { $_[0]->installdirs('vendor') }
154 warn "You MUST provide an explicit true/false value to dynamic_config\n";
157 $self->{values}->{dynamic_config} = $_[0] ? 1 : 0;
163 return $self->{values}->{perl_version} unless @_;
164 my $version = shift or die(
165 "Did not provide a value to perl_version()"
168 # Normalize the version
169 $version = $self->_perl_version($version);
171 # We don't support the reall old versions
172 unless ( $version >= 5.005 ) {
173 die "Module::Install only supports 5.005 or newer (use ExtUtils::MakeMaker)\n";
176 $self->{values}->{perl_version} = $version;
181 perl => 'http://dev.perl.org/licenses/',
182 apache => 'http://apache.org/licenses/LICENSE-2.0',
183 artistic => 'http://opensource.org/licenses/artistic-license.php',
184 artistic_2 => 'http://opensource.org/licenses/artistic-license-2.0.php',
185 lgpl => 'http://opensource.org/licenses/lgpl-license.php',
186 lgpl2 => 'http://opensource.org/licenses/lgpl-2.1.php',
187 lgpl3 => 'http://opensource.org/licenses/lgpl-3.0.html',
188 bsd => 'http://opensource.org/licenses/bsd-license.php',
189 gpl => 'http://opensource.org/licenses/gpl-license.php',
190 gpl2 => 'http://opensource.org/licenses/gpl-2.0.php',
191 gpl3 => 'http://opensource.org/licenses/gpl-3.0.html',
192 mit => 'http://opensource.org/licenses/mit-license.php',
193 mozilla => 'http://opensource.org/licenses/mozilla1.1.php',
194 open_source => undef,
195 unrestricted => undef,
196 restrictive => undef,
202 return $self->{values}->{license} unless @_;
203 my $license = shift or die(
204 'Did not provide a value to license()'
206 $self->{values}->{license} = $license;
208 # Automatically fill in license URLs
209 if ( $license_urls{$license} ) {
210 $self->resources( license => $license_urls{$license} );
217 my ( $self, $file ) = @_;
219 unless ( defined($file) ) {
220 my $name = $self->name or die(
221 "all_from called with no args without setting name() first"
223 $file = join('/', 'lib', split(/-/, $name)) . '.pm';
224 $file =~ s{.*/}{} unless -e $file;
225 unless ( -e $file ) {
226 die("all_from cannot find $file from $name");
229 unless ( -f $file ) {
230 die("The path '$file' does not exist, or is not a file");
233 # Some methods pull from POD instead of code.
234 # If there is a matching .pod, use that instead
236 $pod =~ s/\.pm$/.pod/i;
237 $pod = $file unless -e $pod;
239 # Pull the different values
240 $self->name_from($file) unless $self->name;
241 $self->version_from($file) unless $self->version;
242 $self->perl_version_from($file) unless $self->perl_version;
243 $self->author_from($pod) unless $self->author;
244 $self->license_from($pod) unless $self->license;
245 $self->abstract_from($pod) unless $self->abstract;
252 my $provides = ( $self->{values}->{provides} ||= {} );
253 %$provides = (%$provides, @_) if @_;
259 return $self unless $self->is_admin;
260 unless (-e 'MANIFEST') {
261 warn "Cannot deduce auto_provides without a MANIFEST, skipping\n";
264 # Avoid spurious warnings as we are not checking manifest here.
265 local $SIG{__WARN__} = sub {1};
266 require ExtUtils::Manifest;
267 local *ExtUtils::Manifest::manicheck = sub { return };
269 require Module::Build;
270 my $build = Module::Build->new(
271 dist_name => $self->name,
272 dist_version => $self->version,
273 license => $self->license,
275 $self->provides( %{ $build->find_dist_packages || {} } );
281 my $features = ( $self->{values}->{features} ||= [] );
284 if ( @_ == 1 and ref( $_[0] ) ) {
285 # The user used ->feature like ->features by passing in the second
286 # argument as a reference. Accomodate for that.
296 ref($_) ? ( ref($_) eq 'HASH' ) ? %$_ : @$_ : $_
306 while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) {
307 $self->feature( $name, @$mods );
309 return $self->{values}->{features}
310 ? @{ $self->{values}->{features} }
317 push @{ $self->{values}->{no_index}->{$type} }, @_ if $type;
318 return $self->{values}->{no_index};
323 $self->include_deps( 'YAML::Tiny', 0 );
326 my $data = YAML::Tiny::LoadFile('META.yml');
328 # Call methods explicitly in case user has already set some values.
329 while ( my ( $key, $value ) = each %$data ) {
330 next unless $self->can($key);
331 if ( ref $value eq 'HASH' ) {
332 while ( my ( $module, $version ) = each %$value ) {
333 $self->can($key)->($self, $module => $version );
336 $self->can($key)->($self, $value);
344 return $self unless $self->is_admin;
345 $self->admin->write_meta;
350 require ExtUtils::MM_Unix;
351 my ( $self, $file ) = @_;
352 $self->version( ExtUtils::MM_Unix->parse_version($file) );
356 require ExtUtils::MM_Unix;
357 my ( $self, $file ) = @_;
360 { DISTNAME => $self->name },
362 )->parse_abstract($file)
366 # Add both distribution and module name
368 my ($self, $file) = @_;
370 Module::Install::_read($file) =~ m/
377 my ($name, $module_name) = ($1, $1);
380 unless ( $self->module_name ) {
381 $self->module_name($module_name);
384 die("Cannot determine name from $file\n");
388 sub perl_version_from {
391 Module::Install::_read($_[0]) =~ m/
399 my $perl_version = $1;
400 $perl_version =~ s{_}{}g;
401 $self->perl_version($perl_version);
403 warn "Cannot determine perl version info from $_[0]\n";
410 my $content = Module::Install::_read($_[0]);
412 =head \d \s+ (?:authors?)\b \s*
415 =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
416 .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
419 my $author = $1 || $2;
420 $author =~ s{E<lt>}{<}g;
421 $author =~ s{E<gt>}{>}g;
422 $self->author($author);
424 warn "Cannot determine author info from $_[0]\n";
431 Module::Install::_read($_[0]) =~ m/
434 (?:licen[cs]e|licensing|copyright|legal)\b
440 my $license_text = $1;
442 'under the same (?:terms|license) as (?:perl|the perl programming language) itself' => 'perl', 1,
443 'GNU general public license' => 'gpl', 1,
444 'GNU public license' => 'gpl', 1,
445 'GNU lesser general public license' => 'lgpl', 1,
446 'GNU lesser public license' => 'lgpl', 1,
447 'GNU library general public license' => 'lgpl', 1,
448 'GNU library public license' => 'lgpl', 1,
449 'BSD license' => 'bsd', 1,
450 'Artistic license' => 'artistic', 1,
454 'Artistic' => 'artistic', 1,
456 'proprietary' => 'proprietary', 0,
458 while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
459 $pattern =~ s{\s+}{\\s+}g;
460 if ( $license_text =~ /\b$pattern\b/i ) {
461 $self->license($license);
467 warn "Cannot determine license info from $_[0]\n";
471 sub _extract_bugtracker {
472 my @links = $_[0] =~ m#L<(\Qhttp://rt.cpan.org/\E[^>]+)>#g;
479 sub bugtracker_from {
481 my $content = Module::Install::_read($_[0]);
482 my @links = _extract_bugtracker($content);
484 warn "Cannot determine bugtracker info from $_[0]\n";
488 warn "Found more than on rt.cpan.org link in $_[0]\n";
493 bugtracker( $links[0] );
499 my $content = Module::Install::_readperl($_[0]);
500 my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
501 while ( @requires ) {
502 my $module = shift @requires;
503 my $version = shift @requires;
504 $self->requires( $module => $version );
508 sub test_requires_from {
510 my $content = Module::Install::_readperl($_[0]);
511 my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
512 while ( @requires ) {
513 my $module = shift @requires;
514 my $version = shift @requires;
515 $self->test_requires( $module => $version );
519 # Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
520 # numbers (eg, 5.006001 or 5.008009).
521 # Also, convert double-part versions (eg, 5.8)
524 $v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
525 $v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
526 $v =~ s/(\.\d\d\d)000$/$1/;
539 ######################################################################
543 die "WriteMyMeta has been deprecated";
546 sub write_mymeta_yaml {
549 # We need YAML::Tiny to write the MYMETA.yml file
550 unless ( eval { require YAML::Tiny; 1; } ) {
555 my $meta = $self->_write_mymeta_data or return 1;
557 # Save as the MYMETA.yml file
558 print "Writing MYMETA.yml\n";
559 YAML::Tiny::DumpFile('MYMETA.yml', $meta);
562 sub write_mymeta_json {
565 # We need JSON to write the MYMETA.json file
566 unless ( eval { require JSON; 1; } ) {
571 my $meta = $self->_write_mymeta_data or return 1;
573 # Save as the MYMETA.yml file
574 print "Writing MYMETA.json\n";
575 Module::Install::_write(
577 JSON->new->pretty(1)->canonical->encode($meta),
581 sub _write_mymeta_data {
584 # If there's no existing META.yml there is nothing we can do
585 return undef unless -f 'META.yml';
587 # We need Parse::CPAN::Meta to load the file
588 unless ( eval { require Parse::CPAN::Meta; 1; } ) {
592 # Merge the perl version into the dependencies
593 my $val = $self->Meta->{values};
594 my $perl = delete $val->{perl_version};
596 $val->{requires} ||= [];
597 my $requires = $val->{requires};
599 # Canonize to three-dot version after Perl 5.6
600 if ( $perl >= 5.006 ) {
601 $perl =~ s{^(\d+)\.(\d\d\d)(\d*)}{join('.', $1, int($2||0), int($3||0))}e
603 unshift @$requires, [ perl => $perl ];
606 # Load the advisory META.yml file
607 my @yaml = Parse::CPAN::Meta::LoadFile('META.yml');
610 # Overwrite the non-configure dependency hashs
611 delete $meta->{requires};
612 delete $meta->{build_requires};
613 delete $meta->{recommends};
614 if ( exists $val->{requires} ) {
615 $meta->{requires} = { map { @$_ } @{ $val->{requires} } };
617 if ( exists $val->{build_requires} ) {
618 $meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };