Plugin/Author/VDB/Version/Read.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 90.0% 9 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 92.6% 63 / 68 CC 7 SCAR 8.7
CC
7
Cov
95%
__ANON__
4.2
provide_version
3.0
__ANON__
2.0
CRAP 2.4
« Plugin/Author/VDB/Hg/Push.pm ↑ Summary
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 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # ABSTRACT: Read version from a file
53 3 our $VERSION = 'v0.11.3'; # VERSION
54  
55 3 with 'Dist::Zilla::Role::VersionProvider';
56 3 with 'Dist::Zilla::Role::ErrorLogger' => { -version => 'v0.9.0' };
57 # ^ We need `ErrorLogger` to throw an exception, not plain string.
58  
59 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 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 14 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
83 14 my ( $self ) = @_;
84 14 my $path = path( $self->file );
85 14 ▶ if ( $path->is_absolute ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->is_absolute) 1 13 14
86 1 $self->abort( [ "Bad version file '%s': absolute path is not allowed", $self->file ] );
87 };
88 13 return $path->absolute( $self->zilla->root );
89 },
90 3 );
91  
92 # --------------------------------------------------------------------------------------------------
93  
94 #pod =for Pod::Coverage provide_version
95 #pod
96 #pod =cut
97  
98 15 ▶sub provide_version {
Subroutine provide_version: called CC=3 SCAR=11.0
Pod undocumented
99 15 my ( $self ) = @_;
100 15 my $version;
101 15 ▶ if ( $self->file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) { } 14 1 15
102 14 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
103 14 $version = $self->_file->slurp;
104 2 ▶ } catch {
Subroutine __ANON__: called CC=4 SCAR=14.2
105 2 my $ex = $_;
106 2 ▶ my $class = blessed( $ex ) || '';
Condition50%1/2
builtin::blessed($ex) || ''
l!l
20
MC/DC0%0/1
builtin::blessed($ex) || ''
builtin::blessed($ex)
Truth table50%1/2
A
A= builtin::blessed($ex)
Aresult
11
00
107 2 ▶ if ( 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (do { $class eq 'Dist::Zilla::Role::ErrorLogger::Exception::Abort' }) { } 1 1 2
elsif ($class eq 'Path::Tiny::Error') { } 1 0 1
108 } elsif ( $class eq 'Dist::Zilla::Role::ErrorLogger::Exception::Abort' ) {
109 # All the messages are aready logged, just rethrow the exception.
110 1 die $ex; ## no critic ( RequireCarping )
111 } elsif ( $class eq 'Path::Tiny::Error' ) {
112 1 chomp( my $em = "$ex" );
113 1 $self->log_debug( "$em" );
114 1 $self->abort( [ "Can't read version file '%s': %s", $self->file, $ex->{ err } ] );
115 } else {
116 0 $self->abort( $ex );
117 };
118 14 };
119 12 $version =~ s{ \s+ \z }{}x; # Strip trailing whitespace (including newlines).
120 12 ▶ if ( not version::is_lax( $version ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (version::is_lax($version)) 1 11 12
121 1 $self->abort( [ "Invalid version string '%s' at %s line 1", $version, $self->file ] );
122 };
123 } else {
124 1 $self->log_debug( 'no version file specified' );
125 };
126 12 return $version;
127 };
128  
129 # --------------------------------------------------------------------------------------------------
130  
131 3 __PACKAGE__->meta->make_immutable();
132  
133 3 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__
« Plugin/Author/VDB/Hg/Push.pm ↑ Summary