File Coverage

blib/lib/Dist/Zilla/Role/ModuleMetadata.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 8 50.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 46 50 92.0


line stmt bran cond sub pod time code
1 3     3   1142305 use strict;
  3         6  
  3         90  
2 3     3   12 use warnings;
  3         4  
  3         135  
3             package Dist::Zilla::Role::ModuleMetadata; # git description: v0.002-6-g48c90d4
4             # ABSTRACT: a role for plugins that use Module::Metadata
5             # KEYWORDS: zilla distribution plugin role metadata cache packages versions
6             # vim: set ts=8 sts=4 sw=4 tw=78 et :
7              
8             our $VERSION = '0.003';
9              
10 3     3   12 use Moose::Role;
  3         5  
  3         22  
11 3     3   11160 use Module::Metadata 1.000005;
  3         4094  
  3         64  
12 3     3   12 use Digest::MD5 'md5';
  3         6  
  3         151  
13 3     3   15 use namespace::autoclean;
  3         5  
  3         26  
14              
15             # filename => md5 content => MMD object
16             my %CACHE;
17              
18             sub module_metadata_for_file
19             {
20 5     5 1 654927 my ($self, $file) = @_;
21              
22             # handle dzil v4 files by assuming no (or latin1) encoding
23 5 50       52 my $encoded_content = $file->can('encoded_content') ? $file->encoded_content : $file->content;
24              
25             # We cache on the MD5 checksum to detect if the file has been modified
26             # by some other plugin since it was last parsed, making our object invalid.
27 5         1539 my $md5 = md5($encoded_content);
28 5 50       16 return $CACHE{$file->name}{$md5} if $CACHE{$file->name}{$md5};
29              
30 3 50   3   152 open(
  3 50   3   3  
  3         24  
  3         4277  
  3         6  
  3         27  
  5         304  
31             my $fh,
32             ($file->can('encoding') ? sprintf('<:encoding(%s)', $file->encoding) : '<'),
33             \$encoded_content,
34             ) or $self->log_fatal('cannot open handle to ' . $file->name . ' content: ' . $!);
35              
36 5         2537 $self->log_debug([ 'parsing %s for Module::Metadata', $file->name ]);
37 5         1701 my $mmd = Module::Metadata->new_from_handle($fh, $file->name);
38 5         2717 return ($CACHE{$file->name}{$md5} = $mmd);
39             }
40              
41             around dump_config => sub
42             {
43             my ($orig, $self) = @_;
44             my $config = $self->$orig;
45              
46             $config->{+__PACKAGE__} = {
47             'Module::Metadata' => Module::Metadata->VERSION,
48             version => __PACKAGE__->VERSION,
49             };
50              
51             return $config;
52             };
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Dist::Zilla::Role::ModuleMetadata - a role for plugins that use Module::Metadata
65              
66             =head1 VERSION
67              
68             version 0.003
69              
70             =head1 SYNOPSIS
71              
72             package Dist::Zilla::Plugin::YourNewPlugin;
73             use Moose;
74             with
75             'Dist::Zilla::Role::...',
76             'Dist::Zilla::Role::ModuleMetadata';
77             use namespace::autoclean;
78              
79             sub your_method {
80             my $self = shift;
81              
82             my $file = ...; # perhaps via the :InstallModules filefinder?
83             my $version = $self->module_metadata_for_file->($file)->version;
84             ...
85             }
86              
87             =head1 DESCRIPTION
88              
89             This L<role|Moose::Role> provides some common utilities for L<Dist::Zilla>
90             plugins which use L<Module::Metadata> and the information that it provides.
91              
92             =head1 METHODS PROVIDED
93              
94             =head2 C<module_metadata_for_file>
95              
96             my $mmd = $self->module_metadata_for_file($file);
97              
98             Given a dzil file object (anything that does L<Dist::Zilla::Role::File>), this
99             method returns a L<Module::Metadata> object for that file's content.
100              
101             =for stopwords reparsing
102              
103             Internally, this method caches these objects. If multiple plugins want an
104             object for the same file, this avoids reparsing it.
105              
106             =head1 SUPPORT
107              
108             =for stopwords irc
109              
110             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Role-ModuleMetadata>
111             (or L<bug-Dist-Zilla-Role-ModuleMetadata@rt.cpan.org|mailto:bug-Dist-Zilla-Role-ModuleMetadata@rt.cpan.org>).
112             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
113              
114             =head1 SEE ALSO
115              
116             =over 4
117              
118             =item *
119              
120             L<Module::Metadata>
121              
122             =back
123              
124             =head1 AUTHOR
125              
126             Karen Etheridge <ether@cpan.org>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2015 by Karen Etheridge.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut