File Coverage

blib/lib/Dist/Zilla/Plugin/CoderwallEndorse.pm
Criterion Covered Total %
statement 41 47 87.2
branch 6 10 60.0
condition 2 3 66.6
subroutine 9 10 90.0
pod 0 3 0.0
total 58 73 79.4


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::CoderwallEndorse;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Adds a Coderwall 'endorse' button to README Markdown file (DEPRECATED)
4             $Dist::Zilla::Plugin::CoderwallEndorse::VERSION = '0.2.1';
5              
6 2     2   3626486 use strict;
  2         4  
  2         69  
7 2     2   8 use warnings;
  2         3  
  2         103  
8              
9 2     2   608 use Moose;
  2         565873  
  2         16  
10              
11 2     2   15022 use List::Util qw/ first /;
  2         4  
  2         243  
12              
13 2     2   1442 use Dist::Zilla::Role::File::ChangeNotification;
  2         38215  
  2         1726  
14              
15             with qw/
16             Dist::Zilla::Role::Plugin
17             Dist::Zilla::Role::FileMunger
18             /;
19              
20             has users => (
21             is => 'ro',
22             isa => 'Str',
23             );
24              
25             has "filename" => (
26             isa => 'Str',
27             is => 'ro',
28             default => 'README.mkdn',
29             );
30              
31             has mapping => (
32             traits => [ 'Hash' ],
33             is => 'ro',
34             isa => 'HashRef',
35             lazy => 1,
36             default => sub {
37             my $self = shift;
38             my %m;
39             for my $p ( split /\s*,\s*/, $self->users ) {
40             my( $cd, $auth) = $p =~ /(\w+)\s*:\s*(.+?)\s*$/;
41             $m{$auth} = $cd;
42             }
43              
44             return \%m;
45             },
46             handles => {
47             'authors' => 'keys',
48             'cd_user' => 'get',
49             },
50             );
51              
52             has "_last_content" => (
53             isa => 'Str',
54             is => 'rw',
55             default => '',
56             );
57              
58             sub munge_files {
59 1     1 0 95857 my $self = shift;
60              
61 1         65 my $filename = $self->filename;
62 1 50       2 my( $file ) = grep { $_->name eq $filename } @{ $self->zilla->files }
  2         144  
  1         45  
63             or return $self->log([ "file '%s' not found", $filename ]);
64              
65 1         68 $self->munge_file($file);
66 1         329 $self->watch($file);
67             }
68              
69             sub watch {
70 1     1 0 3 my( $self, $file ) = @_;
71            
72 1 50       12 Dist::Zilla::Role::File::ChangeNotification->meta->apply($file)
73             unless $file->does('Dist::Zilla::Role::File::ChangeNotification');
74              
75 1         52516 my $plugin = $self;
76             $file->on_changed(sub {
77 0     0   0 my ($self, $newcontent) = @_;
78              
79 0         0 $self->_content_checksum(0);
80 0         0 $self->watch_file;
81              
82             # If the new content is actually different, recalculate
83             # the content based on the updates.
84 0 0       0 return if $newcontent eq $plugin->_last_content;
85              
86 0         0 $plugin->log_debug('someone tried to munge ' . $self->name . ' after we read from it. Making modifications again...');
87 0         0 $plugin->munge_file($file);
88 1         10 });
89              
90 1         103 $file->watch_file;
91             }
92              
93             sub munge_file {
94 1     1 0 3 my( $self, $file ) = @_;
95              
96 1         5 $self->log_debug([ 'CoderwallEndorse updating contents of %s in dist', $file->name ]);
97              
98 1         153 my $new_content;
99              
100 1         5 for my $line ( split /\n/, $file->content ) {
101 7 100       1194 if ( $line=~ /^# AUTHOR/ ... $line =~ /^#/ ) {
102 5         317 for my $auth ( $self->authors ) {
103              
104             # author not mentioned, or endorse link already there
105 10 100 66     44 next if -1 == index $line, $auth
106             or -1 != index $line, '[endorse]';
107              
108 2         177 $line .= sprintf " [![endorse](http://api.coderwall.com/%s/endorsecount.png)](http://coderwall.com/%s)",
109             ( $self->cd_user($auth) ) x 2;
110              
111             }
112             }
113 7         23 $new_content .= $line."\n";
114             }
115              
116 1         63 $self->_last_content($new_content);
117 1         5 $file->content($new_content);
118             }
119              
120             __PACKAGE__->meta->make_immutable;
121 2     2   21 no Moose;
  2         4  
  2         16  
122              
123             1;
124              
125             __END__
126              
127             =pod
128              
129             =encoding UTF-8
130              
131             =head1 NAME
132              
133             Dist::Zilla::Plugin::CoderwallEndorse - Adds a Coderwall 'endorse' button to README Markdown file (DEPRECATED)
134              
135             =head1 VERSION
136              
137             version 0.2.1
138              
139             =head1 SYNOPSIS
140              
141             ; in dist.ini
142              
143             ; typically, to create the README off the main module
144             [ReadmeMarkdownFromPod]
145              
146             [CoderwallEndorse]
147             filename = README.mkdn
148             users = coderwall_name : author name, other_cw_name : other author
149              
150             =head1 DESCRIPTION
151              
152             B<Deprecated>: Coderwall endorse buttons are, alas, not a thing anymore. :-(
153              
154             If a C<README.mkdn> file is presents, a Coderwall 'endorse' button will be
155             added beside author names if a author-name-to-coderwall-user mapping has been
156             given.
157              
158             =head1 SEE ALSO
159              
160             L<http://www.coderwall.com>
161              
162             L<Dist::Zilla::Plugin::ReadmeMarkdownFromPod>
163              
164             For an example of what the result of this plugin looks like, see its
165             GitHub main page: L<https://github.com/yanick/Dist-Zilla-Plugin-CoderwallEndorse>
166              
167             Original blog entry: L<http://babyl.dyndns.org/techblog/entry/coderwall-button>
168              
169             =head1 AUTHOR
170              
171             Yanick Champoux <yanick@cpan.org>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2025 by Yanick Champoux.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut