0e7548fc1b3cc33cdf1798b2272bdb9f3b8bed6e
[scpubgit/DKit.git] / lib / DX / Lib / FS / Action / RewriteFile.pm
1 package DX::Lib::FS::Action::RewriteFile;
2
3 use DX::Lib::FS::Fact::FileContent;
4 use Moo;
5
6 with 'DX::Role::Action';
7
8 has from => (is => 'ro', required => 1);
9
10 has add_lines => (is => 'ro', default => sub { [] });
11
12 has remove_lines => (is => 'ro', default => sub { {} });
13
14 has final_content => (is => 'lazy', init_arg => undef, builder => sub {
15   my ($self) = @_;
16   my %remove = %{$self->remove_lines};
17   join("\n",
18     (grep !$remove{$_}, $self->from->lines->all),
19     @{$self->add_lines},
20     ''
21   );
22 });
23
24 sub but_add {
25   $_[0]->but(add_lines => [ @{$_[0]->add_lines}, $_[1] ]);
26 }
27
28 sub but_remove {
29   $_[0]->but(remove_lines => [ %{$_[0]->remove_lines}, $_[1] => 1 ]);
30 }
31
32 sub expected_effect {
33   my ($self) = @_;
34   +(file_content => DX::Lib::FS::Fact::FileContent->new(
35     path => $self->from->path,
36     data => $self->final_content
37   ));
38 }
39
40 sub _do_run { die }
41
42 1;