File Coverage

blib/lib/Dist/Zilla/Plugin/PkgVersion/Block.pm
Criterion Covered Total %
statement 56 65 86.1
branch 21 30 70.0
condition 12 15 80.0
subroutine 8 8 100.0
pod 0 3 0.0
total 97 121 80.1


line stmt bran cond sub pod time code
1 1     1   684310 use 5.14.0;
  1         3  
  1         68  
2              
3             # ABSTRACT: PkgVersion for block packages
4              
5             package Dist::Zilla::Plugin::PkgVersion::Block {
6              
7             our $VERSION = '0.0106'; # VERSION
8              
9 1     1   5 use Moose;
  1         2  
  1         7  
10             with('Dist::Zilla::Role::FileMunger',
11             'Dist::Zilla::Role::FileFinderUser' => { default_finders => [':InstallModules', ':ExecFiles'] },
12             'Dist::Zilla::Role::PPI'
13             );
14              
15 1     1   5914 use PPI;
  1         92359  
  1         37  
16 1     1   6 use MooseX::Types::Perl qw/StrictVersionStr/;
  1         1  
  1         13  
17 1     1   2339 use namespace::sweep;
  1         6204  
  1         6  
18              
19             sub munge_files {
20 1     1 0 92837 my($self) = @_;
21              
22 1 50       8 if(!StrictVersionStr->check($self->zilla->version)) {
23 0         0 Carp::croak('Version ' . $self->zilla->version . ' does not conform to the strict version string format.') ;
24             }
25 1         1050 $self->munge_file($_) for @{ $self->found_files };
  1         8  
26             }
27              
28             sub munge_file {
29 5     5 0 4282 my $self = shift;
30 5         7 my $file = shift;
31              
32 5 50       16 if($file->is_bytes) {
33 0         0 $self->log_debug([ "%s has 'bytes' encoding, skipping...", $file->name ]);
34 0         0 return;
35             }
36 5         258 return $self->munge_perl($file);
37             }
38              
39             sub munge_perl {
40 5     5 0 7 my $self = shift;
41 5         6 my $file = shift;
42              
43 5         16 my $document = $self->ppi_document_for_file($file);
44              
45 5         11989 my $package_statements = $document->find('PPI::Statement::Package');
46 5 50       3093 if(!$package_statements) {
47 0         0 $self->log_debug([ 'skipping %s: no package statement found', $file->name ]);
48 0         0 return;
49             }
50              
51 5 50       19 if($self->document_assigns_to_variable($document, '$VERSION')) {
52 0         0 $self->log_fatal([ 'existing assignment to $VERSION in %s', $file->name ]);
53             }
54              
55 5         4390 my %seen;
56 5         7 my $munged = 0;
57              
58 5         8 STATEMENT:
59 5         7 for my $stmt (@{ $package_statements }) {
60 6         16 my $package = $stmt->namespace;
61 6 50       121 if($seen{ $package }++) {
62 0         0 $self->log([ 'skipping package re-declaration for %s', $package ]);
63 0         0 next STATEMENT;
64             }
65 6 100       12 if($stmt->content =~ m{ package \s* (?:\#.*)? \n \s* \Q$package}x ) {
66 1         61 $self->log([ 'skipping private package %s in %s', $package, $file->name] );
67 1         339 next STATEMENT;
68             }
69 5 50       319 if($package =~ m{\P{ASCII}}) {
70 0         0 $self->log('non-ASCII package name is likely to cause problems');
71             }
72              
73 5         5 my $count = 0;
74 5         6 my $name_token = undef;
75              
76             TOKEN:
77 5         17 foreach my $token ($stmt->tokens) {
78 24         208 ++$count;
79              
80 24 50 66     45 last TOKEN if $count == 1 && $token ne 'package';
81 24 50 66     123 last TOKEN if $count == 2 && $token !~ m{\s+};
82 24 50 66     71 last TOKEN if $count == 3 && $token !~ m{\w+(::\w+)*};
83 24 100       60 $name_token = $token if $count == 3;
84 24 100 100     44 last TOKEN if $count == 4 && $token !~ m{\s+};
85 23 100 100     67 last TOKEN if $count == 5 && $token ne '{';
86              
87 22 100       63 if($count == 5) {
88 3         105 my $version_token = PPI::Token::Comment->new(" " . $self->zilla->version);
89 3         117 $name_token->insert_after($version_token);
90 3         194 $munged = 1;
91 3         13 $self->log([ 'adding version to %s in %s', $package, $file->name ]);
92 3         963 last TOKEN;
93             }
94             }
95             }
96 5 100       47 $self->save_ppi_document_to_file($document, $file) if $munged;
97             }
98             }
99              
100             1;
101              
102             __END__
103              
104             =pod
105              
106             =encoding UTF-8
107              
108             =head1 NAME
109              
110             Dist::Zilla::Plugin::PkgVersion::Block - PkgVersion for block packages
111              
112             =head1 VERSION
113              
114             Version 0.0106, released 2015-02-18.
115              
116             =head1 SYNOPSIS
117              
118             # dist.ini
119             [PkgVersion::Block]
120              
121             =head1 DESCRIPTION
122              
123             This plugin turns:
124              
125             package My::Package {
126             ...
127             }
128              
129             into:
130              
131             package My::Package 0.01 {
132             ...
133             }
134              
135             for all packages in the distribution.
136              
137             The block package syntax was introduced in Perl 5.14, so this plugin is only usable in projects that only support 5.14+.
138              
139             There are no attributes. However:
140              
141             =over 4
142              
143             =item *
144              
145             Having an existing assignment to $VERSION in the file is a fatal error.
146              
147             =item *
148              
149             Packages with a version number between the namespace and the block are silently skipped.
150              
151             =back
152              
153             =head1 KNOWN PROBLEMS
154              
155             In files with more than one package block it is currently necessary to end (all but the last) package blocks with a semicolon. Otherwise only the first package will get a version number:
156              
157             package My::Package {
158             ...
159             };
160              
161             package My::Package::Other {
162             ...
163             }
164              
165             =head1 SEE ALSO
166              
167             =over 4
168              
169             =item *
170              
171             L<Dist::Zilla::Plugin::PkgVersion> (on which this is based)
172              
173             =item *
174              
175             L<Dist::Zilla::Plugin::OurPkgVersion>
176              
177             =back
178              
179             =head1 SOURCE
180              
181             L<https://github.com/Csson/p5-Dist-Zilla-Plugin-PkgVersion-Block>
182              
183             =head1 HOMEPAGE
184              
185             L<https://metacpan.org/release/Dist-Zilla-Plugin-PkgVersion-Block>
186              
187             =head1 AUTHOR
188              
189             Erik Carlsson <info@code301.com>
190              
191             =head1 COPYRIGHT AND LICENSE
192              
193             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
194              
195             This is free software; you can redistribute it and/or modify it under
196             the same terms as the Perl 5 programming language system itself.
197              
198             =cut