File Coverage

blib/lib/Dist/Zilla/Plugin/Git/Describe.pm
Criterion Covered Total %
statement 35 39 89.7
branch 6 10 60.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 48 57 84.2


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Git::Describe 0.008;
2             # git description: 0.007-6-g8d29db3
3              
4             # ABSTRACT: add the results of `git describe` (roughly) to your main module
5              
6 3     3   10089798 use Moose;
  3         9  
  3         39  
7             with(
8             'Dist::Zilla::Role::FileMunger',
9             'Dist::Zilla::Role::PPI',
10             );
11              
12 3     3   21691 use Git::Wrapper;
  3         7  
  3         76  
13 3     3   21 use Try::Tiny;
  3         8  
  3         285  
14 3     3   24 use List::Util 1.33 'all';
  3         117  
  3         212  
15              
16 3     3   28 use namespace::autoclean;
  3         6  
  3         31  
17              
18             #pod =head1 SYNOPSIS
19             #pod
20             #pod in dist.ini
21             #pod
22             #pod [Git::Describe]
23             #pod
24             #pod =head1 DESCRIPTION
25             #pod
26             #pod This plugin will add the long-form git commit description for the current repo
27             #pod to the dist's main module as a comment. It may change, in the future, to put
28             #pod things in a package variable, or to provide an option.
29             #pod
30             #pod It inserts this in the same place that PkgVersion would insert a version.
31             #pod
32             #pod =attr on_package_line
33             #pod
34             #pod If true, then the comment is added to the same line as the package declaration.
35             #pod Otherwise, it is added on its own line, with an additional blank line following it.
36             #pod Defaults to false.
37             #pod
38             #pod =cut
39              
40             has on_package_line => (
41             is => 'ro',
42             isa => 'Bool',
43             default => 0,
44             );
45              
46             sub munge_files {
47 3     3 0 552871 my ($self) = @_;
48              
49 3         185 my $file = $self->zilla->main_module;
50              
51 3         10665 require PPI::Document;
52 3         328571 my $document = $self->ppi_document_for_file($file);
53              
54 3 50       14632 return unless my $package_stmts = $document->find('PPI::Statement::Package');
55              
56 3         1847 my $git = Git::Wrapper->new( $self->zilla->root );
57 3         481 my @lines = $git->describe({ long => 1, always => 1 });
58              
59 3         40411 my $desc = $lines[0];
60              
61 3         37 my %seen_pkg;
62              
63 3         57 for my $stmt (@$package_stmts) {
64 3         113 my $package = $stmt->namespace;
65              
66 3 50       443 if ($seen_pkg{ $package }++) {
67 0         0 $self->log([ 'skipping package re-declaration for %s', $package ]);
68 0         0 next;
69             }
70              
71 3 50       76 if ($stmt->content =~ /package\s*(?:#.*)?\n\s*\Q$package/) {
72 0         0 $self->log([ 'skipping private package %s in %s', $package, $file->name ]);
73 0         0 next;
74             }
75              
76 3 100       876 my $perl = $self->on_package_line
77             ? " # git description: $desc"
78             : "\n# git description: $desc\n";
79              
80 3         131 my $version_doc = PPI::Document->new(\$perl);
81 3         3435 my @children = $version_doc->children;
82              
83 3         172 $self->log_debug([
84             'adding git description comment to %s in %s',
85             $package,
86             $file->name,
87             ]);
88              
89             Carp::carp("error inserting git description in " . $file->name)
90 3 50   5   4508 unless all { $stmt->insert_after($_->clone) } reverse @children;
  5         738  
91             }
92              
93 3         646 $self->save_ppi_document_to_file($document, $file);
94             }
95              
96             __PACKAGE__->meta->make_immutable;
97             1;
98              
99             =pod
100              
101             =encoding UTF-8
102              
103             =head1 NAME
104              
105             Dist::Zilla::Plugin::Git::Describe - add the results of `git describe` (roughly) to your main module
106              
107             =head1 VERSION
108              
109             version 0.008
110              
111             =head1 SYNOPSIS
112              
113             in dist.ini
114              
115             [Git::Describe]
116              
117             =head1 DESCRIPTION
118              
119             This plugin will add the long-form git commit description for the current repo
120             to the dist's main module as a comment. It may change, in the future, to put
121             things in a package variable, or to provide an option.
122              
123             It inserts this in the same place that PkgVersion would insert a version.
124              
125             =head1 PERL VERSION
126              
127             This module should work on any version of perl still receiving updates from
128             the Perl 5 Porters. This means it should work on any version of perl released
129             in the last two to three years. (That is, if the most recently released
130             version is v5.40, then this module should work on both v5.40 and v5.38.)
131              
132             Although it may work on older versions of perl, no guarantee is made that the
133             minimum required version will not be increased. The version may be increased
134             for any reason, and there is no promise that patches will be accepted to lower
135             the minimum required perl.
136              
137             =head1 ATTRIBUTES
138              
139             =head2 on_package_line
140              
141             If true, then the comment is added to the same line as the package declaration.
142             Otherwise, it is added on its own line, with an additional blank line following it.
143             Defaults to false.
144              
145             =head1 SEE ALSO
146              
147             L<PodVersion|Dist::Zilla::Plugin::PkgVersion>
148              
149             =head1 AUTHOR
150              
151             Ricardo Signes <cpan@semiotic.systems>
152              
153             =head1 CONTRIBUTORS
154              
155             =for stopwords Karen Etheridge Mikko Koivunalho Ricardo Signes
156              
157             =over 4
158              
159             =item *
160              
161             Karen Etheridge <ether@cpan.org>
162              
163             =item *
164              
165             Mikko Koivunalho <mikkoi@cpan.org>
166              
167             =item *
168              
169             Ricardo Signes <rjbs@semiotic.systems>
170              
171             =back
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2012 by Ricardo Signes.
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
181              
182             __END__
183              
184             #pod =head1 SEE ALSO
185             #pod
186             #pod L<PodVersion|Dist::Zilla::Plugin::PkgVersion>
187             #pod
188             #pod =cut