File Coverage

blib/lib/Dist/Zilla/Plugin/TrialVersionComment.pm
Criterion Covered Total %
statement 50 50 100.0
branch 15 20 75.0
condition 7 12 58.3
subroutine 8 8 100.0
pod 0 1 0.0
total 80 91 87.9


line stmt bran cond sub pod time code
1 7     7   13423784 use strict;
  7         12  
  7         218  
2 7     7   28 use warnings;
  7         12  
  7         402  
3             package Dist::Zilla::Plugin::TrialVersionComment; # git description: v0.006-2-gf7316ec
4             # ABSTRACT: Add a "# TRIAL" comment after your version declaration in trial releases
5             # KEYWORDS: plugin modules package version comment trial release
6             # vim: set ts=8 sts=4 sw=4 tw=115 et :
7              
8             our $VERSION = '0.007';
9              
10 7     7   29 use Moose;
  7         9  
  7         57  
11             with
12             'Dist::Zilla::Role::PPI',
13             'Dist::Zilla::Role::FileMunger',
14             'Dist::Zilla::Role::FileFinderUser' =>
15             { default_finders => [ ':InstallModules', ':ExecFiles' ] },
16             ;
17 7     7   30425 use Module::Runtime 'module_notional_filename';
  7         11  
  7         42  
18 7     7   3956 use PPI::Document;
  7         627279  
  7         248  
19 7     7   58 use namespace::autoclean;
  7         10  
  7         68  
