move the actual doing stuff code for FS lib into a Guts.pm
[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;
0d1a41d9 4use DX::Lib::FS::Guts;
8c3eab7b 5use Moo;
6
7with 'DX::Role::Action';
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 {
30 $_[0]->but(remove_lines => [ %{$_[0]->remove_lines}, $_[1] => 1 ]);
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) = @_;
0d1a41d9 43 DX::Lib::FS::Guts->rewrite_file($self->path, $self->final_content);
44 return +(file_content => $self->path);
e02a5c0a 45}
8c3eab7b 46
471;