blib/lib/Dist/Zilla/Plugin/ChangelogFromGit/Debian/Sequential.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 134 CC 11 SCAR 39.9
CC
11
Cov
0%
render_changelog
110.0
add_file
6.0
after_release
2.0
CRAP 53.8
1 package Dist::Zilla::Plugin::ChangelogFromGit::Debian::Sequential;
2 {
3 $Dist::Zilla::Plugin::ChangelogFromGit::Debian::Sequential::VERSION = '0.6';
4 }
5  
6 # ABSTRACT: Add changelog entries into debain/changelog
7  
8 use Moose;
9 extends 'Dist::Zilla::Plugin::ChangelogFromGit';
10 with 'Dist::Zilla::Role::AfterRelease';
11  
12 use Debian::Control;
13 use Dpkg::Changelog::Parse;
14  
15 use Dist::Zilla::File::InMemory;
16  
17 use DateTime::Format::Mail;
18 use Text::Wrap qw(wrap fill);
19 use version;
20  
21 override file_name => sub {'debian/changelog'};
22  
23 sub render_changelog {
24 my ($self) = @_;
25  
26 my ($pkg_name, $pkg_distr, $prev_version, $content);
27  
28 my $changelog_file = $self->_get_file('debian/changelog');
29 if ($changelog_file) {
30 my $changelog = changelog_parse(file => $changelog_file->_original_name);
31 ($pkg_name, $pkg_distr, $prev_version) = map {$changelog->{$_}} qw(Source Distribution Version);
32 $content = $changelog_file->content;
33 } else {
34 my $control = Debian::Control->new();
35  
36 my $control_file = $self->_get_file('debian/control');
37 $self->logger->log_fatal("File 'debian/control' does not exist") unless $control_file;
38  
39 $control->read($control_file->_original_name);
40  
41 $pkg_name = $control->source->Source;
42 $pkg_distr = `lsb_release -cs`;
43 chomp($pkg_distr);
44  
45 $prev_version = '0';
46  
47 $content = '';
48 }
49  
50 $prev_version = version->parse($prev_version);
51  
52 local $Text::Wrap::huge = 'wrap';
53 local $Text::Wrap::columns = $self->wrap_column();
54  
55 $self->logger->log_fatal('Unsetted envirement variable DEBFULLNAME') unless $ENV{'DEBFULLNAME'};
56 $self->logger->log_fatal('Unsetted envirement variable DEBEMAIL') unless $ENV{'DEBEMAIL'};
57  
58 foreach my $release ($self->all_releases) {
59 next if $release->has_no_changes && $release->version ne 'HEAD';
60 next if $release->version ne 'HEAD' && $release->version <= version->parse($prev_version);
61  
62 my @changes;
63 foreach my $change (@{$release->changes}) {
64 # Ignoring merges
65 my $log = Git::Repository::Log::Iterator->new($change->change_id);
66 my $parents = $log->next->parent;
67 $log->{'cmd'}->close();
68 next if $parents > 1;
69  
70 my $text = $change->description;
71 chomp($text);
72 push(@changes, fill(' * ', ' ', $text));
73 }
74  
75 my $version = $release->version;
76 $version = $self->zilla->version if $version eq 'HEAD';
77  
78 $content =
79 "$pkg_name ($version) $pkg_distr; urgency=low\n\n"
80 . join("\n\n", @changes ? @changes : ' * No changes') . "\n\n"
81 . " -- $ENV{'DEBFULLNAME'} <$ENV{'DEBEMAIL'}> "
82 . DateTime::Format::Mail->format_datetime($release->date->clone->set_time_zone('local'))
83 . "\n\n$content";
84 }
85  
86 return $content;
87 }
88  
89 sub after_release {
90 my ($self) = @_;
91  
92 my $fn = $self->zilla->root . "/debian/changelog";
93 open(my $fh, '>', $fn) || $self->logger->log_fatal("Cannot write into '$fn': $!");
94 print $fh $self->_get_file('debian/changelog')->content;
95 close($fh);
96 }
97  
98 sub add_file {
99 my ($self, $file) = @_;
100  
101 if (my $added_file = $self->_get_file($file->name)) {
102 $added_file->content($file->content);
103 } else {
104 return $self->SUPER::add_file($file);
105 }
106  
107 return;
108 }
109  
110 sub _get_file {
111 my ($self, $name) = @_;
112  
113 return [grep {$_->name eq $name} @{$self->zilla->files}]->[0];
114 }
115  
116 1;
117  
118 __END__