20              
21             around dump_config => sub
22             {
23             my ($orig, $self) = @_;
24             my $config = $self->$orig;
25              
26             my $data = {
27             finder => $self->finder,
28             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
29             };
30             $config->{+__PACKAGE__} = $data if keys %$data;
31              
32             return $config;
33             };
34              
35             sub munge_files
36             {
37 7     7 0 329354 my $self = shift;
38              
39 7 100       206 $self->log_debug([ 'release_status is not trial; doing nothing' ]), return
40             if not $self->zilla->is_trial;
41              
42 6         1318 foreach my $file ( @{ $self->found_files })
  6         39  
43             {
44 6 50 33     6585 next if $file->can('is_bytes') and $file->is_bytes;
45 6 50 33     313 next if $INC{module_notional_filename('Dist::Zilla::Role::MutableFile')} and not $file->does('Dist::Zilla::Role::MutableFile');
46              
47             # it would be nice if we could just ask Module::Metadata for the line
48             # (and character offset!) that it already found - might be faster
49              
50 6         2798 my $document = $self->ppi_document_for_file($file);
51              
52 6         22159 my $package_stmt = $document->find_first('PPI::Statement::Package');
53 6 50       1358 $self->log_debug([ 'skipping %s: no package statement found', $file->name ]), return
54             if not $package_stmt;
55              
56 6         12 my %seen_version_for_package;
57 6         12 my $package = 'main';
58              
59 6         10 my $munged = 0;
60              
61             my $finder = sub {
62 86     86   646 my $node = $_[1];
63 86 100       222 return 0 if not $node->isa('PPI::Statement');
64              
65             # this does not properly handle scopes - see the ::Package docs
66 20 100       79 $package = $node->namespace, return undef if $node->isa('PPI::Statement::Package');
67              
68             # do not descend into the nodes comprising the statement
69             return undef unless $node->isa('PPI::Statement::Variable')
70             and $node->type eq 'our'
71 12 100 100     73 and grep { $_ eq '$VERSION' } $node->variables;
  4   66     295  
72              
73             # find the line with this statement - this is safe to do even
74             # after munging because we do not insert or remove lines
75 4         17 my @content_lines = split(/\n/, $file->content, $node->line_number + 1);
76 4         1868 return $content_lines[$#content_lines - 1] !~ /;\h*#\s*TRIAL/; # no existing comment on line
77 6         36 };
78              
79 6         44 my $matches = $document->find($finder);
80 6 100       61 if (not $matches)
81             {
82 4 50       12 $self->log_fatal('got PPI error') if not defined $matches;
83 4         48 next;
84             }
85              
86 2         4 foreach my $node (@{ $matches })
  2         5  
87             {
88 3         18 $self->log_debug([ 'Adding # TRIAL to $VERSION line for %s', $package ]);
89              
90             # inserted in reverse order... can I insert both at the same time?
91 3         781 $node->insert_after(PPI::Token::Comment->new('# TRIAL'));
92 3         164 $node->insert_after(PPI::Token::Whitespace->new(' '));
93 3         83 $document->flush_locations;
94 3         310 $munged = 1;
95             }
96              
97 2 50       14 $self->save_ppi_document_to_file($document, $file) if $munged;
98             }
99             }
100              
101             __PACKAGE__->meta->make_immutable;
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =head1 NAME
110              
111             Dist::Zilla::Plugin::TrialVersionComment - Add a "# TRIAL" comment after your version declaration in trial releases
112              
113             =head1 VERSION
114              
115             version 0.007
116              
117             =head1 SYNOPSIS
118              
119             In your F<dist.ini>:
120              
121             [TrialVersionComment]
122              
123             =head1 DESCRIPTION
124              
125             This is a L<Dist::Zilla> plugin that munges your F<.pm> files to add a
126             C<# TRIAL> comment after C<$VERSION> assignments, if the release is C<--trial>.
127              
128             If the distribution is not a C<--trial> release (i.e. C<release_status> in
129             metadata is C<stable>), this plugin does nothing.
130              
131             =for stopwords PkgVersion OurPkgVersion RewriteVersion
132              
133             Other plugins that munge versions into files also add the C<# TRIAL> comment (such as
134             L<[PkgVersion]|Dist::Zilla::Plugin::PkgVersion>,
135             L<[OurPkgVersion]|Dist::Zilla::Plugin::OurPkgVersion>, and
136             L<[RewriteVersion]|Dist::Zilla::Plugin::RewriteVersion>, so you would
137             generally only need this plugin if you added the version yourself, manually.
138              
139             Nothing currently parses these comments, but the idea is that things like
140             L<Module::Metadata> might make use of this in the future.
141              
142             =head1 PURPOSE
143              
144             This is a rather silly plugin, and doesn't really add any value. I suppose if
145             you are adding C<$VERSION> statements with C<[PkgVersion]> it could be useful,
146             but in that case I think I'd rather patch C<[PkgVersion]> to add the comment
147             as well, rather than going to all the effort of re-parsing the perl document
148             again. I only really wrote this as an exercise in using L<PPI>, to help
149             diagnose some issues I was seeing with L<Acme::LookOfDisapproval> and
150             utf8-encoded perl code.
151              
152             =for Pod::Coverage munge_files
153              
154             =head1 ACKNOWLEDGEMENTS
155              
156             =for stopwords xdg
157              
158             Inspiration for this module came about through multiple toolchain conversations with David Golden (xdg).
159              
160             =head1 SEE ALSO
161              
162             =for stopwords BumpVersionAfterRelease
163             OverridePkgVersion
164             PkgVersionIfModuleWithPod
165             SurgicalPkgVersion
166              
167             =over 4
168              
169             =item *
170              
171             L<[PkgVersion]|Dist::Zilla::Plugin::PkgVersion>
172              
173             =item *
174              
175             L<[OurPkgVersion]|Dist::Zilla::Plugin::OurPkgVersion>
176              
177             =item *
178              
179             L<[BumpVersionAfterRelease]|Dist::Zilla::Plugin::BumpVersionAfterRelease>
180              
181             =item *
182              
183             L<[OverridePkgVersion]|Dist::Zilla::Plugin::OverridePkgVersion>
184              
185             =item *
186              
187             L<[SurgicalPkgVersion]|Dist::Zilla::Plugin::SurgicalPkgVersion>
188              
189             =item *
190              
191             L<[PkgVersionIfModuleWithPod]|Dist::Zilla::Plugin::PkgVersionIfModuleWithPod>
192              
193             =back
194              
195             =head1 SUPPORT
196              
197             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-TrialVersionComment>
198             (or L<bug-Dist-Zilla-Plugin-TrialVersionComment@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-TrialVersionComment@rt.cpan.org>).
199              
200             There is also a mailing list available for users of this distribution, at
201             L<http://dzil.org/#mailing-list>.
202              
203             There is also an irc channel available for users of this distribution, at
204             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
205              
206             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
207              
208             =head1 AUTHOR
209              
210             Karen Etheridge <ether@cpan.org>
211              
212             =head1 CONTRIBUTOR
213              
214             =for stopwords David Golden
215              
216             David Golden <dagolden@cpan.org>
217              
218             =head1 COPYRIGHT AND LICENCE
219              
220             This software is copyright (c) 2014 by Karen Etheridge.
221              
222             This is free software; you can redistribute it and/or modify it under
223             the same terms as the Perl 5 programming language system itself.
224              
225             =cut