File Coverage

blib/lib/Dist/Zilla/Role/Git/StringFormatter.pm
Criterion Covered Total %
statement 51 55 92.7
branch 6 10 60.0
condition 0 2 0.0
subroutine 12 12 100.0
pod n/a
total 69 79 87.3


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dist-Zilla-Plugin-Git
3             #
4             # This software is copyright (c) 2009 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Dist::Zilla::Role::Git::StringFormatter;
10             # ABSTRACT: Provide a String::Formatter for commit messages
11              
12             our $VERSION = '2.046';
13              
14 11     11   7584 use 5.008;
  11         48  
15 11     11   77 use strict;
  11         36  
  11         406  
16 11     11   66 use warnings;
  11         30  
  11         482  
17              
18 11     11   76 use namespace::autoclean;
  11         25  
  11         113  
19 11     11   985 use List::Util qw{ first };
  11         28  
  11         1016  
20              
21 11     11   80 use Moose::Role;
  11         22  
  11         143  
22 11     11   61846 use MooseX::Has::Sugar;
  11         39  
  11         132  
23 11     11   1200 use Types::Standard qw{ Str };
  11         42  
  11         126  
24              
25             requires qw(changelog log zilla);
26              
27             use String::Formatter method_stringf => {
28             -as => '_format_string_sub',
29             codes => {
30 20         400 c => sub { $_[0]->_get_changes },
31 0         0 d => sub { require DateTime;
32 0   0     0 DateTime->now(time_zone => $_[0]->time_zone)
33             ->format_cldr($_[1] || 'dd-MMM-yyyy') },
34 40         734 n => sub { "\n" },
35 0         0 N => sub { $_[0]->zilla->name },
36 0 0       0 t => sub { $_[0]->zilla->is_trial
    0          
37             ? (defined $_[1] ? $_[1] : '-TRIAL') : '' },
38 2         684 v => sub { $_[0]->zilla->version },
39 28         7260 V => sub { my $v = $_[0]->zilla->version; $v =~ s/\Av//; $v },
  28         1560  
  28         264  
40             },
41 11     11   17129 };
  11         33378  
  11         219  
42              
43             #pod =attr changelog
44             #pod
45             #pod The filename of your F<Changes> file. (Must be provided by the class
46             #pod that consumes this role.)
47             #pod
48             #pod =attr time_zone
49             #pod
50             #pod The time zone used with the C<%d> code. The default is C<local>.
51             #pod
52             #pod =cut
53              
54             has time_zone => ( ro, isa=>Str, default => 'local' );
55              
56             around dump_config => sub
57             {
58             my $orig = shift;
59             my $self = shift;
60              
61             my $config = $self->$orig;
62             $config->{+__PACKAGE__} = {
63             time_zone => $self->time_zone,
64             };
65              
66             return $config;
67             };
68              
69             # -- private methods
70              
71             # The sub generated by String::Formatter can't be used as a method directly.
72             sub _format_string
73             {
74 30     30   165 my $self = shift;
75              
76 30         371 _format_string_sub(@_, $self);
77             } # end _format_string
78              
79             sub _get_changes {
80 20     20   97 my $self = shift;
81              
82             # parse changelog to find changes for this release
83 20         878 my $cl_name = $self->changelog;
84 20     19   285 my $changelog = first { $_->name eq $cl_name } @{ $self->zilla->files };
  19         1079  
  20         657  
85 20 100       1933 unless ($changelog) {
86 1         7 $self->log("WARNING: Unable to find $cl_name");
87 1         367 return '';
88             }
89 19         621 my $newver = $self->zilla->version;
90             $changelog->content =~ /
91             ^\Q$newver\E(?![_.]*[0-9]).*\n # from line beginning with version number
92             ( (?: (?> .* ) (?:\n|\z) )*? ) # capture as few lines as possible
93             (?: (?> \s* ) ^\S | \z ) # until non-indented line or EOF
94 19 100       780 /xm or do {
95 1         79 $self->log("WARNING: Unable to find $newver in $cl_name");
96 1         258 return '';
97             };
98              
99 18         11838 (my $changes = $1) =~ s/^\s*\n//; # Remove leading blank lines
100              
101 18 100       152 if (length $changes) {
102 17         243 $changes =~ s/\s*\z/\n/; # Change trailing whitespace to a single newline
103             } else {
104 1         7 $self->log("WARNING: No changes listed under $newver in $cl_name")
105             }
106              
107             # return changes
108 18         409 return $changes;
109             } # end _get_changes
110              
111             1;
112              
113             __END__
114              
115             =pod
116              
117             =encoding UTF-8
118              
119             =head1 NAME
120              
121             Dist::Zilla::Role::Git::StringFormatter - Provide a String::Formatter for commit messages
122              
123             =head1 VERSION
124              
125             version 2.046
126              
127             =head1 DESCRIPTION
128              
129             This role is used within the Git plugins to format strings that may
130             include the changes from the current release.
131              
132             These formatting codes are available:
133              
134             =over 4
135              
136             =item C<%c>
137              
138             The list of changes in the just-released version (read from C<changelog>).
139             It will include lines between the current version and timestamp and
140             the next non-indented line, except that blank lines at the beginning
141             or end are removed. It always ends in a newline unless it is the empty string.
142              
143             =item C<%{dd-MMM-yyyy}d>
144              
145             The current date. You can use any CLDR format supported by
146             L<DateTime>. A bare C<%d> means C<%{dd-MMM-yyyy}d>.
147              
148             =item C<%n>
149              
150             a newline
151              
152             =item C<%N>
153              
154             the distribution name
155              
156             =item C<%{-TRIAL}t>
157              
158             Expands to -TRIAL (or any other supplied string) if this is a trial
159             release, or the empty string if not. A bare C<%t> means C<%{-TRIAL}t>.
160              
161             =item C<%v>
162              
163             the distribution version
164              
165             =item C<%V>
166              
167             The distribution version, but with a leading C<v> removed if it exists.
168              
169             =back
170              
171             =head1 ATTRIBUTES
172              
173             =head2 changelog
174              
175             The filename of your F<Changes> file. (Must be provided by the class
176             that consumes this role.)
177              
178             =head2 time_zone
179              
180             The time zone used with the C<%d> code. The default is C<local>.
181              
182             =head1 SUPPORT
183              
184             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Git>
185             (or L<bug-Dist-Zilla-Plugin-Git@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Git@rt.cpan.org>).
186              
187             There is also a mailing list available for users of this distribution, at
188             L<http://dzil.org/#mailing-list>.
189              
190             There is also an irc channel available for users of this distribution, at
191             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
192              
193             =head1 AUTHOR
194              
195             Jerome Quelin
196              
197             =head1 COPYRIGHT AND LICENCE
198              
199             This software is copyright (c) 2009 by Jerome Quelin.
200              
201             This is free software; you can redistribute it and/or modify it under
202             the same terms as the Perl 5 programming language system itself.
203              
204             =cut