rewrite file correctly chaining from create file
[scpubgit/DKit.git] / lib / DX / Lib / FS / Action / RewriteFile.pm
CommitLineData
8c3eab7b 1package DX::Lib::FS::Action::RewriteFile;
2
3use DX::Lib::FS::Fact::FileContent;
e02a5c0a 4use File::stat;
5use File::Copy;
6use Fcntl qw(O_CREAT O_WRONLY);
8c3eab7b 7use Moo;
8
9with 'DX::Role::Action';
10
e02a5c0a 11has from => (is => 'ro', required => 1, handles => [ 'path' ]);
8c3eab7b 12
13has add_lines => (is => 'ro', default => sub { [] });
14
15has remove_lines => (is => 'ro', default => sub { {} });
16
17has final_content => (is => 'lazy', init_arg => undef, builder => sub {
18 my ($self) = @_;
19 my %remove = %{$self->remove_lines};
20 join("\n",
21 (grep !$remove{$_}, $self->from->lines->all),
22 @{$self->add_lines},
23 ''
24 );
25});
26
27sub but_add {
28 $_[0]->but(add_lines => [ @{$_[0]->add_lines}, $_[1] ]);
29}
30
31sub but_remove {
32 $_[0]->but(remove_lines => [ %{$_[0]->remove_lines}, $_[1] => 1 ]);
33}
34
35sub expected_effect {
36 my ($self) = @_;
37 +(file_content => DX::Lib::FS::Fact::FileContent->new(
e02a5c0a 38 path => $self->path,
8c3eab7b 39 data => $self->final_content
40 ));
41}
42
e02a5c0a 43sub _do_run {
44 my ($self) = @_;
45 my $stat = stat($self->path) or die "Couldn't stat ${\$self->path}: $!";
46 my $perms = $stat->mode & 07777;
47 my $new = $self->path.'.new';
48 unlink($new);
49 sysopen my $fh, $new, O_CREAT | O_WRONLY, $perms
50 or die "Couldn't open ${new}: $!";
51 print $fh $self->final_content
52 or die "Couldn't write data to ${new}: $!";
53 close $fh;
54 move($new, $self->path) or die "Couldn't install ${new}: $!";
55 return +(file_content => $self->from);
56}
8c3eab7b 57
581;