FS action bugfixes
[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;
4use Moo;
5
6with 'DX::Role::Action';
9854aea0 7with 'DX::Lib::FS::Role::RunOn';
8c3eab7b 8
e02a5c0a 9has from => (is => 'ro', required => 1, handles => [ 'path' ]);
8c3eab7b 10
11has add_lines => (is => 'ro', default => sub { [] });
12
13has remove_lines => (is => 'ro', default => sub { {} });
14
15has final_content => (is => 'lazy', init_arg => undef, builder => sub {
16 my ($self) = @_;
17 my %remove = %{$self->remove_lines};
18 join("\n",
19 (grep !$remove{$_}, $self->from->lines->all),
20 @{$self->add_lines},
21 ''
22 );
23});
24
25sub but_add {
26 $_[0]->but(add_lines => [ @{$_[0]->add_lines}, $_[1] ]);
27}
28
29sub but_remove {
8773d7de 30 $_[0]->but(remove_lines => { %{$_[0]->remove_lines}, $_[1] => 1 });
8c3eab7b 31}
32
33sub expected_effect {
34 my ($self) = @_;
35 +(file_content => DX::Lib::FS::Fact::FileContent->new(
e02a5c0a 36 path => $self->path,
8c3eab7b 37 data => $self->final_content
38 ));
39}
40
e02a5c0a 41sub _do_run {
42 my ($self) = @_;
9854aea0 43 $self->_call_guts(rewrite_file => $self->final_content);
0d1a41d9 44 return +(file_content => $self->path);
e02a5c0a 45}
8c3eab7b 46
471;