File Coverage

blib/lib/Dist/Zilla/Plugin/GitHubREADME/Badge.pm
Criterion Covered Total %
statement 62 67 92.5
branch 37 48 77.0
condition 3 5 60.0
subroutine 13 13 100.0
pod 0 5 0.0
total 115 138 83.3


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