File Coverage

blib/lib/Dist/Zilla/Plugin/Author/VDB/Version/Read.pm
Criterion Covered Total %
statement 32 34 94.1
branch 7 8 87.5
condition 1 2 50.0
subroutine 8 8 100.0
pod 0 1 0.0
total 48 53 90.5


line stmt bran cond sub pod time code
1             # ---------------------------------------------------------------------- copyright and license ---
2             #
3             # file: lib/Dist/Zilla/Plugin/Author/VDB/Version/Read.pm
4             #
5             # Copyright © 2015 Van de Bugger
6             #
7             # This file is part of perl-Dist-Zilla-PluginBundle-Author-VDB.
8             #
9             # perl-Dist-Zilla-PluginBundle-Author-VDB is free software: you can redistribute it and/or modify
10             # it under the terms of the GNU General Public License as published by the Free Software
11             # Foundation, either version 3 of the License, or (at your option) any later version.
12             #
13             # perl-Dist-Zilla-PluginBundle-Author-VDB is distributed in the hope that it will be useful, but
14             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15             # PARTICULAR PURPOSE. See the GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License along with
18             # perl-Dist-Zilla-PluginBundle-Author-VDB. If not, see <http://www.gnu.org/licenses/>.
19             #
20             # ---------------------------------------------------------------------- copyright and license ---
21              
22             #pod =for test_synopsis BEGIN { die "SKIP: not a Perl code"; }
23             #pod
24             #pod =head1 SYNOPSIS
25             #pod
26             #pod F<dist.in> file:
27             #pod
28             #pod ; version = v0.3.1 ; *Should **not** be used.*
29             #pod ...
30             #pod [Author::VDB::Version::Read]
31             #pod file = VERSION
32             #pod
33             #pod F<VERSION> file:
34             #pod
35             #pod v0.3.1
36             #pod
37             #pod =head1 DESCRIPTION
38             #pod
39             #pod This plugin does C<VersionProvider> role. It reads version from specified file. Trailing
40             #pod whitespaces (including newlines) in a file is allowed and ignored.
41             #pod
42             #pod =cut
43              
44             # --------------------------------------------------------------------------------------------------
45              
46             package Dist::Zilla::Plugin::Author::VDB::Version::Read;
47              
48 2     2   2238276 use Moose;
  2         5  
  2         11  
49 2     2   10253 use namespace::autoclean;
  2         4  
  2         18  
50 2     2   184 use version 0.77;
  2         56  
  2         16  
51              
52             # ABSTRACT: Read version from a file
53             our $VERSION = 'v0.11.2_05'; # TRIAL VERSION
54              
55             with 'Dist::Zilla::Role::VersionProvider';
56             with 'Dist::Zilla::Role::ErrorLogger' => { -version => 'v0.9.0' };
57             # ^ We need `ErrorLogger` to throw an exception, not plain string.
58              
59 2     2   214 use Path::Tiny;
  2         2  
  2         114  
60 2     2   10 use Try::Tiny;
  2         3  
  2         919  
61              
62             # --------------------------------------------------------------------------------------------------
63              
64             #pod =option file
65             #pod
66             #pod Name of file to read version from. Default value is C<VERSION>. Specifying an empty file disables
67             #pod the plugin.
68             #pod
69             #pod =cut
70              
71             has file => (
72             isa => 'Str',
73             is => 'ro',
74             default => 'VERSION',
75             );
76              
77             has _file => (
78             isa => 'Path::Tiny',
79             is => 'ro',
80             init_arg => undef,
81             lazy => 1,
82             default => sub {
83             my ( $self ) = @_;
84             my $path = path( $self->file );
85             if ( $path->is_absolute ) {
86             $self->abort( [ "Bad version file '%s': absolute path is not allowed", $self->file ] );
87             };
88             return $path->absolute( $self->zilla->root );
89             },
90             );
91              
92             # --------------------------------------------------------------------------------------------------
93              
94             #pod =for Pod::Coverage provide_version
95             #pod
96             #pod =cut
97              
98             sub provide_version {
99 15     15 0 156395 my ( $self ) = @_;
100 15         27 my $version;
101 15 100       670 if ( $self->file ) {
102             try {
103 14     14   1039 $version = $self->_file->slurp;
104             } catch {
105 2     2   795 my $ex = $_;
106 2   50     15 my $class = blessed( $ex ) || '';
107 2 100       11 if ( 0 ) {
    50          
108 0         0 } elsif ( $class eq 'Dist::Zilla::Role::ErrorLogger::Exception::Abort' ) {
109             # All the messages are aready logged, just rethrow the exception.
110 1         10 die $ex; ## no critic ( RequireCarping )
111             } elsif ( $class eq 'Path::Tiny::Error' ) {
112 1         26 chomp( my $em = "$ex" );
113 1         10 $self->log_debug( "$em" );
114 1         103 $self->abort( [ "Can't read version file '%s': %s", $self->file, $ex->{ err } ] );
115             } else {
116 0         0 $self->abort( $ex );
117             };
118 14         137 };
119 12         2846 $version =~ s{ \s+ \z }{}x; # Strip trailing whitespace (including newlines).
120 12 100       62 if ( not version::is_lax( $version ) ) {
121 1         65 $self->abort( [ "Invalid version string '%s' at %s line 1", $version, $self->file ] );
122             };
123             } else {
124 1         6 $self->log_debug( 'no version file specified' );
125             };
126 12         485 return $version;
127             };
128              
129             # --------------------------------------------------------------------------------------------------
130              
131             __PACKAGE__->meta->make_immutable();
132              
133             1;
134              
135             #pod =head1 COPYRIGHT AND LICENSE
136             #pod
137             #pod Copyright (C) 2015 Van de Bugger
138             #pod
139             #pod License GPLv3+: The GNU General Public License version 3 or later
140             #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
141             #pod
142             #pod This is free software: you are free to change and redistribute it. There is
143             #pod NO WARRANTY, to the extent permitted by law.
144             #pod
145             #pod
146             #pod =cut
147              
148             # end of file #
149              
150             __END__
151              
152             =pod
153              
154             =encoding UTF-8
155              
156             =head1 NAME
157              
158             Dist::Zilla::Plugin::Author::VDB::Version::Read - Read version from a file
159              
160             =head1 VERSION
161              
162             Version v0.11.2_05, released on 2016-12-08 00:45 UTC.
163             This is a B<trial release>.
164              
165             =for test_synopsis BEGIN { die "SKIP: not a Perl code"; }
166              
167             =head1 SYNOPSIS
168              
169             F<dist.in> file:
170              
171             ; version = v0.3.1 ; *Should **not** be used.*
172             ...
173             [Author::VDB::Version::Read]
174             file = VERSION
175              
176             F<VERSION> file:
177              
178             v0.3.1
179              
180             =head1 DESCRIPTION
181              
182             This plugin does C<VersionProvider> role. It reads version from specified file. Trailing
183             whitespaces (including newlines) in a file is allowed and ignored.
184              
185             =head1 OPTIONS
186              
187             =head2 file
188              
189             Name of file to read version from. Default value is C<VERSION>. Specifying an empty file disables
190             the plugin.
191              
192             =for Pod::Coverage provide_version
193              
194             =head1 AUTHOR
195              
196             Van de Bugger <van.de.bugger@gmail.com>
197              
198             =head1 COPYRIGHT AND LICENSE
199              
200             Copyright (C) 2015 Van de Bugger
201              
202             License GPLv3+: The GNU General Public License version 3 or later
203             <http://www.gnu.org/licenses/gpl-3.0.txt>.
204              
205             This is free software: you are free to change and redistribute it. There is
206             NO WARRANTY, to the extent permitted by law.
207              
208             =cut