File Coverage

blib/lib/Badge/Depot/Plugin/Perl.pm
Criterion Covered Total %
statement 27 50 54.0
branch 0 12 0.0
condition 0 5 0.0
subroutine 9 12 75.0
pod 0 2 0.0
total 36 81 44.4


line stmt bran cond sub pod time code
1 1     1   13719 use strict;
  1         1  
  1         28  
2 1     1   3 use warnings;
  1         2  
  1         32  
3              
4             package Badge::Depot::Plugin::Perl;
5              
6 1     1   423 use Moose;
  1         1540340  
  1         8  
7 1     1   5455 use namespace::autoclean;
  1         1018  
  1         4  
8 1     1   480 use MooseX::AttributeShortcuts;
  1         243051  
  1         5  
9 1     1   25046 use Types::Standard qw/Str Bool/;
  1         42033  
  1         10  
10 1     1   949 use Types::URI qw/Uri/;
  1         96804  
  1         11  
11 1     1   730 use JSON::MaybeXS 'decode_json';
  1         2347  
  1         54  
12 1     1   7 use Path::Tiny;
  1         1  
  1         343  
13             with 'Badge::Depot';
14              
15             our $VERSION = '0.0101'; # VERSION
16             # ABSTRACT: Perl version plugin for Badge::Depot
17              
18             has version => (
19             is => 'ro',
20             isa => Str,
21             builder => 1,
22             predicate => 1,
23             );
24             has trailing => (
25             is => 'ro',
26             isa => Str,
27             default => '+',
28             );
29             has custom_image_url => (
30             is => 'ro',
31             isa => Uri,
32             coerce => 1,
33             default => 'https://img.shields.io/badge/perl-%s-brightgreen.svg',
34             );
35              
36             sub BUILD {
37 0     0 0   my $self = shift;
38 0           $self->image_url(sprintf $self->custom_image_url, $self->version);
39 0           $self->image_alt(sprintf 'Requires Perl %s', $self->version);
40             }
41              
42             sub _build_version {
43 0     0     my $self = shift;
44              
45 0 0         return if !path('META.json')->exists;
46              
47 0           my $json = path('META.json')->slurp_utf8;
48 0           my $data = decode_json($json);
49              
50 0 0         return 'unknown' if !exists $data->{'prereqs'}{'runtime'}{'requires'}{'perl'};
51              
52 0           my $version = $data->{'prereqs'}{'runtime'}{'requires'}{'perl'};
53              
54 0           my $nodot_version = $self->version_from($version, qr{^5\.(\d{3})(\d{3})$});
55 0           my $dotted_version = $self->version_from($version, qr{^5\.(\d+)(?:\.(\d+))?$});
56              
57 0   0       return $nodot_version || $dotted_version || 'unknown';
58             }
59              
60             sub version_from {
61 0     0 0   my $self = shift;
62              
63 0           my $version = shift;
64 0           my $regex = shift;
65              
66 0           $version =~ s{^v}{};
67              
68 0 0         return if $version !~ m{$regex};
69              
70 0           my $major = $1;
71 0           my $minor = $2;
72              
73 0           $major =~ s{^0+}{};
74 0 0         $minor =~ s{^0+}{}x if defined $minor;
75              
76 0 0 0       my $display_version = join '.' => 5, (length $major ? $major : ()), (defined $minor && length $minor ? $minor : ());
    0          
77 0           return $display_version . $self->trailing;
78             }
79              
80             1;
81              
82             __END__
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             Badge::Depot::Plugin::Perl - Perl version plugin for Badge::Depot
91              
92             =head1 VERSION
93              
94             Version 0.0101, released 2015-04-18.
95              
96             =head1 SYNOPSIS
97              
98             If used standalone:
99              
100             use Badge::Depot::Plugin::Perl;
101              
102             my $badge = Badge::Depot::Plugin::Perl->new(version => '5.8.5+');
103              
104             print $badge->to_html;
105             # prints '<img src="https://img.shields.io/badge/perl-5.8.5+-brightgreen.svg" />'
106              
107             If used with L<Pod::Weaver::Section::Badges>, in weaver.ini:
108              
109             [Badges]
110             ; other settings
111             badge = Perl
112             -perl_version = 5.8.5
113              
114             =head1 DESCRIPTION
115              
116             Creates a Perl version badge, like this:
117              
118             =for HTML <img src="https://img.shields.io/badge/perl-5.8.5+-brightgreen.svg" />
119              
120             =for markdown ![Requires Perl 5.8+](https://img.shields.io/badge/perl-5.8.5+-brightgreen.svg)
121              
122             This class consumes the L<Badge::Depot> role.
123              
124             =head1 ATTRIBUTES
125              
126             All attributes are optional.
127              
128             =head2 version
129              
130             The minimum supported Perl version. If it isn't given, it looks for a C<prereqs/runtime/requires/perl> entry in C<META.json> and uses that.
131              
132             If it is neither given or exists in C<META.json>, the string C<unknown> is displayed.
133              
134             =head2 trailing
135              
136             A string to add after the version, if the version is fetched from C<META.json>. Defaults to C<+>.
137              
138             Not used if C<version> is set.
139              
140             =head2 custom_image_url
141              
142             By default, this module shows an image from L<shields.io|https://shields.io>. Use this attribute to override that with a custom url. Use a C<%s> placeholder where the version should be inserted.
143              
144             =head1 SEE ALSO
145              
146             =over 4
147              
148             =item *
149              
150             L<Badge::Depot>
151              
152             =back
153              
154             =head1 SOURCE
155              
156             L<https://github.com/Csson/p5-Badge-Depot-Plugin-Perl>
157              
158             =head1 HOMEPAGE
159              
160             L<https://metacpan.org/release/Badge-Depot-Plugin-Perl>
161              
162             =head1 AUTHOR
163              
164             Erik Carlsson <info@code301.com>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut