File Coverage

blib/lib/Dist/Zilla/Role/ModuleMetadata.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 10 50.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 49 54 90.7


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