1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
package CPAN::Changes::Release;
use strict;
use warnings;
our $VERSION = '0.500005';
$VERSION =~ tr/_//d;
use Moo;
with 'CPAN::Changes::HasEntries';
has version => (is => 'rw');
has date => (is => 'rw');
has note => (is => 'rw');
has line => (is => 'ro');
around BUILDARGS => sub {
my ($orig, $class, @args) = @_;
my $args = $class->$orig(@args);
if (my $changes = delete $args->{changes}) {
if ($args->{entries}) {
die "Mixing back-compat interface with new interface not allowed";
}
$args->{entries} = [];
for my $group (sort keys %$changes) {
my @entries = @{$changes->{$group}};
if ($group eq '') {
push @{$args->{entries}}, @entries;
}
else {
my $entry = CPAN::Changes::Entry->new(
text => $group,
entries => \@entries,
);
push @{$args->{entries}}, $entry;
}
}
}
$args;
};
sub serialize {
my ($self, %args) = @_;
my $indents = $args{indents} || ['', ' ', ''];
my $styles = $args{styles} || ['', '[]'];
my $width = $args{width} || 75;
my $out = $indents->[0] . $styles->[0] . $self->version;
if ($self->date || $self->note) {
$out .= ' ' . join ' ', (grep { defined } $self->date, $self->note);
}
$out . "\n";
}
around serialize => sub {
my ($orig, $self, %args) = @_;
$args{indents} ||= ['', ' ', ''];
$args{styles} ||= ['', '[]'];
$args{width} ||= 75;
if (my $sort = $args{group_sort}) {
my $entries = $self->_sorted_groups($sort);
$self = $self->clone(entries => $entries);
}
$self->$orig(%args);
};
sub changes {
my ($self, $group) = @_;
if (defined $group) {
return $self->get_group($group)->changes;
}
else {
return { map { $_ => $self->get_group($_)->changes } $self->groups };
}
}
sub add_changes {
my $self = shift;
my %opts;
if (@_ > 1 && ref $_[0] eq 'HASH') {
%opts = %{ +shift };
}
$self->get_group($opts{group} || '')->add_changes(@_);
}
sub set_changes {
my $self = shift;
my %opts;
if (@_ > 1 && ref $_[0] eq 'HASH') {
%opts = %{ +shift };
}
$self->get_group($opts{group} || '')->set_changes(@_);
}
sub clear_changes {
$_[0]->entries([]);
}
sub groups {
my ($self, %args) = @_;
my $sort = $args{sort} || sub { sort @_ };
my %groups;
for my $entry ( @{ $self->entries } ) {
if ($entry->has_entries) {
$groups{$entry->text}++;
}
else {
$groups{''}++;
}
}
return $sort->(keys %groups);
}
sub add_group {
my ($self, @groups) = @_;
push @{ $self->entries }, map { CPAN::Changes::Entry->new(text => $_) } @groups;
}
sub delete_group {
my ($self, @groups) = @_;
my @entries = @{ $self->entries };
for my $name (@groups) {
@entries = grep { $_->text ne $name } @entries;
}
$self->entries(\@entries);
}
# this is nonsense, but try to emulate. if nothing has entries, then there
# are no "groups", so leave everything.
sub delete_empty_groups {
my ($self) = @_;
my @entries = grep { $_->has_entries } @{ $self->entries };
return
if !@entries;
$self->entries(\@entries);
}
sub get_group {
my ($self, $name) = @_;
require CPAN::Changes::Group;
if (defined $name && length $name) {
my ($entry) = grep { $_->text eq $name } @{ $self->entries };
$entry ||= $self->add_entry($name);
return CPAN::Changes::Group->new(_entry => $entry);
}
else {
return CPAN::Changes::Group->new(_entry => $self);
}
}
sub attach_group {
my ($self, $group) = @_;
my $entry = $group->_maybe_entry;
my $text = $entry->text;
my $entries = $self->entries;
if ($text eq '') {
$self->add_entry( @{ $entry->entries } );
}
elsif (my ($found) = grep { $_->text eq $text } @$entries) {
$found->add_entry( @{ $entry->entries } );
}
else {
$self->add_entry( $entry );
}
}
sub group_values {
my ($self, @groups) = @_;
return map { $self->get_group($_) } $self->groups(@groups);
}
sub _sorted_groups {
my ($self, $sort_function) = @_;
my @groups = grep { $_->has_entries } @{ $self->entries };
my @bare = grep { !$_->has_entries } @{ $self->entries };
return \@bare
if !@groups;
my %entries = map { $_->text => [$_] } @groups;
$entries{''} = \@bare
if @bare;
my @sorted = $sort_function->(keys %entries);
return [ map { @{ $entries{$_} || [] } } @sorted ];
}
1;
__END__
=head1 NAME
CPAN::Changes::Release - A release in a CPAN Changes file
=head1 SYNOPSIS
my $release = CPAN::Changes::Release->new(
version => '0.01',
date => '2015-07-20',
);
$release->add_entry('This is a change');
=head1 DESCRIPTION
A changelog is made up of one or more releases. This object provides access to
all of the key data that embodies a release including the version number, date
of release, and all of the changelog information lines.
=head1 ATTRIBUTES
=head2 version
The version number of the release.
=head2 date
The date for the release.
=head2 note
The note attached to the release.
=head2 entries
An array ref of L<entries|CPAN::Changes::Entry> in the release.
=head2 line
The line number that the release starts at.
=head1 METHODS
=head2 serialize
Returns the changes entry for the release in string form.
=head2 clone
Returns a new release object with the same data. Can be given any attributes to
set them differently in the new object.
=head2 has_entries
Returns true if there are changes entries in this release.
=head2 find_entry
Accepts a string or a regex, returns a matching entry object.
=head2 add_entry
Adds a changes entry. Accepts a changes entry object or a string.
=head2 remove_entry
Given a string or a changes entry object, removes the entry from the release.
=head1 LEGACY METHODS
=head2 changes
=head2 add_changes
=head2 set_changes
=head2 clear_changes
=head2 groups
=head2 add_group
=head2 delete_group
=head2 delete_empty_groups
=head2 get_group
=head2 attach_group
=head2 group_values
=head1 SEE ALSO
=over 4
=item * L<CPAN::Changes>
=back
=head1 AUTHORS
See L<CPAN::Changes> for authors.
=head1 COPYRIGHT AND LICENSE
See L<CPAN::Changes> for the copyright and license.
=cut
|