From 0b437556a5ba7749f1f046d14becfce8f5a2909a Mon Sep 17 00:00:00 2001 From: Karen Etheridge Date: Tue, 22 Oct 2013 19:54:20 -0700 Subject: [PATCH] instead of dying when the file is changed, simply warn, and regenerate README --- lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm | 57 ++++++++++++----------- t/too-soon.t | 14 ++++-- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm b/lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm index f8fdc7e..270cddb 100644 --- a/lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm +++ b/lib/Dist/Zilla/Plugin/ReadmeAnyFromPod.pm @@ -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; } @@ -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. @@ -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)); } { @@ -372,9 +372,10 @@ case-insensitive. The SYNOPSIS section above gives one example. When run with C, this plugin runs in the C phase to create the new file. If it runs before another C plugin does, that happens to modify the input pod (like, say, -L|Dist::Zilla::Plugin::PodWeaver>), the build will fail, -notifying you that you need to adjust your plugin order. Modify your -F by referencing C<[ReadmeAnyFromPod]> lower down in the file. +L|Dist::Zilla::Plugin::PodWeaver>), the README file contents +will be recalculated, along with a warning that you should modify your +F by referencing C<[ReadmeAnyFromPod]> lower down in the file (the +build still works, but is less efficient). =head1 BUGS AND LIMITATIONS diff --git a/t/too-soon.t b/t/too-soon.t index a1c59d1..7da9a2c 100644 --- a/t/too-soon.t +++ b/t/too-soon.t @@ -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 @@ -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', ); }