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.91';
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|copyright|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			'GNU general public license'         => 'gpl',         1,
454			'GNU public license'                 => 'gpl',         1,
455			'GNU lesser general public license'  => 'lgpl',        1,
456			'GNU lesser public license'          => 'lgpl',        1,
457			'GNU library general public license' => 'lgpl',        1,
458			'GNU library public license'         => 'lgpl',        1,
459			'BSD license'                        => 'bsd',         1,
460			'Artistic license'                   => 'artistic',    1,
461			'GPL'                                => 'gpl',         1,
462			'LGPL'                               => 'lgpl',        1,
463			'BSD'                                => 'bsd',         1,
464			'Artistic'                           => 'artistic',    1,
465			'MIT'                                => 'mit',         1,
466			'proprietary'                        => 'proprietary', 0,
467		);
468		while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
469			$pattern =~ s#\s+#\\s+#g;
470			if ( $license_text =~ /\b$pattern\b/i ) {
471			        return $license;
472			}
473		}
474	} else {
475	        return;
476	}
477}
478
479sub license_from {
480	my $self = shift;
481	if (my $license=_extract_license(Module::Install::_read($_[0]))) {
482		$self->license($license);
483	} else {
484		warn "Cannot determine license info from $_[0]\n";
485		return 'unknown';
486	}
487}
488
489sub _extract_bugtracker {
490	my @links   = $_[0] =~ m#L<(\Qhttp://rt.cpan.org/\E[^>]+|\Qhttp://github.com/\E[\w_]+/[\w_]+/issues)>#g;
491	my %links;
492	@links{@links}=();
493	@links=keys %links;
494	return @links;
495}
496
497sub bugtracker_from {
498	my $self    = shift;
499	my $content = Module::Install::_read($_[0]);
500	my @links   = _extract_bugtracker($content);
501	unless ( @links ) {
502		warn "Cannot determine bugtracker info from $_[0]\n";
503		return 0;
504	}
505	if ( @links > 1 ) {
506		warn "Found more than one bugtracker link in $_[0]\n";
507		return 0;
508	}
509
510	# Set the bugtracker
511	bugtracker( $links[0] );
512	return 1;
513}
514
515sub requires_from {
516	my $self     = shift;
517	my $content  = Module::Install::_readperl($_[0]);
518	my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
519	while ( @requires ) {
520		my $module  = shift @requires;
521		my $version = shift @requires;
522		$self->requires( $module => $version );
523	}
524}
525
526sub test_requires_from {
527	my $self     = shift;
528	my $content  = Module::Install::_readperl($_[0]);
529	my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
530	while ( @requires ) {
531		my $module  = shift @requires;
532		my $version = shift @requires;
533		$self->test_requires( $module => $version );
534	}
535}
536
537# Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
538# numbers (eg, 5.006001 or 5.008009).
539# Also, convert double-part versions (eg, 5.8)
540sub _perl_version {
541	my $v = $_[-1];
542	$v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
543	$v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
544	$v =~ s/(\.\d\d\d)000$/$1/;
545	$v =~ s/_.+$//;
546	if ( ref($v) ) {
547		# Numify
548		$v = $v + 0;
549	}
550	return $v;
551}
552
553
554
555
556
557######################################################################
558# MYMETA Support
559
560sub WriteMyMeta {
561	die "WriteMyMeta has been deprecated";
562}
563
564sub write_mymeta_yaml {
565	my $self = shift;
566
567	# We need YAML::Tiny to write the MYMETA.yml file
568	unless ( eval { require YAML::Tiny; 1; } ) {
569		return 1;
570	}
571
572	# Generate the data
573	my $meta = $self->_write_mymeta_data or return 1;
574
575	# Save as the MYMETA.yml file
576	print "Writing MYMETA.yml\n";
577	YAML::Tiny::DumpFile('MYMETA.yml', $meta);
578}
579
580sub write_mymeta_json {
581	my $self = shift;
582
583	# We need JSON to write the MYMETA.json file
584	unless ( eval { require JSON; 1; } ) {
585		return 1;
586	}
587
588	# Generate the data
589	my $meta = $self->_write_mymeta_data or return 1;
590
591	# Save as the MYMETA.yml file
592	print "Writing MYMETA.json\n";
593	Module::Install::_write(
594		'MYMETA.json',
595		JSON->new->pretty(1)->canonical->encode($meta),
596	);
597}
598
599sub _write_mymeta_data {
600	my $self = shift;
601
602	# If there's no existing META.yml there is nothing we can do
603	return undef unless -f 'META.yml';
604
605	# We need Parse::CPAN::Meta to load the file
606	unless ( eval { require Parse::CPAN::Meta; 1; } ) {
607		return undef;
608	}
609
610	# Merge the perl version into the dependencies
611	my $val  = $self->Meta->{values};
612	my $perl = delete $val->{perl_version};
613	if ( $perl ) {
614		$val->{requires} ||= [];
615		my $requires = $val->{requires};
616
617		# Canonize to three-dot version after Perl 5.6
618		if ( $perl >= 5.006 ) {
619			$perl =~ s{^(\d+)\.(\d\d\d)(\d*)}{join('.', $1, int($2||0), int($3||0))}e
620		}
621		unshift @$requires, [ perl => $perl ];
622	}
623
624	# Load the advisory META.yml file
625	my @yaml = Parse::CPAN::Meta::LoadFile('META.yml');
626	my $meta = $yaml[0];
627
628	# Overwrite the non-configure dependency hashs
629	delete $meta->{requires};
630	delete $meta->{build_requires};
631	delete $meta->{recommends};
632	if ( exists $val->{requires} ) {
633		$meta->{requires} = { map { @$_ } @{ $val->{requires} } };
634	}
635	if ( exists $val->{build_requires} ) {
636		$meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };
637	}
638
639	return $meta;
640}
641
6421;
643