File Coverage

blib/lib/Dist/Zilla/Plugin/GitHubREADME/Badge.pm
Criterion Covered Total %
statement 61 66 92.4
branch 37 48 77.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 0 5 0.0
total 113 135 83.7


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GitHubREADME::Badge;
2              
3 5     5   17650547 use strict;
  5         18  
  5         194  
4 5     5   38 use warnings;
  5         23  
  5         186  
5 5     5   176 use 5.008_005;
  5         21  
6             our $VERSION = '0.33';
7              
8 5     5   45 use Moose;
  5         13  
  5         48  
9 5     5   36035 use Moose::Util::TypeConstraints qw(enum);
  5         13  
  5         57  
10 5     5   2287 use namespace::autoclean;
  5         13  
  5         61  
11 5     5   435 use Dist::Zilla::File::OnDisk;
  5         12  
  5         193  
12 5     5   40 use Path::Tiny;
  5         21  
  5         7270  
13              
14             # same as Dist::Zilla::Plugin::ReadmeAnyFromPod
15             with qw(
16             Dist::Zilla::Role::AfterBuild
17             Dist::Zilla::Role::AfterRelease
18             Dist::Zilla::Role::FileMunger
19             );
20              
21             has badges => (
22             is => 'rw',
23             isa => 'ArrayRef[Str]',
24             default => sub { ['travis', 'coveralls', 'cpants'] },
25             );
26 14     14 0 507862 sub mvp_multivalue_args { ('badges') }
27              
28             has 'place' => ( is => 'rw', isa => 'Str', default => sub { 'top' } );
29              
30             has phase => (
31             is => 'ro',
32             isa => enum([qw(build release filemunge)]),
33             default => 'build',
34             );
35              
36             has readme_file => (
37             is => 'ro',
38             lazy => 1,
39             default => sub {
40             my $self = shift;
41             my @candidates = qw/ README.md README.mkdn README.markdown /;
42              
43              
44             # only for the filemunge phase do we look
45             # in the slurped files
46             if( $self->phase eq 'filemunge' ) {
47             for my $file ( @{ $self->zilla->files } ) {
48             return $file if grep { $file->name eq $_ } @candidates;
49             }
50             }
51             else {
52             # for the other phases we look on disk
53             my $root = path($self->zilla->root);
54              
55             return Dist::Zilla::File::OnDisk->new(
56             name => "$_",
57             content => $_->slurp_raw,
58             encoding => 'bytes',
59             ) for grep { -f $_ } map { $root->child($_) } @candidates
60             }
61              
62             $self->log_fatal('README file not found');
63             },
64             );
65              
66             sub after_build {
67 14     14 0 398104 my ($self) = @_;
68 14 100       618 $self->add_badges if $self->phase eq 'build';
69             }
70              
71             sub after_release {
72 2     2 0 215595 my ($self) = @_;
73 2 100       138 $self->add_badges if $self->phase eq 'release';
74             }
75              
76             sub munge_files {
77 14     14 0 960362 my $self = shift;
78              
79 14 50       722 $self->add_badges if $self->phase eq 'filemunge';
80             }
81              
82              
83             sub add_badges {
84 14     14 0 55 my ($self) = @_;
85              
86 14         464 my $distname = $self->zilla->name;
87 14         978 my $distmeta = $self->zilla->distmeta;
88 14         918 my $dist_version = $self->zilla->version;
89 14         526 my $repository = $distmeta->{resources}->{repository}->{url};
90 14 50       68 return unless $repository;
91 14         226 my ($base_url, $user_name, $repository_name) = ($repository =~ m{^\w+://(.*)/([^\/]+)/(.*?)(\.git|\/|$)});
92 14 50       94 return unless $repository_name;
93              
94 14         41 my @badges;
95 14         32 foreach my $badge (@{$self->badges}) {
  14         620  
96 45 100 66     524 if ($badge eq 'travis' or $badge eq 'travis-ci.org') {
    50          
    50          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    50          
    100          
    100          
    50          
    100          
    50          
    0          
97 12         155 push @badges, "[![Build Status](https://travis-ci.org/$user_name/$repository_name.svg?branch=master)](https://travis-ci.org/$user_name/$repository_name)";
98             } elsif ($badge eq 'travis-ci.com') {
99 0         0 push @badges, "[![Build Status](https://travis-ci.com/$user_name/$repository_name.svg?branch=master)](https://travis-ci.com/$user_name/$repository_name)";
100             } elsif ($badge eq 'appveyor') {
101 0         0 push @badges, "[![AppVeyor Status](https://ci.appveyor.com/api/projects/status/github/$user_name/$repository_name?branch=master&svg=true)](https://ci.appveyor.com/project/$user_name/$repository_name)";
102             } elsif ($badge eq 'coveralls') {
103 12         87 push @badges, "[![Coverage Status](https://coveralls.io/repos/$user_name/$repository_name/badge.svg?branch=master)](https://coveralls.io/r/$user_name/$repository_name?branch=master)"
104             } elsif ($badge eq 'gitter') {
105 1         6 push @badges, "[![Gitter chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/$user_name/$repository_name)";
106             } elsif ($badge eq 'cpants') {
107 12         79 push @badges, "[![Kwalitee status](https://cpants.cpanauthors.org/dist/$distname.png)](https://cpants.cpanauthors.org/dist/$distname)";
108             } elsif ($badge eq 'issues') {
109 1         12 push @badges, "[![GitHub issues](https://img.shields.io/github/issues/$user_name/$repository_name.svg)](https://github.com/$user_name/$repository_name/issues)";
110             } elsif ($badge eq 'github_tag') {
111 1         5 push @badges, "[![GitHub tag](https://img.shields.io/github/tag/$user_name/$repository_name.svg)]()";
112             } elsif ($badge eq 'license') {
113 1         6 push @badges, "[![Cpan license](https://img.shields.io/cpan/l/$distname.svg)](https://metacpan.org/release/$distname)";
114             } elsif ($badge eq 'version') {
115 1         7 push @badges, "[![Cpan version](https://img.shields.io/cpan/v/$distname.svg)](https://metacpan.org/release/$distname)";
116             } elsif ($badge eq 'codecov') {
117 0         0 push @badges, "[![codecov](https://codecov.io/gh/$user_name/$repository_name/branch/master/graph/badge.svg)](https://codecov.io/gh/$user_name/$repository_name)";
118             } elsif ($badge eq 'gitlab_ci') {
119 1         11 push @badges, "[![build status](https://$base_url/$user_name/$repository_name/badges/master/build.svg)]($repository/$user_name/$repository_name/commits/master)";
120             } elsif ($badge eq 'gitlab_cover') {
121 1         10 push @badges, "[![coverage report](https://$base_url/$user_name/$repository_name/badges/master/coverage.svg)]($repository/$user_name/$repository_name/commits/master)";
122             } elsif ($badge eq 'docker_automated') {
123 0         0 push @badges, "[![Docker Automated Build](https://img.shields.io/docker/automated/\L$user_name/$repository_name\E.svg)](https://github.com/$user_name/$repository_name)";
124             } elsif ($badge eq 'docker_build') {
125 1         14 push @badges, "[![Docker Build Status](https://img.shields.io/docker/build/\L$user_name/$repository_name\E.svg)](https://hub.docker.com/r/\L$user_name/$repository_name\E/)";
126             } elsif ($badge =~ m{^github_actions/(.+)}) {
127 1         11 push @badges, "[![Actions Status](https://github.com/$user_name/$repository_name/workflows/$1/badge.svg)](https://github.com/$user_name/$repository_name/actions)";
128             } elsif ($badge eq 'cpancover') {
129 0         0 push @badges, "[![CPAN Cover Status](https://cpancoverbadge.perl-services.de/$distname-$dist_version)](https://cpancoverbadge.perl-services.de/$distname-$dist_version)";
130             }
131             }
132              
133 14         687 my $readme = $self->readme_file;
134              
135 13         79 my $content = $readme->encoded_content;
136              
137 13 100       6608 if ($self->place eq 'bottom') {
138 1         7 $content = $content . "\n\n" . join("\n", @badges);
139             } else {
140 12         120 $content = join("\n", @badges) . "\n\n" . $content;
141             }
142              
143 13         94 $readme->content($content);
144              
145             # need to write it to disk if we're in a
146             # phase that is not filemunge
147 13 50       5509 path( $readme->name )->spew_raw( $readme->encoded_content )
148             if $self->phase ne 'filemunge';
149             }
150              
151             1;
152             __END__
153              
154             =encoding utf-8
155              
156             =head1 NAME
157              
158             Dist::Zilla::Plugin::GitHubREADME::Badge - Dist::Zilla - add badges to github README.md
159              
160             =head1 SYNOPSIS
161              
162             # in dist.ini
163             [GitHubREADME::Badge]
164              
165             # configure it yourself
166             [GitHubREADME::Badge]
167             badges = travis
168             badges = travis-ci.com
169             badges = appveyor
170             badges = coveralls
171             badges = gitter
172             badges = cpants
173             badges = issues
174             badges = github_tag
175             badges = license
176             badges = version
177             badges = codecov
178             badges = gitlab_ci
179             badges = gitlab_cover
180             badges = docker_automated
181             badges = docker_build
182             badges = github_actions/test
183             badges = cpancover
184             place = bottom
185             phase = release
186              
187             =head1 DESCRIPTION
188              
189             Dist::Zilla::Plugin::GitHubREADME::Badge adds badges to GitHub README.md
190              
191             =head1 CONFIG
192              
193             =head2 badges
194              
195             Currently only travis, coveralls, codecov, gitter, cpants and GH issues are
196             supported. However patches are welcome.
197              
198             The default goes to travis, coveralls and cpants.
199              
200             [GitHubREADME::Badge]
201             badges = travis
202             badges = coveralls
203             badges = gitter
204             badges = cpants
205              
206             =head2 place
207              
208             [GitHubREADME::Badge]
209             place = bottom
210              
211             Place the badges at the top or bottom of the README. Defaults to top.
212              
213             =head2 phase
214              
215             [GitHubREADME::Badge]
216             phase = release
217              
218             Which Dist::Zilla phase to add the badges: C<build>, C<release> or C<filemunge>.
219             For the C<build> and C<release> phases, the README that is on disk will
220             be modified, whereas for the C<filemunge> it's the internal zilla version of
221             the README that will be modified.
222              
223             The default is C<build>.
224              
225             =head1 SEE ALSO
226              
227             L<Minilla>, L<Dist::Zilla::Plugin::TravisCI::StatusBadge>
228              
229             =head1 AUTHOR
230              
231             Fayland Lam E<lt>fayland@gmail.comE<gt>
232              
233             =head1 COPYRIGHT
234              
235             Copyright 2014- Fayland Lam
236              
237             =head1 LICENSE
238              
239             This library is free software; you can redistribute it and/or modify
240             it under the same terms as Perl itself.
241              
242             =cut