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;
2             # git description: 0.006-1-g35a750b
3              
4             # ABSTRACT: add the results of `git describe` (roughly) to your main module
5             $Dist::Zilla::Plugin::Git::Describe::VERSION = '0.007';
6 3     3   5587986 use Moose;
  3         5  
  3         19  
7             with(
8             'Dist::Zilla::Role::FileMunger',
9             'Dist::Zilla::Role::PPI',
10             );
11              
12 3     3   12899 use Git::Wrapper;
  3         3  
  3         58  
13 3     3   11 use Try::Tiny;
  3         4  
  3         201  
14 3     3   11 use List::Util 1.33 'all';
  3         82  
  3         149  
15              
16 3     3   13 use namespace::autoclean;
  3         5  
  3         25  
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 255216 my ($self) = @_;
48              
49 3         170 my $file = $self->zilla->main_module;
50              
51 3         6220 require PPI::Document;
52 3         256807 my $document = $self->ppi_document_for_file($file);
53              
54 3 50       8030 return unless my $package_stmts = $document->find('PPI::Statement::Package');
55              
56 3         1235 my $git = Git::Wrapper->new( $self->zilla->root );
57 3         261 my @lines = $git->describe({ long => 1, always => 1 });
58              
59 3         19800 my $desc = $lines[0];
60              
61 3         12 my %seen_pkg;
62              
63 3         17 for my $stmt (@$package_stmts) {
64 3         68 my $package = $stmt->namespace;
65              
66 3 50       191 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       31 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       376 my $perl = $self->on_package_line
77             ? " # git description: $desc"
78             : "\n# git description: $desc\n";
79              
80 3         45 my $version_doc = PPI::Document->new(\$perl);
81 3         1437 my @children = $version_doc->children;
82              
83 3         55 $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   1750 unless all { $stmt->insert_after($_->clone) } reverse @children;
  5         223  
91             }
92              
93 3         295 $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.007
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 ATTRIBUTES
126              
127             =head2 on_package_line
128              
129             If true, then the comment is added to the same line as the package declaration.
130             Otherwise, it is added on its own line, with an additional blank line following it.
131             Defaults to false.
132              
133             =head1 SEE ALSO
134              
135             L<PodVersion|Dist::Zilla::Plugin::PkgVersion>
136              
137             =head1 AUTHOR
138              
139             Ricardo Signes <rjbs@cpan.org>
140              
141             =head1 CONTRIBUTOR
142              
143             =for stopwords Karen Etheridge
144              
145             Karen Etheridge <ether@cpan.org>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2012 by Ricardo Signes.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut
155              
156             __END__
157              
158             #pod =head1 SEE ALSO
159             #pod
160             #pod L<PodVersion|Dist::Zilla::Plugin::PkgVersion>
161             #pod
162             #pod =cut