Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 47 additions & 5 deletions lib/Data/Section/Writer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 = '';
Expand All @@ -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 </update_file> hasn't been called yet.

=item `0`

If the last call to </update_file> modified the file.

=item `1`

If the last call to </update_file> did not modify the file.

=back

=cut

sub unchanged ($self) {
return $self->_same;
}

}

1;
Expand Down
10 changes: 10 additions & 0 deletions t/data_section_writer.t
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down
Loading