diff --git a/Changes b/Changes index 5f9bb26..e6cbea6 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for {{$dist->name}} {{$NEXT}} + - Don't write file if __DATA__ hasn't changed (gh#2) + - Add ->unchanged method (gh#2) 0.01 2024-12-03 17:15:21 -0700 - initial version diff --git a/README.md b/README.md index b67bd90..a551dba 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,32 @@ $writer->update_file; Update the existing Perl source file, OR create a new Perl source file with just the `__DATA__` section. +\[version 0.02\] + +Starting with version 0.02, this method will not write to the file if the content won't change. + +## unchanged + +\[version 0.02\] + +```perl +my $bool = $self->unchanged; +``` + +Returns: + +- \`undef\` + + If </update\_file> hasn't been called yet. + +- \`0\` + + If the last call to </update\_file> modified the file. + +- \`1\` + + If the last call to </update\_file> did not modify the file. + # CAVEATS Added text files will get an added trailing new line if they do not already have diff --git a/lib/Data/Section/Writer.pm b/lib/Data/Section/Writer.pm index d12b2e5..64d4a48 100644 --- a/lib/Data/Section/Writer.pm +++ b/lib/Data/Section/Writer.pm @@ -38,7 +38,7 @@ The name of the Perl source file. If not provided then the source for the calle use Path::Tiny (); use Carp (); - use Class::Tiny qw( perl_filename _files ); + use Class::Tiny qw( perl_filename _files _same ); use Ref::Util qw( is_blessed_ref ); use MIME::Base64 qw(encode_base64); @@ -114,13 +114,18 @@ Returns the C<__DATA__> section. Update the existing Perl source file, OR create a new Perl source file with just the C<__DATA__> section. +[version 0.02] + +Starting with version 0.02, this method will not write to the file if the content won't change. + =cut sub update_file ($self) { my $perl; + my $orig; if(-f $self->perl_filename) { - $perl = $self->perl_filename->slurp_utf8; + $orig = $perl = $self->perl_filename->slurp_utf8; if($perl =~ /^__DATA__/) { $perl = ''; @@ -137,14 +142,51 @@ Update the existing Perl source file, OR create a new Perl source file with just $perl = ''; } + $perl .= $self->render_section; + + if(defined $orig && $orig eq $perl) { + $self->_same(1); + return $self; + } else { + $self->_same(0); + } + # re-write the perl with the - $self->perl_filename->spew_utf8( - $perl . $self->render_section, - ); + $self->perl_filename->spew_utf8($perl); return $self; } +=head2 unchanged + +[version 0.02] + + my $bool = $self->unchanged; + +Returns: + +=over 4 + +=item `undef` + +If hasn't been called yet. + +=item `0` + +If the last call to modified the file. + +=item `1` + +If the last call to did not modify the file. + +=back + +=cut + + sub unchanged ($self) { + return $self->_same; + } + } 1; diff --git a/t/data_section_writer.t b/t/data_section_writer.t index 1d10bf9..cc63fd7 100644 --- a/t/data_section_writer.t +++ b/t/data_section_writer.t @@ -52,10 +52,20 @@ is( }; call render_section => $section; + call unchanged => U(); + call update_file => object { prop isa => 'Data::Section::Writer'; }; + call unchanged => F(); + + call update_file => object { + prop isa => 'Data::Section::Writer'; + }; + + call unchanged => T(); + }, 'add_section_file', );