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.047';
13              
14 12     12   8092 use 5.008;
  12         46  
15 12     12   78 use strict;
  12         32  
  12         433  
16 12     12   79 use warnings;
  12         28  
  12         445  
17              
18 12     12   91 use namespace::autoclean;
  12         23  
  12         131  
19 12     12   1106 use List::Util qw{ first };
  12         37  
  12         982  
20              
21 12     12   84 use Moose::Role;
  12         38  
  12         152  
22 12     12   66608 use MooseX::Has::Sugar;
  12         48  
  12         144  
23 12     12   1432 use Types::Standard qw{ Str };
  12         46  
  12         138  
24              
25             requires qw(changelog log zilla);
26              
27             use String::Formatter method_stringf => {
28             -as => '_format_string_sub',
29             codes => {
30 21         448 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 42         793 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         667 v => sub { $_[0]->zilla->version },
39 29         8367 V => sub { my $v = $_[0]->zilla->version; $v =~ s/\Av//; $v },
  29         1822  
  29         326  
40             },
41 12     12   18870 };
  12         36319  
  12         228  
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 31     31   157 my $self = shift;
75              
76 31         451 _format_string_sub(@_, $self);
77             } # end _format_string
78              
79             sub _get_changes {
80 21     21   94 my $self = shift;
81              
82             # parse changelog to find changes for this release
83 21         953 my $cl_name = $self->changelog;
84 21     20   351 my $changelog = first { $_->name eq $cl_name } @{ $self->zilla->files };
  20         1204  
  21         693  
85 21 100       2266 unless ($changelog) {
86 1         12 $self->log("WARNING: Unable to find $cl_name");
87 1         377 return '';
88             }
89 20         660 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 20 100       876 /xm or do {
95 1         80 $self->log("WARNING: Unable to find $newver in $cl_name");
96 1         244 return '';
97             };
98              
99 19         13942 (my $changes = $1) =~ s/^\s*\n//; # Remove leading blank lines
100              
101 19 100       180 if (length $changes) {
102 18         272 $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 19         403 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.047
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