PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Package-Generator-0.103/inc/Module/Install/Metadata.pm

#
Perl | 580 lines | 479 code | 71 blank | 30 comment | 37 complexity | 4fc7d8e2579a8498f336b9863df0f7bf MD5 | raw file
Possible License(s): AGPL-1.0
  1. #line 1
  2. package Module::Install::Metadata;
  3. use strict 'vars';
  4. use Module::Install::Base;
  5. use vars qw{$VERSION @ISA $ISCORE};
  6. BEGIN {
  7. $VERSION = '0.88';
  8. @ISA = qw{Module::Install::Base};
  9. $ISCORE = 1;
  10. }
  11. my @boolean_keys = qw{
  12. sign
  13. mymeta
  14. };
  15. my @scalar_keys = qw{
  16. name
  17. module_name
  18. abstract
  19. author
  20. version
  21. distribution_type
  22. tests
  23. installdirs
  24. };
  25. my @tuple_keys = qw{
  26. configure_requires
  27. build_requires
  28. requires
  29. recommends
  30. bundles
  31. resources
  32. };
  33. my @resource_keys = qw{
  34. homepage
  35. bugtracker
  36. repository
  37. };
  38. my @array_keys = qw{
  39. keywords
  40. };
  41. sub Meta { shift }
  42. sub Meta_BooleanKeys { @boolean_keys }
  43. sub Meta_ScalarKeys { @scalar_keys }
  44. sub Meta_TupleKeys { @tuple_keys }
  45. sub Meta_ResourceKeys { @resource_keys }
  46. sub Meta_ArrayKeys { @array_keys }
  47. foreach my $key ( @boolean_keys ) {
  48. *$key = sub {
  49. my $self = shift;
  50. if ( defined wantarray and not @_ ) {
  51. return $self->{values}->{$key};
  52. }
  53. $self->{values}->{$key} = ( @_ ? $_[0] : 1 );
  54. return $self;
  55. };
  56. }
  57. foreach my $key ( @scalar_keys ) {
  58. *$key = sub {
  59. my $self = shift;
  60. return $self->{values}->{$key} if defined wantarray and !@_;
  61. $self->{values}->{$key} = shift;
  62. return $self;
  63. };
  64. }
  65. foreach my $key ( @array_keys ) {
  66. *$key = sub {
  67. my $self = shift;
  68. return $self->{values}->{$key} if defined wantarray and !@_;
  69. $self->{values}->{$key} ||= [];
  70. push @{$self->{values}->{$key}}, @_;
  71. return $self;
  72. };
  73. }
  74. foreach my $key ( @resource_keys ) {
  75. *$key = sub {
  76. my $self = shift;
  77. unless ( @_ ) {
  78. return () unless $self->{values}->{resources};
  79. return map { $_->[1] }
  80. grep { $_->[0] eq $key }
  81. @{ $self->{values}->{resources} };
  82. }
  83. return $self->{values}->{resources}->{$key} unless @_;
  84. my $uri = shift or die(
  85. "Did not provide a value to $key()"
  86. );
  87. $self->resources( $key => $uri );
  88. return 1;
  89. };
  90. }
  91. foreach my $key ( grep { $_ ne "resources" } @tuple_keys) {
  92. *$key = sub {
  93. my $self = shift;
  94. return $self->{values}->{$key} unless @_;
  95. my @added;
  96. while ( @_ ) {
  97. my $module = shift or last;
  98. my $version = shift || 0;
  99. push @added, [ $module, $version ];
  100. }
  101. push @{ $self->{values}->{$key} }, @added;
  102. return map {@$_} @added;
  103. };
  104. }
  105. # Resource handling
  106. my %lc_resource = map { $_ => 1 } qw{
  107. homepage
  108. license
  109. bugtracker
  110. repository
  111. };
  112. sub resources {
  113. my $self = shift;
  114. while ( @_ ) {
  115. my $name = shift or last;
  116. my $value = shift or next;
  117. if ( $name eq lc $name and ! $lc_resource{$name} ) {
  118. die("Unsupported reserved lowercase resource '$name'");
  119. }
  120. $self->{values}->{resources} ||= [];
  121. push @{ $self->{values}->{resources} }, [ $name, $value ];
  122. }
  123. $self->{values}->{resources};
  124. }
  125. # Aliases for build_requires that will have alternative
  126. # meanings in some future version of META.yml.
  127. sub test_requires { shift->build_requires(@_) }
  128. sub install_requires { shift->build_requires(@_) }
  129. # Aliases for installdirs options
  130. sub install_as_core { $_[0]->installdirs('perl') }
  131. sub install_as_cpan { $_[0]->installdirs('site') }
  132. sub install_as_site { $_[0]->installdirs('site') }
  133. sub install_as_vendor { $_[0]->installdirs('vendor') }
  134. sub dynamic_config {
  135. my $self = shift;
  136. unless ( @_ ) {
  137. warn "You MUST provide an explicit true/false value to dynamic_config\n";
  138. return $self;
  139. }
  140. $self->{values}->{dynamic_config} = $_[0] ? 1 : 0;
  141. return 1;
  142. }
  143. sub perl_version {
  144. my $self = shift;
  145. return $self->{values}->{perl_version} unless @_;
  146. my $version = shift or die(
  147. "Did not provide a value to perl_version()"
  148. );
  149. # Normalize the version
  150. $version = $self->_perl_version($version);
  151. # We don't support the reall old versions
  152. unless ( $version >= 5.005 ) {
  153. die "Module::Install only supports 5.005 or newer (use ExtUtils::MakeMaker)\n";
  154. }
  155. $self->{values}->{perl_version} = $version;
  156. }
  157. #Stolen from M::B
  158. my %license_urls = (
  159. perl => 'http://dev.perl.org/licenses/',
  160. apache => 'http://apache.org/licenses/LICENSE-2.0',
  161. artistic => 'http://opensource.org/licenses/artistic-license.php',
  162. artistic_2 => 'http://opensource.org/licenses/artistic-license-2.0.php',
  163. lgpl => 'http://opensource.org/licenses/lgpl-license.php',
  164. lgpl2 => 'http://opensource.org/licenses/lgpl-2.1.php',
  165. lgpl3 => 'http://opensource.org/licenses/lgpl-3.0.html',
  166. bsd => 'http://opensource.org/licenses/bsd-license.php',
  167. gpl => 'http://opensource.org/licenses/gpl-license.php',
  168. gpl2 => 'http://opensource.org/licenses/gpl-2.0.php',
  169. gpl3 => 'http://opensource.org/licenses/gpl-3.0.html',
  170. mit => 'http://opensource.org/licenses/mit-license.php',
  171. mozilla => 'http://opensource.org/licenses/mozilla1.1.php',
  172. open_source => undef,
  173. unrestricted => undef,
  174. restrictive => undef,
  175. unknown => undef,
  176. );
  177. sub license {
  178. my $self = shift;
  179. return $self->{values}->{license} unless @_;
  180. my $license = shift or die(
  181. 'Did not provide a value to license()'
  182. );
  183. $self->{values}->{license} = $license;
  184. # Automatically fill in license URLs
  185. if ( $license_urls{$license} ) {
  186. $self->resources( license => $license_urls{$license} );
  187. }
  188. return 1;
  189. }
  190. sub all_from {
  191. my ( $self, $file ) = @_;
  192. unless ( defined($file) ) {
  193. my $name = $self->name or die(
  194. "all_from called with no args without setting name() first"
  195. );
  196. $file = join('/', 'lib', split(/-/, $name)) . '.pm';
  197. $file =~ s{.*/}{} unless -e $file;
  198. unless ( -e $file ) {
  199. die("all_from cannot find $file from $name");
  200. }
  201. }
  202. unless ( -f $file ) {
  203. die("The path '$file' does not exist, or is not a file");
  204. }
  205. # Some methods pull from POD instead of code.
  206. # If there is a matching .pod, use that instead
  207. my $pod = $file;
  208. $pod =~ s/\.pm$/.pod/i;
  209. $pod = $file unless -e $pod;
  210. # Pull the different values
  211. $self->name_from($file) unless $self->name;
  212. $self->version_from($file) unless $self->version;
  213. $self->perl_version_from($file) unless $self->perl_version;
  214. $self->author_from($pod) unless $self->author;
  215. $self->license_from($pod) unless $self->license;
  216. $self->abstract_from($pod) unless $self->abstract;
  217. return 1;
  218. }
  219. sub provides {
  220. my $self = shift;
  221. my $provides = ( $self->{values}->{provides} ||= {} );
  222. %$provides = (%$provides, @_) if @_;
  223. return $provides;
  224. }
  225. sub auto_provides {
  226. my $self = shift;
  227. return $self unless $self->is_admin;
  228. unless (-e 'MANIFEST') {
  229. warn "Cannot deduce auto_provides without a MANIFEST, skipping\n";
  230. return $self;
  231. }
  232. # Avoid spurious warnings as we are not checking manifest here.
  233. local $SIG{__WARN__} = sub {1};
  234. require ExtUtils::Manifest;
  235. local *ExtUtils::Manifest::manicheck = sub { return };
  236. require Module::Build;
  237. my $build = Module::Build->new(
  238. dist_name => $self->name,
  239. dist_version => $self->version,
  240. license => $self->license,
  241. );
  242. $self->provides( %{ $build->find_dist_packages || {} } );
  243. }
  244. sub feature {
  245. my $self = shift;
  246. my $name = shift;
  247. my $features = ( $self->{values}->{features} ||= [] );
  248. my $mods;
  249. if ( @_ == 1 and ref( $_[0] ) ) {
  250. # The user used ->feature like ->features by passing in the second
  251. # argument as a reference. Accomodate for that.
  252. $mods = $_[0];
  253. } else {
  254. $mods = \@_;
  255. }
  256. my $count = 0;
  257. push @$features, (
  258. $name => [
  259. map {
  260. ref($_) ? ( ref($_) eq 'HASH' ) ? %$_ : @$_ : $_
  261. } @$mods
  262. ]
  263. );
  264. return @$features;
  265. }
  266. sub features {
  267. my $self = shift;
  268. while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) {
  269. $self->feature( $name, @$mods );
  270. }
  271. return $self->{values}->{features}
  272. ? @{ $self->{values}->{features} }
  273. : ();
  274. }
  275. sub no_index {
  276. my $self = shift;
  277. my $type = shift;
  278. push @{ $self->{values}->{no_index}->{$type} }, @_ if $type;
  279. return $self->{values}->{no_index};
  280. }
  281. sub read {
  282. my $self = shift;
  283. $self->include_deps( 'YAML::Tiny', 0 );
  284. require YAML::Tiny;
  285. my $data = YAML::Tiny::LoadFile('META.yml');
  286. # Call methods explicitly in case user has already set some values.
  287. while ( my ( $key, $value ) = each %$data ) {
  288. next unless $self->can($key);
  289. if ( ref $value eq 'HASH' ) {
  290. while ( my ( $module, $version ) = each %$value ) {
  291. $self->can($key)->($self, $module => $version );
  292. }
  293. } else {
  294. $self->can($key)->($self, $value);
  295. }
  296. }
  297. return $self;
  298. }
  299. sub write {
  300. my $self = shift;
  301. return $self unless $self->is_admin;
  302. $self->admin->write_meta;
  303. return $self;
  304. }
  305. sub version_from {
  306. require ExtUtils::MM_Unix;
  307. my ( $self, $file ) = @_;
  308. $self->version( ExtUtils::MM_Unix->parse_version($file) );
  309. }
  310. sub abstract_from {
  311. require ExtUtils::MM_Unix;
  312. my ( $self, $file ) = @_;
  313. $self->abstract(
  314. bless(
  315. { DISTNAME => $self->name },
  316. 'ExtUtils::MM_Unix'
  317. )->parse_abstract($file)
  318. );
  319. }
  320. # Add both distribution and module name
  321. sub name_from {
  322. my ($self, $file) = @_;
  323. if (
  324. Module::Install::_read($file) =~ m/
  325. ^ \s*
  326. package \s*
  327. ([\w:]+)
  328. \s* ;
  329. /ixms
  330. ) {
  331. my ($name, $module_name) = ($1, $1);
  332. $name =~ s{::}{-}g;
  333. $self->name($name);
  334. unless ( $self->module_name ) {
  335. $self->module_name($module_name);
  336. }
  337. } else {
  338. die("Cannot determine name from $file\n");
  339. }
  340. }
  341. sub perl_version_from {
  342. my $self = shift;
  343. if (
  344. Module::Install::_read($_[0]) =~ m/
  345. ^
  346. (?:use|require) \s*
  347. v?
  348. ([\d_\.]+)
  349. \s* ;
  350. /ixms
  351. ) {
  352. my $perl_version = $1;
  353. $perl_version =~ s{_}{}g;
  354. $self->perl_version($perl_version);
  355. } else {
  356. warn "Cannot determine perl version info from $_[0]\n";
  357. return;
  358. }
  359. }
  360. sub author_from {
  361. my $self = shift;
  362. my $content = Module::Install::_read($_[0]);
  363. if ($content =~ m/
  364. =head \d \s+ (?:authors?)\b \s*
  365. ([^\n]*)
  366. |
  367. =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
  368. .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
  369. ([^\n]*)
  370. /ixms) {
  371. my $author = $1 || $2;
  372. $author =~ s{E<lt>}{<}g;
  373. $author =~ s{E<gt>}{>}g;
  374. $self->author($author);
  375. } else {
  376. warn "Cannot determine author info from $_[0]\n";
  377. }
  378. }
  379. sub license_from {
  380. my $self = shift;
  381. if (
  382. Module::Install::_read($_[0]) =~ m/
  383. (
  384. =head \d \s+
  385. (?:licen[cs]e|licensing|copyright|legal)\b
  386. .*?
  387. )
  388. (=head\\d.*|=cut.*|)
  389. \z
  390. /ixms ) {
  391. my $license_text = $1;
  392. my @phrases = (
  393. 'under the same (?:terms|license) as perl itself' => 'perl', 1,
  394. 'GNU general public license' => 'gpl', 1,
  395. 'GNU public license' => 'gpl', 1,
  396. 'GNU lesser general public license' => 'lgpl', 1,
  397. 'GNU lesser public license' => 'lgpl', 1,
  398. 'GNU library general public license' => 'lgpl', 1,
  399. 'GNU library public license' => 'lgpl', 1,
  400. 'BSD license' => 'bsd', 1,
  401. 'Artistic license' => 'artistic', 1,
  402. 'GPL' => 'gpl', 1,
  403. 'LGPL' => 'lgpl', 1,
  404. 'BSD' => 'bsd', 1,
  405. 'Artistic' => 'artistic', 1,
  406. 'MIT' => 'mit', 1,
  407. 'proprietary' => 'proprietary', 0,
  408. );
  409. while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
  410. $pattern =~ s{\s+}{\\s+}g;
  411. if ( $license_text =~ /\b$pattern\b/i ) {
  412. $self->license($license);
  413. return 1;
  414. }
  415. }
  416. }
  417. warn "Cannot determine license info from $_[0]\n";
  418. return 'unknown';
  419. }
  420. sub _extract_bugtracker {
  421. my @links = $_[0] =~ m#L<(\Qhttp://rt.cpan.org/\E[^>]+)>#g;
  422. my %links;
  423. @links{@links}=();
  424. @links=keys %links;
  425. return @links;
  426. }
  427. sub bugtracker_from {
  428. my $self = shift;
  429. my $content = Module::Install::_read($_[0]);
  430. my @links = _extract_bugtracker($content);
  431. unless ( @links ) {
  432. warn "Cannot determine bugtracker info from $_[0]\n";
  433. return 0;
  434. }
  435. if ( @links > 1 ) {
  436. warn "Found more than on rt.cpan.org link in $_[0]\n";
  437. return 0;
  438. }
  439. # Set the bugtracker
  440. bugtracker( $links[0] );
  441. return 1;
  442. }
  443. sub requires_from {
  444. my $self = shift;
  445. my $content = Module::Install::_readperl($_[0]);
  446. my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
  447. while ( @requires ) {
  448. my $module = shift @requires;
  449. my $version = shift @requires;
  450. $self->requires( $module => $version );
  451. }
  452. }
  453. # Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
  454. # numbers (eg, 5.006001 or 5.008009).
  455. # Also, convert double-part versions (eg, 5.8)
  456. sub _perl_version {
  457. my $v = $_[-1];
  458. $v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
  459. $v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
  460. $v =~ s/(\.\d\d\d)000$/$1/;
  461. $v =~ s/_.+$//;
  462. if ( ref($v) ) {
  463. $v = $v + 0; # Numify
  464. }
  465. return $v;
  466. }
  467. ######################################################################
  468. # MYMETA.yml Support
  469. sub WriteMyMeta {
  470. die "WriteMyMeta has been deprecated";
  471. }
  472. sub write_mymeta {
  473. my $self = shift;
  474. # If there's no existing META.yml there is nothing we can do
  475. return unless -f 'META.yml';
  476. # We need YAML::Tiny to write the MYMETA.yml file
  477. unless ( eval { require YAML::Tiny; 1; } ) {
  478. return 1;
  479. }
  480. # Merge the perl version into the dependencies
  481. my $val = $self->Meta->{values};
  482. my $perl = delete $val->{perl_version};
  483. if ( $perl ) {
  484. $val->{requires} ||= [];
  485. my $requires = $val->{requires};
  486. # Canonize to three-dot version after Perl 5.6
  487. if ( $perl >= 5.006 ) {
  488. $perl =~ s{^(\d+)\.(\d\d\d)(\d*)}{join('.', $1, int($2||0), int($3||0))}e
  489. }
  490. unshift @$requires, [ perl => $perl ];
  491. }
  492. # Load the advisory META.yml file
  493. my @yaml = YAML::Tiny::LoadFile('META.yml');
  494. my $meta = $yaml[0];
  495. # Overwrite the non-configure dependency hashs
  496. delete $meta->{requires};
  497. delete $meta->{build_requires};
  498. delete $meta->{recommends};
  499. if ( exists $val->{requires} ) {
  500. $meta->{requires} = { map { @$_ } @{ $val->{requires} } };
  501. }
  502. if ( exists $val->{build_requires} ) {
  503. $meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };
  504. }
  505. # Save as the MYMETA.yml file
  506. print "Writing MYMETA.yml\n";
  507. YAML::Tiny::DumpFile('MYMETA.yml', $meta);
  508. }
  509. 1;