Debian Codegraph @

inc/ExtUtils/MY_Metafile.pm

raw
1 ## ----------------------------------------------------------------------------
2 # ExtUtils::MY_Metafile
3 # -----------------------------------------------------------------------------
4 # Mastering programmed by YAMASHINA Hio
5 #
6 # Copyright 2006-2008 YAMASHINA Hio
7 # -----------------------------------------------------------------------------
8 # $Id: MY_Metafile.pm 5362 2008-01-30 05:09:42Z hio $
9 # -----------------------------------------------------------------------------
10 package ExtUtils::MY_Metafile;
11 use strict;
12 #use warnings; # warnings pragma was first released with perl 5.006.
13 use ExtUtils::MakeMaker;
14
15 use vars qw($VERSION @EXPORT);
16
17 $VERSION = '0.09';
18 @EXPORT = qw(my_metafile);
19
20 use vars qw(%META_PARAMS); # DISTNAME(pkgname)=>HASHREF.
21
22 use vars qw($DEFAULT_META_SPEC_VERSION);
23 $DEFAULT_META_SPEC_VERSION = '1.3';
24
25 1;
26
27 # -----------------------------------------------------------------------------
28 # for: use inc::ExtUtils::MY_Metafile;
29 #
30 sub inc::ExtUtils::MY_Metafile::import
31 {
32 my $pkg = 'ExtUtils::MY_Metafile';
33 push(@inc::ExtUtils::MY_Metafile::ISA, $pkg);
34 goto &import;
35 }
36
37 # -----------------------------------------------------------------------------
38 # import.
39 #
40 sub import
41 {
42 my $pkg = shift;
43 my @syms = (!@_ || grep{/^:all$/}@_) ? @EXPORT : @_;
44 my $callerpkg = caller;
45
46 foreach my $name (@syms)
47 {
48 my $sub = $pkg->can($name);
49 $sub or next;
50 no strict 'refs';
51 *{$callerpkg.'::'.$name} = $sub;
52 }
53 if( !grep{ /^:no_setup$/ } @_ )
54 {
55 # override.
56 *MM::metafile_target = \&_mm_metafile;
57 }
58 }
59
60 # -----------------------------------------------------------------------------
61 # _diag_version();
62 #
63 sub _diag_version
64 {
65 my $mmver = $ExtUtils::MakeMaker::VERSION;
66 my $mmvernum = $mmver;
67 if( $mmvernum =~ /^(\d+)\.(\d+)_(\d+)\z/ )
68 {
69 $mmvernum = "$1.$2$3";
70 $mmver .= "=$mmvernum";
71 }
72 if( $mmvernum >= 6.30 )
73 {
74 print STDERR "# ExtUtils::MY_Metafile for MM 6.30 or later ($mmver).\n";
75 }else
76 {
77 print STDERR "# ExtUtils::MY_Metafile for MM 6.25 or earlier ($mmver).\n";
78 }
79 }
80
81 # -----------------------------------------------------------------------------
82 # my_metafile($distname => $param);
83 # my_metafile($param);
84 #
85 sub my_metafile
86 {
87 my $distname = @_>=2 && shift;
88 my $param = shift;
89 UNIVERSAL::isa($distname,'HASH') and $distname = $distname->{DISTNAME};
90 $distname ||= '';
91 $distname =~ s/::/-/g;
92 $META_PARAMS{$distname} and warn "# overwrite previous meta config $distname.\n";
93 $META_PARAMS{$distname} = $param;
94 }
95
96 # -----------------------------------------------------------------------------
97 # _mm_metafile($MM)
98 # altanative of MM::metafile_target.
99 # takes $MM object and returns makefile text.
100 #
101 sub _mm_metafile
102 {
103 my $this = shift;
104
105 if( $this->{NO_META} )
106 {
107 return
108 "metafile:\n" .
109 "\t\$(NOECHO) \$(NOOP)\n";
110 }
111
112 # generate META.yml text.
113 #
114 my $meta = _gen_meta_yml($this);
115 my @write_meta = (
116 '$(NOECHO) $(ECHO) Generating META.yml',
117 $this->echo($meta, 'META_new.yml'),
118 );
119
120 # format as makefile text.
121 #
122 my ($make_target, $metaout_file);
123 my $mmvernum = $ExtUtils::MakeMaker::VERSION;
124 if( $mmvernum =~ /^(\d+)\.(\d+)_(\d+)\z/ )
125 {
126 $mmvernum = "$1.$2$3";
127 }
128 if( $mmvernum >= 6.30 )
129 {
130 $make_target = "# for MM 6.30 or later.\n";
131 $make_target .= "metafile : create_distdir\n";
132 $metaout_file = '$(DISTVNAME)/META.yml';
133 }else
134 {
135 $make_target = "# for MM 6.25 or earlier.\n";
136 $make_target .= "metafile :\n";
137 $metaout_file = 'META.yml',
138 }
139
140 my $rename_meta = "-\$(NOECHO) \$(MV) META_new.yml $metaout_file";
141 my $make_body = join('', map{"\t$_\n"} @write_meta, $rename_meta);
142 "$make_target$make_body";
143 }
144
145 # -----------------------------------------------------------------------------
146 # _gen_meta_yml($MM);
147 # generate META.yml text.
148 #
149 sub _gen_meta_yml
150 {
151 # from MakeMaker-6.30.
152 my $this = shift;
153 my $param = shift;
154 my $check_meta_spec = 1;
155 if( !$param )
156 {
157 $param = $META_PARAMS{$this->{DISTNAME}} || $META_PARAMS{''};
158 if( !$param )
159 {
160 $param = {};
161 $check_meta_spec = 0;
162 }
163 }
164 if( $META_PARAMS{':all'} )
165 {
166 # special key.
167 $param = { %{$META_PARAMS{':all'}}, %$param };
168 }
169
170 # meta_spec and meta_spec_version.
171 my $meta_spec = $param->{meta_spec} || $param->{'meta-spec'};
172 if($param->{meta_spec} && $param->{'meta-spec'} )
173 {
174 warn "both meta_spec and meta-spec exist.\n";
175 }
176 $meta_spec &&= {%$meta_spec}; # sharrow-copy.
177 $meta_spec ||= {};
178 if( exists($param->{meta_spec_version}) && exists($meta_spec->{version}) )
179 {
180 warn "both meta_spec_vesrion and meta_spec.version exist.\n";
181 }
182 $meta_spec->{version} ||= $param->{meta_spec_version} || $DEFAULT_META_SPEC_VERSION;
183 $meta_spec->{url} ||= "http://module-build.sourceforge.net/META-spec-v$meta_spec->{version}.html";
184
185 # requires:, build_requires:
186 my $requires_to_yaml = sub{
187 my $key = shift;
188 my $hash = shift;
189 my $yaml = '';
190 my ($maxkeylen) = sort{$b<=>$a} map{length($_)} keys %$hash;
191 my ($maxvallen) = sort{$b<=>$a} map{length($_)} values %$hash;
192 foreach my $name ( sort { lc $a cmp lc $b } keys %$hash )
193 {
194 my $ver = $hash->{$name};
195 $yaml .= sprintf " %-*s %*s\n", $maxkeylen+1, "$name:", $maxvallen, $ver;
196 }
197 chomp $yaml;
198 $yaml ? "$key:\n$yaml" : '';
199 };
200 my $requires = $requires_to_yaml->(requires => $param->{requires} || $this->{PREREQ_PM});
201 my $build_requires = $requires_to_yaml->(build_requires => $param->{build_requires});
202
203 # no_index:
204 my $no_index = $param->{no_index};
205 if( !$no_index || !$no_index->{directory} )
206 {
207 my @dirs = grep{-d $_} (qw(
208 inc t
209 ex eg example examples sample samples demo demos
210 ));
211 $no_index = @dirs && +{ directory => \@dirs };
212 }
213 $no_index = $no_index ? _yaml_out({no_index=>$no_index}) : '';
214 chomp $no_index;
215 if( $param->{no_index} && !$ENV{NO_NO_INDEX_CHECK} )
216 {
217 my $warned;
218 foreach my $key (keys %{$param->{no_index}})
219 {
220 # dir is in spec-v1.2, directory is from spec-v1.3.
221 if( $key eq 'dir' && $meta_spec->{version}>=1.3 )
222 {
223 $warned ||= print STDERR "\n";
224 warn "$key should be `directory' in META-spec-v1.3 and later.\n";
225 next;
226 }
227 $key =~ /^(file|dir|directory|package|namespace)$/ and next;
228 $warned ||= print STDERR "\n";
229 warn "$key is invalid field for no_index.\n";
230 }
231 $warned and print STDERR "\n";
232 }
233
234 # abstract is from file.
235 my $abstract = '';
236 if( $this->{ABSTRACT} )
237 {
238 $abstract = _yaml_out({abstract => $this->{ABSTRACT}});
239 }elsif( $this->{ABSTRACT_FROM} && open(my$fh, "< $this->{ABSTRACT_FROM}") )
240 {
241 while(<$fh>)
242 {
243 /^=head1 NAME$/ or next;
244 (my $pkg = $this->{DISTNAME}) =~ s/-/::/g;
245 while(<$fh>)
246 {
247 /^=/ and last;
248 /^(\Q$pkg\E\s+-+\s+)(.*)/ or next;
249 $abstract = $2;
250 last;
251 }
252 last;
253 }
254 $abstract = $abstract ? _yaml_out({abstract=>$abstract}) : '';
255 }
256 chomp $abstract;
257
258 # build yaml object as hash.
259 my $yaml = {}; # key=>"value as yaml-text".
260
261 # first, set from arguments for WriteMakefile().
262 $yaml->{name} = $this->{DISTNAME};
263 $yaml->{version} = $this->{VERSION};
264 $yaml->{version_from} = $this->{VERSION_FROM};
265 $yaml->{installdirs} = $this->{INSTALLDIRS};
266 $yaml->{author} = $this->{AUTHOR};
267 $yaml->{license} = $this->{LICENSE};
268 foreach my $key (keys %$yaml)
269 {
270 if( $yaml->{$key} )
271 {
272 my $pad = ' 'x(12-length($key));
273 $yaml->{$key} = sprintf('%s:%s %s', $key, $pad, $yaml->{$key});
274 }
275 }
276 $yaml->{abstract} = $abstract;
277 $yaml->{no_index} = $no_index;
278 $yaml->{requires} = $requires;
279 $yaml->{build_requires} = $build_requires;
280
281 $yaml->{distribution_type} = 'distribution_type: module';
282 $yaml->{generated_by} = "generated_by: ExtUtils::MY_Metafile version $VERSION, EUMM-$ExtUtils::MakeMaker::VERSION.";
283
284 $yaml->{'meta-spec'} = "meta-spec:\n";
285 $yaml->{'meta-spec'} .= " version: ".delete($meta_spec->{version})."\n";
286 $yaml->{'meta-spec'} .= " url: ".delete($meta_spec->{url})."\n";
287
288 # next, set from arguments for my_metafile().
289 my $extras = {};
290 foreach my $key (sort keys %$param)
291 {
292 grep{$key eq $_} qw(no_index requires build_requires meta_spec meta-spec meta_spec_version) and next;
293 my $line = _yaml_out->({$key=>$param->{$key}});
294 if( exists($yaml->{$key}) )
295 {
296 chomp $line;
297 $yaml->{$key} = $line;
298 }else
299 {
300 $extras->{$key} = $line;
301 }
302 }
303 $yaml->{extras} = join('', map{$extras->{$_}} sort keys %$extras);
304
305 # then, check required keys by yaml-spec.
306 my @required_keys = qw(meta-spec name version abstract author license generated_by);
307 foreach my $key (@required_keys)
308 {
309 $check_meta_spec or next;
310 my $ok = $yaml->{$key} && $yaml->{$key}=~/\w/;
311 $ok ||= $extras->{$key} and next;
312 warn "$key is required for meta-spec v1.2 ($this->{DISTNAME}).\n";
313 }
314
315 if( exists($param->{license}) && exists($this->{LICENSE}) && $param->{license} ne $this->{LICENSE} )
316 {
317 warn "WriteMakefile.LICENSE ($this->{LICENSE}) is different from my_metafile.license ($param->{license}).";
318 }
319
320 $yaml->{license} ||= 'license: unknown';
321 foreach my $key (keys %$yaml)
322 {
323 $key eq 'extras' and next;
324 $yaml->{$key} ||= "#$key:";
325 }
326 $yaml->{extras} &&= "\n# extras.\n$yaml->{extras}";
327
328 foreach my $key (qw(abstract license))
329 {
330 my $pad = ' 'x(12-length($key));
331 $yaml->{$key} =~ s/^$key: +(.+)\z/$key:$pad $1/;
332 }
333
334 # packing into singple text.
335 my $meta = <<YAML;
336 # http://module-build.sourceforge.net/META-spec.html
337 $yaml->{name}
338 $yaml->{version}
339 $yaml->{version_from}
340 $yaml->{installdirs}
341 $yaml->{author}
342 $yaml->{abstract}
343 $yaml->{license}
344 $yaml->{requires}
345 $yaml->{build_requires}
346 $yaml->{no_index}
347 $yaml->{extras}
348 $yaml->{distribution_type}
349 $yaml->{generated_by}
350 $yaml->{'meta-spec'}
351 YAML
352 #print "$meta";
353 $meta;
354 }
355
356 # -----------------------------------------------------------------------------
357 # generate simple yaml.
358 #
359 sub _yaml_out
360 {
361 my $obj = shift;
362
363 my $depth = shift || 0;
364 my $out = '';
365
366 if( !defined($obj) )
367 {
368 $out = " "x$depth."~\n";
369 }elsif( !ref($obj) )
370 {
371 $out = " "x$depth.$obj."\n";
372 }elsif( ref($obj)eq'ARRAY' )
373 {
374 my @e = map{_yaml_out->($_, $depth+1)} @$obj;
375 @e = map{ " "x$depth."- ".substr($_, ($depth+1)*2)} @e;
376 $out = join('', @e);
377 $out ||= " "x$depth."[]";
378 }elsif( ref($obj)eq'HASH' )
379 {
380 foreach my $k (sort keys %$obj)
381 {
382 $out .= " "x$depth."$k:";
383 $out .= ref($obj->{$k}) ? "\n"._yaml_out($obj->{$k}, $depth+1) : " $obj->{$k}\n";
384 }
385 $out ||= " "x$depth."{}";
386 }else
387 {
388 die "not supported: $obj";
389 }
390 $out;
391 }
392
393 # -----------------------------------------------------------------------------
394 # End of Code.
395 # -----------------------------------------------------------------------------
396
397 __END__
398
399 =encoding utf8
400
401 =for stopwords
402 YAMASHINA
403 Hio
404 ACKNOWLEDGEMENTS
405 AnnoCPAN
406 CPAN
407 EUMM
408 META.yml
409 RT
410
411 =head1 NAME
412
413 ExtUtils::MY_Metafile - META.yml customize with ExtUtil::MakeMaker
414
415
416 =head1 VERSION
417
418 Version 0.09
419
420
421 =head1 SYNOPSIS
422
423 put ExtUtils/MY_Metafile.pm into inc/ExtUtils/MY_Metafile.pm:
424
425
426 $ mkdir -p inc/ExtUtils
427 $ cp `perldoc -l ExtUtils::MY_Metafile` inc/ExtUtils/
428
429 and write in your Makefile.PL:
430
431
432 use ExtUtils::MakeMaker;
433 use inc::ExtUtils::MY_Metafile;
434
435 my_metafile {
436 no_index => {
437 directory => [ qw(inc example t), ],
438 },
439 license => 'perl',
440 };
441
442 WriteMakefile(
443 DISTNAME => 'Your::Module',
444 ...
445 );
446
447 =head1 EXPORT
448
449 This module exports one function.
450
451
452 =head1 FUNCTIONS
453
454 =head2 my_metafile $modname => \%meta_param;
455
456 Takes two arguments.
457 First one is package name to be generated, and you can omit this
458 argument. Second is hashref which contains META.yml contents.
459
460
461 my_metafile {
462 no_index => {
463 directory => [ qw(inc example t), ],
464 },
465 license => 'perl',
466 };
467
468 Some parameters are checked automatically.
469
470
471 =over
472
473 =item no_index
474
475 If you not specify C<directory> parameter for C<no_index> and
476 there are any directory of F<inc t ex eg example examples
477 sample samples demo demos>, they are set as it.
478
479
480 =item requires
481
482 C<requires> directive is set from C<PREREQ_PM> parameter
483 of EUMM. If you want to use C<build_requires>, you can write it.
484
485
486 =back
487
488 =head1 AUTHOR
489
490 YAMASHINA Hio, C<< <hio at cpan.org> >>
491
492
493 =head1 BUGS
494
495 Please report any bugs or feature requests to
496 C<bug-extutils-my_metafile at rt.cpan.org>, or through the web interface at
497 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ExtUtils-MY_Metafile>.
498 I will be notified, and then you'll automatically be notified of progress on
499 your bug as I make changes.
500
501
502 =head1 SUPPORT
503
504 You can find documentation for this module with the perldoc command.
505
506
507 perldoc ExtUtils::MY_Metafile
508
509 You can also look for information at:
510
511
512 =over 4
513
514 =item * AnnoCPAN: Annotated CPAN documentation
515
516 L<http://annocpan.org/dist/ExtUtils-MY_Metafile>
517
518
519 =item * CPAN Ratings
520
521 L<http://cpanratings.perl.org/d/ExtUtils-MY_Metafile>
522
523
524 =item * RT: CPAN's request tracker
525
526 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ExtUtils-MY_Metafile>
527
528
529 =item * Search CPAN
530
531 L<http://search.cpan.org/dist/ExtUtils-MY_Metafile>
532
533
534 =back
535
536 =head1 ACKNOWLEDGEMENTS
537
538 =head1 COPYRIGHT & LICENSE
539
540 Copyright 2006-2008 YAMASHINA Hio, all rights reserved.
541
542
543 This program is free software; you can redistribute it and/or modify it
544 under the same terms as Perl itself.
545
546 =cut
547
548 # -----------------------------------------------------------------------------
549 # End of File.
550 # -----------------------------------------------------------------------------