1#line 1
2package Module::Install::Metadata;
3
4use strict 'vars';
5use Module::Install::Base ();
6
7use vars qw{$VERSION @ISA $ISCORE};
8BEGIN {
9	$VERSION = '0.93';
10	@ISA     = 'Module::Install::Base';
11	$ISCORE  = 1;
12}
13
14my @boolean_keys = qw{
15	sign
16};
17
18my @scalar_keys = qw{
19	name
20	module_name
21	abstract
22	author
23	version
24	distribution_type
25	tests
26	installdirs
27};
28
29my @tuple_keys = qw{
30	configure_requires
31	build_requires
32	requires
33	recommends
34	bundles
35	resources
36};
37
38my @resource_keys = qw{
39	homepage
40	bugtracker
41	repository
42};
43
44my @array_keys = qw{
45	keywords
46};
47
48sub Meta              { shift          }
49sub Meta_BooleanKeys  { @boolean_keys  }
50sub Meta_ScalarKeys   { @scalar_keys   }
51sub Meta_TupleKeys    { @tuple_keys    }
52sub Meta_ResourceKeys { @resource_keys }
53sub Meta_ArrayKeys    { @array_keys    }
54
55foreach my $key ( @boolean_keys ) {
56	*$key = sub {
57		my $self = shift;
58		if ( defined wantarray and not @_ ) {
59			return $self->{values}->{$key};
60		}
61		$self->{values}->{$key} = ( @_ ? $_[0] : 1 );
62		return $self;
63	};
64}
65
66foreach my $key ( @scalar_keys ) {
67	*$key = sub {
68		my $self = shift;
69		return $self->{values}->{$key} if defined wantarray and !@_;
70		$self->{values}->{$key} = shift;
71		return $self;
72	};
73}
74
75foreach my $key ( @array_keys ) {
76	*$key = sub {
77		my $self = shift;
78		return $self->{values}->{$key} if defined wantarray and !@_;
79		$self->{values}->{$key} ||= [];
80		push @{$self->{values}->{$key}}, @_;
81		return $self;
82	};
83}
84
85foreach my $key ( @resource_keys ) {
86	*$key = sub {
87		my $self = shift;
88		unless ( @_ ) {
89			return () unless $self->{values}->{resources};
90			return map  { $_->[1] }
91			       grep { $_->[0] eq $key }
92			       @{ $self->{values}->{resources} };
93		}
94		return $self->{values}->{resources}->{$key} unless @_;
95		my $uri = shift or die(
96			"Did not provide a value to $key()"
97		);
98		$self->resources( $key => $uri );
99		return 1;
100	};
101}
102
103foreach my $key ( grep { $_ ne "resources" } @tuple_keys) {
104	*$key = sub {
105		my $self = shift;
106		return $self->{values}->{$key} unless @_;
107		my @added;
108		while ( @_ ) {
109			my $module  = shift or last;
110			my $version = shift || 0;
111			push @added, [ $module, $version ];
112		}
113		push @{ $self->{values}->{$key} }, @added;
114		return map {@$_} @added;
115	};
116}
117
118# Resource handling
119my %lc_resource = map { $_ => 1 } qw{
120	homepage
121	license
122	bugtracker
123	repository
124};
125
126sub resources {
127	my $self = shift;
128	while ( @_ ) {
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'");
133		}
134		$self->{values}->{resources} ||= [];
135		push @{ $self->{values}->{resources} }, [ $name, $value ];
136	}
137	$self->{values}->{resources};
138}
139
140# Aliases for build_requires that will have alternative
141# meanings in some future version of META.yml.
142sub test_requires     { shift->build_requires(@_) }
143sub install_requires  { shift->build_requires(@_) }
144
145# Aliases for installdirs options
146sub install_as_core   { $_[0]->installdirs('perl')   }
147sub install_as_cpan   { $_[0]->installdirs('site')   }
148sub install_as_site   { $_[0]->installdirs('site')   }
149sub install_as_vendor { $_[0]->installdirs('vendor') }
150
151sub dynamic_config {
152	my $self = shift;
153	unless ( @_ ) {
154		warn "You MUST provide an explicit true/false value to dynamic_config\n";
155		return $self;
156	}
157	$self->{values}->{dynamic_config} = $_[0] ? 1 : 0;
158	return 1;
159}
160
161sub perl_version {
162	my $self = shift;
163	return $self->{values}->{perl_version} unless @_;
164	my $version = shift or die(
165		"Did not provide a value to perl_version()"
166	);
167
168	# Normalize the version
169	$version = $self->_perl_version($version);
170
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";
174	}
175
176	$self->{values}->{perl_version} = $version;
177}
178
179#Stolen from M::B
180my %license_urls = (
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,
197    unknown      => undef,
198);
199
200sub license {
201	my $self = shift;
202	return $self->{values}->{license} unless @_;
203	my $license = shift or die(
204		'Did not provide a value to license()'
205	);
206	$self->{values}->{license} = $license;
207
208	# Automatically fill in license URLs
209	if ( $license_urls{$license} ) {
210		$self->resources( license => $license_urls{$license} );
211	}
212
213	return 1;
214}
215
216sub all_from {
217	my ( $self, $file ) = @_;
218
219	unless ( defined($file) ) {
220		my $name = $self->name or die(
221			"all_from called with no args without setting name() first"
222		);
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");
227		}
228	}
229	unless ( -f $file ) {
230		die("The path '$file' does not exist, or is not a file");
231	}
232
233	$self->{values}{all_from} = $file;
234
235	# Some methods pull from POD instead of code.
236	# If there is a matching .pod, use that instead
237	my $pod = $file;
238	$pod =~ s/\.pm$/.pod/i;
239	$pod = $file unless -e $pod;
240
241	# Pull the different values
242	$self->name_from($file)         unless $self->name;
243	$self->version_from($file)      unless $self->version;
244	$self->perl_version_from($file) unless $self->perl_version;
245	$self->author_from($pod)        unless $self->author;
246	$self->license_from($pod)       unless $self->license;
247	$self->abstract_from($pod)      unless $self->abstract;
248
249	return 1;
250}
251
252sub provides {
253	my $self     = shift;
254	my $provides = ( $self->{values}->{provides} ||= {} );
255	%$provides = (%$provides, @_) if @_;
256	return $provides;
257}
258
259sub auto_provides {
260	my $self = shift;
261	return $self unless $self->is_admin;
262	unless (-e 'MANIFEST') {
263		warn "Cannot deduce auto_provides without a MANIFEST, skipping\n";
264		return $self;
265	}
266	# Avoid spurious warnings as we are not checking manifest here.
267	local $SIG{__WARN__} = sub {1};
268	require ExtUtils::Manifest;
269	local *ExtUtils::Manifest::manicheck = sub { return };
270
271	require Module::Build;
272	my $build = Module::Build->new(
273		dist_name    => $self->name,
274		dist_version => $self->version,
275		license      => $self->license,
276	);
277	$self->provides( %{ $build->find_dist_packages || {} } );
278}
279
280sub feature {
281	my $self     = shift;
282	my $name     = shift;
283	my $features = ( $self->{values}->{features} ||= [] );
284	my $mods;
285
286	if ( @_ == 1 and ref( $_[0] ) ) {
287		# The user used ->feature like ->features by passing in the second
288		# argument as a reference.  Accomodate for that.
289		$mods = $_[0];
290	} else {
291		$mods = \@_;
292	}
293
294	my $count = 0;
295	push @$features, (
296		$name => [
297			map {
298				ref($_) ? ( ref($_) eq 'HASH' ) ? %$_ : @$_ : $_
299			} @$mods
300		]
301	);
302
303	return @$features;
304}
305
306sub features {
307	my $self = shift;
308	while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) {
309		$self->feature( $name, @$mods );
310	}
311	return $self->{values}->{features}
312		? @{ $self->{values}->{features} }
313		: ();
314}
315
316sub no_index {
317	my $self = shift;
318	my $type = shift;
319	push @{ $self->{values}->{no_index}->{$type} }, @_ if $type;
320	return $self->{values}->{no_index};
321}
322
323sub read {
324	my $self = shift;
325	$self->include_deps( 'YAML::Tiny', 0 );
326
327	require YAML::Tiny;
328	my $data = YAML::Tiny::LoadFile('META.yml');
329
330	# Call methods explicitly in case user has already set some values.
331	while ( my ( $key, $value ) = each %$data ) {
332		next unless $self->can($key);
333		if ( ref $value eq 'HASH' ) {
334			while ( my ( $module, $version ) = each %$value ) {
335				$self->can($key)->($self, $module => $version );
336			}
337		} else {
338			$self->can($key)->($self, $value);
339		}
340	}
341	return $self;
342}
343
344sub write {
345	my $self = shift;
346	return $self unless $self->is_admin;
347	$self->admin->write_meta;
348	return $self;
349}
350
351sub version_from {
352	require ExtUtils::MM_Unix;
353	my ( $self, $file ) = @_;
354	$self->version( ExtUtils::MM_Unix->parse_version($file) );
355}
356
357sub abstract_from {
358	require ExtUtils::MM_Unix;
359	my ( $self, $file ) = @_;
360	$self->abstract(
361		bless(
362			{ DISTNAME => $self->name },
363			'ExtUtils::MM_Unix'
364		)->parse_abstract($file)
365	 );
366}
367
368# Add both distribution and module name
369sub name_from {
370	my ($self, $file) = @_;
371	if (
372		Module::Install::_read($file) =~ m/
373		^ \s*
374		package \s*
375		([\w:]+)
376		\s* ;
377		/ixms
378	) {
379		my ($name, $module_name) = ($1, $1);
380		$name =~ s{::}{-}g;
381		$self->name($name);
382		unless ( $self->module_name ) {
383			$self->module_name($module_name);
384		}
385	} else {
386		die("Cannot determine name from $file\n");
387	}
388}
389
390sub _extract_perl_version {
391	if (
392		$_[0] =~ m/
393		^\s*
394		(?:use|require) \s*
395		v?
396		([\d_\.]+)
397		\s* ;
398		/ixms
399	) {
400		my $perl_version = $1;
401		$perl_version =~ s{_}{}g;
402		return $perl_version;
403	} else {
404		return;
405	}
406}
407
408sub perl_version_from {
409	my $self = shift;
410	my $perl_version=_extract_perl_version(Module::Install::_read($_[0]));
411	if ($perl_version) {
412		$self->perl_version($perl_version);
413	} else {
414		warn "Cannot determine perl version info from $_[0]\n";
415		return;
416	}
417}
418
419sub author_from {
420	my $self    = shift;
421	my $content = Module::Install::_read($_[0]);
422	if ($content =~ m/
423		=head \d \s+ (?:authors?)\b \s*
424		([^\n]*)
425		|
426		=head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
427		.*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
428		([^\n]*)
429	/ixms) {
430		my $author = $1 || $2;
431		$author =~ s{E<lt>}{<}g;
432		$author =~ s{E<gt>}{>}g;
433		$self->author($author);
434	} else {
435		warn "Cannot determine author info from $_[0]\n";
436	}
437}
438
439sub _extract_license {
440	if (
441		$_[0] =~ m/
442		(
443			=head \d \s+
444			(?:licen[cs]e|licensing|copyrights?|legal)\b
445			.*?
446		)
447		(=head\\d.*|=cut.*|)
448		\z
449	/ixms ) {
450		my $license_text = $1;
451		my @phrases      = (
452			'under the same (?:terms|license) as (?:perl|the perl programming language)' => 'perl', 1,
453			'under the terms of (?:perl|the perl programming language) itself' => 'perl', 1,
454			'Artistic and GPL'                   => 'perl',        1,
455			'GNU general public license'         => 'gpl',         1,
456			'GNU public license'                 => 'gpl',         1,
457			'GNU lesser general public license'  => 'lgpl',        1,
458			'GNU lesser public license'          => 'lgpl',        1,
459			'GNU library general public license' => 'lgpl',        1,
460			'GNU library public license'         => 'lgpl',        1,
461			'BSD license'                        => 'bsd',         1,
462			'Artistic license'                   => 'artistic',    1,
463			'GPL'                                => 'gpl',         1,
464			'LGPL'                               => 'lgpl',        1,
465			'BSD'                                => 'bsd',         1,
466			'Artistic'                           => 'artistic',    1,
467			'MIT'                                => 'mit',         1,
468			'proprietary'                        => 'proprietary', 0,
469		);
470		while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
471			$pattern =~ s#\s+#\\s+#gs;
472			if ( $license_text =~ /\b$pattern\b/i ) {
473			        return $license;
474			}
475		}
476	} else {
477	        return;
478	}
479}
480
481sub license_from {
482	my $self = shift;
483	if (my $license=_extract_license(Module::Install::_read($_[0]))) {
484		$self->license($license);
485	} else {
486		warn "Cannot determine license info from $_[0]\n";
487		return 'unknown';
488	}
489}
490
491sub _extract_bugtracker {
492	my @links   = $_[0] =~ m#L<(
493	 \Qhttp://rt.cpan.org/\E[^>]+|
494	 \Qhttp://github.com/\E[\w_]+/[\w_]+/issues|
495	 \Qhttp://code.google.com/p/\E[\w_\-]+/issues/list
496	 )>#gx;
497	my %links;
498	@links{@links}=();
499	@links=keys %links;
500	return @links;
501}
502
503sub bugtracker_from {
504	my $self    = shift;
505	my $content = Module::Install::_read($_[0]);
506	my @links   = _extract_bugtracker($content);
507	unless ( @links ) {
508		warn "Cannot determine bugtracker info from $_[0]\n";
509		return 0;
510	}
511	if ( @links > 1 ) {
512		warn "Found more than one bugtracker link in $_[0]\n";
513		return 0;
514	}
515
516	# Set the bugtracker
517	bugtracker( $links[0] );
518	return 1;
519}
520
521sub requires_from {
522	my $self     = shift;
523	my $content  = Module::Install::_readperl($_[0]);
524	my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
525	while ( @requires ) {
526		my $module  = shift @requires;
527		my $version = shift @requires;
528		$self->requires( $module => $version );
529	}
530}
531
532sub test_requires_from {
533	my $self     = shift;
534	my $content  = Module::Install::_readperl($_[0]);
535	my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
536	while ( @requires ) {
537		my $module  = shift @requires;
538		my $version = shift @requires;
539		$self->test_requires( $module => $version );
540	}
541}
542
543# Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
544# numbers (eg, 5.006001 or 5.008009).
545# Also, convert double-part versions (eg, 5.8)
546sub _perl_version {
547	my $v = $_[-1];
548	$v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
549	$v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
550	$v =~ s/(\.\d\d\d)000$/$1/;
551	$v =~ s/_.+$//;
552	if ( ref($v) ) {
553		# Numify
554		$v = $v + 0;
555	}
556	return $v;
557}
558
559
560
561
562
563######################################################################
564# MYMETA Support
565
566sub WriteMyMeta {
567	die "WriteMyMeta has been deprecated";
568}
569
570sub write_mymeta_yaml {
571	my $self = shift;
572
573	# We need YAML::Tiny to write the MYMETA.yml file
574	unless ( eval { require YAML::Tiny; 1; } ) {
575		return 1;
576	}
577
578	# Generate the data
579	my $meta = $self->_write_mymeta_data or return 1;
580
581	# Save as the MYMETA.yml file
582	print "Writing MYMETA.yml\n";
583	YAML::Tiny::DumpFile('MYMETA.yml', $meta);
584}
585
586sub write_mymeta_json {
587	my $self = shift;
588
589	# We need JSON to write the MYMETA.json file
590	unless ( eval { require JSON; 1; } ) {
591		return 1;
592	}
593
594	# Generate the data
595	my $meta = $self->_write_mymeta_data or return 1;
596
597	# Save as the MYMETA.yml file
598	print "Writing MYMETA.json\n";
599	Module::Install::_write(
600		'MYMETA.json',
601		JSON->new->pretty(1)->canonical->encode($meta),
602	);
603}
604
605sub _write_mymeta_data {
606	my $self = shift;
607
608	# If there's no existing META.yml there is nothing we can do
609	return undef unless -f 'META.yml';
610
611	# We need Parse::CPAN::Meta to load the file
612	unless ( eval { require Parse::CPAN::Meta; 1; } ) {
613		return undef;
614	}
615
616	# Merge the perl version into the dependencies
617	my $val  = $self->Meta->{values};
618	my $perl = delete $val->{perl_version};
619	if ( $perl ) {
620		$val->{requires} ||= [];
621		my $requires = $val->{requires};
622
623		# Canonize to three-dot version after Perl 5.6
624		if ( $perl >= 5.006 ) {
625			$perl =~ s{^(\d+)\.(\d\d\d)(\d*)}{join('.', $1, int($2||0), int($3||0))}e
626		}
627		unshift @$requires, [ perl => $perl ];
628	}
629
630	# Load the advisory META.yml file
631	my @yaml = Parse::CPAN::Meta::LoadFile('META.yml');
632	my $meta = $yaml[0];
633
634	# Overwrite the non-configure dependency hashs
635	delete $meta->{requires};
636	delete $meta->{build_requires};
637	delete $meta->{recommends};
638	if ( exists $val->{requires} ) {
639		$meta->{requires} = { map { @$_ } @{ $val->{requires} } };
640	}
641	if ( exists $val->{build_requires} ) {
642		$meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };
643	}
644
645	return $meta;
646}
647
6481;
649