From: Matt S Trout Date: Wed, 19 Feb 2014 21:12:40 +0000 (+0000) Subject: refactor Lib::FS X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dd866f636c0c186462c15a9e8eca4b08babbc85b;p=scpubgit%2FDKit.git refactor Lib::FS --- diff --git a/lib/DX/Lib/FS.pm b/lib/DX/Lib/FS.pm index 1cdc1c8..de452bc 100644 --- a/lib/DX/Lib/FS.pm +++ b/lib/DX/Lib/FS.pm @@ -13,182 +13,190 @@ use Moo; our @RULES = ( [ path_status => [ qw(PS) ], [ member_of => 'PS', \'path_status' ] ], - [ path => [ qw(PS P) ], - [ prop => 'PS', \'path', 'P' ] ], - [ info_prop => [ qw(PS N V) ], - [ exists => [ qw(PSI) ], - [ prop => 'PS', \'info', 'PSI' ], - [ prop => 'PSI', 'N', 'V' ] ] ], - [ mode => [ qw(PS M) ], - [ info_prop => 'PS', \'mode', 'M' ] ], - [ exists_path => [ qw(PS) ], - [ info_prop => 'PS', \'is_directory', \1 ] ], - [ exists_path => [ qw(PS) ], - [ info_prop => 'PS', \'is_file', \1 ] ], - [ is_directory => [ qw(PS) ], - [ info_prop => 'PS', \'is_directory', \1 ] ], - [ is_file => [ qw(PS) ], - [ info_prop => 'PS', \'is_file', \1 ] ], - [ path_status_at => [ 'PS', 'P' ], + + [ _path_status_at => [ qw(PS P) ], [ path_status => 'PS' ], - [ path => qw(PS P) ], + [ prop => PS => \'path' => 'P' ] ], + + [ path_status_at => [ qw(PS P) ], + [ _path_status_at => qw(PS P) ], [ 'cut' ] ], + [ path_status_at => [ 'PS', 'P' ], [ observe => [ 'P' ], sub { DX::Lib::FS::Observation::PathStatus->new( path => $_[0] ) } ], - [ path_status => 'PS' ], - [ path => qw(PS P) ] ], - [ directory_at => [ qw(PS P) ], - [ path_status_at => qw(PS P) ], - [ is_directory => 'PS' ] ], - [ file_at => [ qw(PS P) ], - [ path_status_at => qw(PS P) ], - [ is_file => 'PS' ] ], - [ is_directory => [ qw(PS) ], - [ not => [ exists_path => 'PS' ] ], - [ act => [ 'PS' ], sub { + [ _path_status_at => qw(PS P) ] ], + + [ ps_prop => [ 'P', 'Name', 'Value' ], + [ exists => [ 'PS' ], + [ path_status_at => qw(PS P) ], + [ prop => qw(PS Name Value) ] ] ], + + [ ps_info_prop => [ 'P', 'Name', 'Value' ], + [ exists => [ 'PSI' ], + [ ps_prop => 'P', \'info', 'PSI' ], + [ prop => 'PSI', 'Name', 'Value' ] ] ], + + [ exists_path => [ qw(P) ], + [ exists => [ qw(PSI) ], [ ps_prop => 'P' => \'info' => 'PSI' ] ] ], + + [ _is_directory => [ qw(P) ], + [ ps_info_prop => 'P' => \'is_directory' => \1 ] ], + + [ is_directory => [ qw(P) ], + [ _is_directory => 'P' ] ], + + [ _is_file => [ qw(P) ], + [ ps_info_prop => 'P' => \'is_file' => \1 ] ], + + [ is_file => [ qw(P) ], + [ _is_file => 'P' ] ], + + [ _mode => [ qw(P M) ], + [ ps_info_prop => 'P' => \'mode' => 'M' ] ], + + [ mode => [ qw(P M) ], + [ _mode => qw(P M) ] ], + + [ is_directory => [ 'Path' ], + [ not => [ _is_directory => 'Path' ] ], + [ act => [ 'Path' ], sub { DX::Lib::FS::Action::CreateDirectory->new( - path => $_[0]->path + path => $_[0], ) - } ] ], - [ is_file => [ qw(PS) ], - [ not => [ exists_path => 'PS' ] ], - [ act => [ 'PS' ], sub { + } ], + [ _is_directory => 'Path' ] ], + + [ is_file => [ 'Path' ], + [ not => [ _is_file => 'Path' ] ], + [ act => [ 'Path' ], sub { DX::Lib::FS::Action::CreateFile->new( - path => $_[0]->path + path => $_[0], ) - } ] ], - [ directory_in => [ qw(DirStatus DirName SubDirStatus) ], - [ is_directory => qw(DirStatus) ], - [ exists => [ qw(DirPath) ], - [ path => qw(DirStatus DirPath) ], - [ exists => [ qw(SubDirPath) ], - [ catfile => qw(DirPath DirName SubDirPath) ], - [ directory_at => qw(SubDirStatus SubDirPath) ] ] ] ], - [ file_in => [ qw(DirStatus FileName FileStatus) ], - [ is_directory => qw(DirStatus) ], - [ exists => [ qw(DirPath) ], - [ path => qw(DirStatus DirPath) ], - [ exists => [ qw(FilePath) ], - [ catfile => qw(DirPath FileName FilePath) ], - [ file_at => qw(FileStatus FilePath) ] ] ] ], - [ mode => [ qw(PS M) ], - [ exists => [ qw(A) ], - [ has_action => qw(PS A) ], - [ does => 'A', \'DX::Lib::FS::Action::CreateDirectory' ], - [ react => [ qw(PS M) ], sub { - $_[0]->but(mode => $_[1]); - } ] ], - [ 'cut' ] ], - [ mode => [ qw(PS M) ], - [ exists => [ qw(A) ], + } ], + [ _is_file => 'Path' ] ], + + [ _action_is_creating => [ qw(A) ], + [ does => 'A', \'DX::Lib::FS::Action::CreateDirectory' ] ], + + [ _action_is_creating => [ qw(A) ], + [ does => 'A', \'DX::Lib::FS::Action::CreateFile' ] ], + + [ mode => [ qw(P M) ], + [ exists => [ qw(PS A) ], + [ path_status_at => qw(PS P) ], [ has_action => qw(PS A) ], - [ does => 'A', \'DX::Lib::FS::Action::CreateFile' ], + [ _action_is_creating => 'A' ], [ react => [ qw(PS M) ], sub { $_[0]->but(mode => $_[1]); } ] ], - [ 'cut' ] ], - [ mode => [ qw(PS M) ], - [ exists_path => qw(PS) ], - [ not => [ info_prop => 'PS', \'mode', 'M' ] ], - [ act => [ qw(PS M) ], sub { - DX::Lib::FS::Action::SetPathMode->new( - path_status => $_[0], mode => $_[1] - ) - } ] ], + [ 'cut' ] ], + + [ mode => [ qw(P M) ], + [ exists => [ qw(PS) ], + [ path_status_at => qw(PS P) ], + [ not => [ _mode => qw(P M) ] ], + [ act => [ qw(PS M) ], sub { + DX::Lib::FS::Action::SetPathMode->new( + path_status => $_[0], mode => $_[1] + ) + } ] ], + [ 'cut' ] ], + [ file_content => [ qw(FC) ], [ member_of => 'FC', \'file_content' ] ], - [ file_content_at => [ qw(FC P) ], + + [ _file_content_at => [ qw(FC P) ], [ file_content => 'FC' ], - [ path => qw(FC P) ], + [ prop => 'FC', \'path', 'P' ] ], + + [ file_content_at => [ qw(FC P) ], + [ _file_content_at => qw(FC P) ], [ 'cut' ] ], + [ file_content_at => [ qw(FC P) ], + [ is_file => 'P' ], [ observe => [ 'P' ], sub { DX::Lib::FS::Observation::FileContent->new( path => $_[0] ) } ], - [ file_content => 'FC' ], - [ path => qw(FC P) ] ], - [ file_data => [ qw(FC D) ], [ prop => 'FC', \'data', 'D' ] ], - [ file_content_line => [ qw(FC L) ], + [ _file_content_at => qw(FC P) ] ], + + [ fc_prop => [ qw(P Name Value) ], + [ exists => [ 'FC' ], + [ file_content_at => qw(FC P) ], + [ prop => qw(FC Name Value) ] ] ], + + [ file_data => [ qw(P D) ], [ fc_prop => 'P', \'data', 'D' ] ], + + [ _contains_line => [ qw(P L) ], + [ is_file => 'P' ], [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ] ], - [ contains_line => [ qw(PS L) ], - [ is_file => 'PS' ], - [ exists => [ qw(FC P) ], - [ path => qw(PS P) ], + [ fc_prop => 'P', \'lines', 'Lines' ], + [ member_of => qw(L Lines) ] ] ], + + [ contains_line => [ qw(P L) ], + [ _contains_line => qw(P L) ] ], + + [ _action_modifying_fc => [ 'A' ], + [ does => 'A' => \'DX::Lib::FS::Action::CreateFile' ] ], + + [ _action_modifying_fc => [ 'A' ], + [ does => 'A' => \'DX::Lib::FS::Action::RewriteFile' ] ], + + [ contains_line => [ qw(P L) ], + [ not => [ _contains_line => qw(P L) ] ], + [ exists => [ qw(FC A) ], [ file_content_at => qw(FC P) ], - [ file_content_line => qw(FC L) ] ] ], - [ file_content_line => [ qw(FC L) ], - [ not => - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ] ], - [ exists => [ 'A' ], - [ has_action => qw(FC A) ], - [ does => 'A' => \'DX::Lib::FS::Action::CreateFile' ] ], - [ react => [ qw(FC L) ], sub { - $_[0]->but(data => $_[0]->data.$_[1]."\n") - } ], - [ 'cut' ] ], - [ file_content_line => [ qw(FC L) ], - [ not => - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ] ], - [ exists => [ 'A' ], [ has_action => qw(FC A) ], - [ does => 'A' => \'DX::Lib::FS::Action::RewriteFile' ] ], - [ react => [ qw(FC L) ], sub { $_[0]->but_add($_[1]) } ], + [ _action_modifying_fc => 'A' ], + [ react => [ qw(FC L) ], sub { + $_[0]->but_add($_[1]) + } ] ], [ 'cut' ] ], - [ file_content_line => [ qw(FC L) ], - [ not => - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ] ], - [ act => [ qw(FC L) ], sub { + + [ contains_line => [ qw(P L) ], + [ not => [ _contains_line => qw(P L) ] ], + [ exists => [ qw(FC) ], + [ file_content_at => qw(FC P) ], + [ act => [ qw(FC L) ], sub { DX::Lib::FS::Action::RewriteFile->new( from => $_[0], - add_lines => [ $_[1] ] - ); - } ], - [ 'cut' ] ], - [ not_file_content_line => [ qw(FC L) ], - [ not => - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ] ] ], - [ not_contains_line => [ qw(PS L) ], - [ is_file => 'PS' ], - [ exists => [ qw(FC P) ], - [ path => qw(PS P) ], - [ file_content_at => qw(FC P) ], - [ not_file_content_line => qw(FC L) ] ] ], - [ not_file_content_line => [ qw(FC L) ], - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ], + )->but_add($_[1]) + } ] ] ], + + [ not_contains_line => [ qw(P L) ], + [ not => [ _contains_line => qw(P L) ] ] ], + + [ _arrange_removal_of => [ qw(FC L) ], [ exists => [ 'A' ], [ has_action => qw(FC A) ], [ does => 'A' => \'DX::Lib::FS::Action::RewriteFile' ] ], - [ react => [ qw(FC L) ], sub { $_[0]->but_remove($_[1]) } ], - [ 'cut' ] ], - [ not_file_content_line => [ qw(FC L) ], - [ exists => [ 'Lines' ], - [ prop => 'FC' => \'lines' => 'Lines' ], - [ member_of => 'L' => 'Lines' ] ], + [ react => [ qw(FC L) ], sub { $_[0]->but_remove($_[1]) } ] ], + + [ _arrange_removal_of => [ qw(FC L) ], [ act => [ qw(FC L) ], sub { DX::Lib::FS::Action::RewriteFile->new( from => $_[0], - remove_lines => { $_[1] => 1 } - ); - } ], + )->but_remove($_[1]); + } ] ], + + [ not_contains_line => [ qw(P L) ], + [ _contains_line => qw(P L) ], + [ exists => [ qw(FC) ], + [ file_content_at => qw(FC P) ], + [ _arrange_removal_of => qw(FC L) ], + ], [ 'cut' ] ], + + [ file_in => [ qw(DirPath FileName FilePath) ], + [ catfile => qw(DirPath FileName FilePath) ], + [ is_directory => 'DirPath' ], + [ is_file => 'FilePath' ] ], ); sub load_into { diff --git a/lib/DX/Lib/FS/Action/CreateFile.pm b/lib/DX/Lib/FS/Action/CreateFile.pm index c05b3ef..37629aa 100644 --- a/lib/DX/Lib/FS/Action/CreateFile.pm +++ b/lib/DX/Lib/FS/Action/CreateFile.pm @@ -45,4 +45,8 @@ sub _do_run { +(path_status => PathStatus->new(path => $self->path)); } +sub but_add { + $_[0]->but(data => $_[0]->data.$_[1]."\n") +} + 1; diff --git a/lib/DX/Lib/FS/Observation/FileContent.pm b/lib/DX/Lib/FS/Observation/FileContent.pm index 84bb263..f0e2bb4 100644 --- a/lib/DX/Lib/FS/Observation/FileContent.pm +++ b/lib/DX/Lib/FS/Observation/FileContent.pm @@ -7,6 +7,7 @@ has path => (is => 'ro', required => 1); sub run { my ($self) = @_; + return () unless -e $self->path; my $data = do { open my $fh, '<', $self->path or die "Couldn't open ${\$self->path}: $!"; local $/;