Skip to content

Commit

Permalink
instead of dying when the file is changed, simply warn, and regenerat…
Browse files Browse the repository at this point in the history
…e README
  • Loading branch information
karenetheridge committed Oct 23, 2013
1 parent 2609653 commit 0b43755
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
57 changes: 29 additions & 28 deletions lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm
Expand Up @@ -228,21 +228,7 @@ sub munge_file {
my ($self, $file) = @_;

$self->log_debug([ 'ReadmeAnyFromPod updating contents of %s in dist', $file->name ]);

my $content = $self->get_readme_content();
my $filename = $self->filename;

if ( $file ) {
$file->content( $content );
$self->log("Override $filename in build");
} else {
$file = Dist::Zilla::File::InMemory->new({
content => $content,
name => $filename,
});
$self->add_file($file);
}

$file->content($self->get_readme_content);
return;
}

Expand Down Expand Up @@ -280,6 +266,12 @@ sub _file_from_filename {
die 'no README found (place [ReadmeAnyFromPod] below [Readme] in dist.ini)!';
}

# possibly set more than once, as other plugins modify the source content
has _readme_content => (
is => 'rw', isa => 'Str',
default => '',
);

=method get_readme_content
Get the content of the README in the desired format.
Expand All @@ -289,24 +281,32 @@ Get the content of the README in the desired format.
sub get_readme_content {
my ($self) = shift;

my $file = $self->_file_from_filename($self->source_filename);
if (not $file->does('Dist::Zilla::Role::File::ChangeNotification'))
my $source_file = $self->_file_from_filename($self->source_filename);

my $callcount = 0;
if (not $source_file->does('Dist::Zilla::Role::File::ChangeNotification'))
{
require Dist::Zilla::Role::File::ChangeNotification;
Dist::Zilla::Role::File::ChangeNotification->meta->apply($file);
Dist::Zilla::Role::File::ChangeNotification->meta->apply($source_file);
my $plugin = $self;
$file->on_changed(sub {
my $self = shift;
$plugin->log_fatal('someone tried to munge ' . $self->name
. ' after we read from it. You need to adjust the load order of your plugins.');
$source_file->on_changed(sub {
my ($self, $newcontent) = @_;

# recalculate the content based on the updates, provided it isn't
# ourselves that triggered this call
if ($newcontent ne $plugin->_readme_content)
{
$plugin->log('someone tried to munge ' . $source_file->name . ' after we read from it. Making modifications again...');
$plugin->munge_file($self);
}
});

$file->watch_file;
$source_file->watch_file;
}

my $mmcontent = $file->content;
my $mmcontent = $source_file->content;
my $parser = $_types->{$self->type}->{parser};
my $readme_content = $parser->($mmcontent);
$self->_readme_content($parser->($mmcontent));
}

{
Expand Down Expand Up @@ -372,9 +372,10 @@ case-insensitive. The SYNOPSIS section above gives one example.
When run with C<location = dist>, this plugin runs in the C<FileMunger> phase
to create the new file. If it runs before another C<FileMunger> plugin does,
that happens to modify the input pod (like, say,
L<C<[PodWeaver]>|Dist::Zilla::Plugin::PodWeaver>), the build will fail,
notifying you that you need to adjust your plugin order. Modify your
F<dist.ini> by referencing C<[ReadmeAnyFromPod]> lower down in the file.
L<C<[PodWeaver]>|Dist::Zilla::Plugin::PodWeaver>), the README file contents
will be recalculated, along with a warning that you should modify your
F<dist.ini> by referencing C<[ReadmeAnyFromPod]> lower down in the file (the
build still works, but is less efficient).
=head1 BUGS AND LIMITATIONS
Expand Down
14 changes: 11 additions & 3 deletions t/too-soon.t
Expand Up @@ -5,12 +5,14 @@ use Test::More;
use Test::DZil;
use Path::Tiny;
use Test::Fatal;
use Test::Deep;

use Test::Requires 'Dist::Zilla::Plugin::PodWeaver';

my @module = (
path(qw(source lib Foo.pm)) => <<'MODULE'
package Foo;
# ABSTRACT: stuff
=pod
=head1 SYNOPSIS
Expand All @@ -37,10 +39,16 @@ MODULE
},
);

like(
is(
exception { $tzil->build },
qr{\[ReadmeAnyFromPod\] someone tried to munge lib/Foo.pm after we read from it},
'build dies with a useful error message when the plugin order is wrong',
undef,
'build still proceeds',
);

cmp_deeply(
$tzil->log_messages,
supersetof('[ReadmeAnyFromPod] someone tried to munge lib/Foo.pm after we read from it. Making modifications again...'),
'...but includes a useful warning about plugin ordering',
);
}

Expand Down

0 comments on commit 0b43755

Please sign in to comment.