File Coverage

blib/lib/Dist/Zilla/Plugin/CheckChangeLog.pm
Criterion Covered Total %
statement 43 46 93.4
branch 11 20 55.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 63 75 84.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::CheckChangeLog;
2             $Dist::Zilla::Plugin::CheckChangeLog::VERSION = '0.04';
3             # ABSTRACT: Dist::Zilla with Changes check
4              
5 2     2   3022210 use 5.004;
  2         8  
6 2     2   512 use Moose;
  2         405145  
  2         18  
7             with 'Dist::Zilla::Role::AfterBuild';
8              
9             has filename => (
10             is => 'ro',
11             isa => 'Str'
12             );
13              
14             sub after_build {
15 4     4 1 168217 my ($self, $args) = @_;
16              
17 4         13 my $root = $args->{build_root};
18 4         265 my $filename = chomp $self->{filename};
19 4         15 my @change_files;
20              
21 4 50       13 if ($filename) {
22 0         0 my $file = $self->zilla->root->file($filename);
23 0 0       0 die "[CheckChangeLog] $! $filename\n" unless -e $file;
24 0         0 push @change_files, Dist::Zilla::File::OnDisk->new({name => $file});
25             } else {
26             # Search for Changes or ChangeLog on build root
27 4         22 my @files = $self->_find_change_files($root);
28 4 50       106 die "[CheckChangeLog] No changelog file found.\n" unless scalar @files;
29 4         12 push @change_files, @files;
30             }
31              
32 4         12 for my $file (@change_files) {
33 5 100       26 if ($self->has_version($file->content, $self->zilla->version)) {
34 2         9 $self->log("[CheckChangeLog] OK with " . $file->name);
35 2         977 return;
36             }
37             }
38              
39 2         6 my $msg = "[CheckChangeLog] No Change Log in ";
40 2         6 $msg .= join(', ', map { $_->name } @change_files) . "\n";
  2         16  
41 2         108 $self->log($msg);
42 2         900 $msg = "[CheckChangeLog] Update your Changes file with an entry for ";
43 2         55 $msg .= $self->zilla->version . "\n";
44 2         82 die $msg;
45             }
46              
47             sub has_version {
48 5     5 1 2734 my ($self, $content, $version) = @_;
49              
50 5         30 for my $line (split(/\n/, $content)) {
51              
52             # no blank lines
53 4 50       22 next unless $line =~ /\S/;
54              
55             # skip things that look like bullets
56 4 50       16 next if $line =~ /^\s+/;
57 4 50       12 next if $line =~ /^\*/;
58 4 50       12 next if $line =~ /^\-/;
59              
60             # seen it?
61 4 100       41 return 1 if $line =~ /\Q$version\E/;
62             }
63 3         13 return 0;
64             }
65              
66             sub _find_change_files {
67 4     4   12 my ($self, $root) = @_;
68 4         126 my $files = $self->zilla->files;
69 4         136 my $change_file_re = qr/Change(?:s|Log)?/i;
70 4 50   17   25 my $filter = sub { -e "$root/$_->{name}" && $_->{name} =~ $change_file_re };
  17         52  
71 4         20 grep { $filter->($_) } @{$files};
  17         351  
  4         12  
72             }
73              
74             __PACKAGE__->meta->make_immutable;
75 2     2   14330 no Moose;
  2         8  
  2         11  
76              
77             1;
78              
79             __END__
80              
81             =pod
82              
83             =encoding UTF-8
84              
85             =head1 NAME
86              
87             Dist::Zilla::Plugin::CheckChangeLog - Dist::Zilla with Changes check
88              
89             =head1 VERSION
90              
91             version 0.04
92              
93             =head1 SYNOPSIS
94              
95             # dist.ini
96             [CheckChangeLog]
97              
98             # or
99             [CheckChangeLog]
100             filename = Changes.pod
101              
102             =head1 DESCRIPTION
103              
104             This plugin will examine your changes file after a build to make sure it has an entry for your distributions current version prior to a release.
105              
106             =head1 File name conventions
107              
108             With no arguments CheckChangeLog will only look in files named Changes and ChangeLog (case insensitive) within the root directory of your dist. Note you can always pass a filename argument if you have an unconvential name and place for your changelog.
109              
110             =head1 METHODS
111              
112             =head2 after_build
113              
114             =head2 has_version($content_str, $version_str)
115              
116             =head1 AUTHORS
117              
118             Fayland Lam, C<< <fayland at gmail.com> >>
119              
120             =head1 AUTHOR
121              
122             Fayland Lam <fayland@gmail.com>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2017 by Fayland Lam.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut