refactor Lib::FS
[scpubgit/DKit.git] / lib / DX / Lib / FS.pm
index a11622f..de452bc 100644 (file)
@@ -3,6 +3,9 @@ package DX::Lib::FS;
 use DX::Lib::FS::Observation::PathStatus;
 use DX::Lib::FS::Action::CreateDirectory;
 use DX::Lib::FS::Action::CreateFile;
+use DX::Lib::FS::Action::SetPathMode;
+use DX::Lib::FS::Observation::FileContent;
+use DX::Lib::FS::Action::RewriteFile;
 use File::Spec;
 use DX::SetOver;
 use Moo;
@@ -10,73 +13,196 @@ 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],
+        )
+      } ],
+    [ _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) ],
+      [ _action_is_creating => 'A' ],
+      [ react => [ qw(PS M) ], sub {
+          $_[0]->but(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 => 'FC' ],
+    [ 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_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' ],
+      [ 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) ],
+      [ has_action => qw(FC A) ],
+      [ _action_modifying_fc => 'A' ],
+      [ react => [ qw(FC L) ], sub {
+          $_[0]->but_add($_[1])
+        } ] ],
+    [ 'cut' ] ],
+
+  [ 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],
+        )->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]) } ] ],
+
+  [ _arrange_removal_of => [ qw(FC L) ],
+    [ act => [ qw(FC L) ], sub {
+        DX::Lib::FS::Action::RewriteFile->new(
+          from => $_[0],
+        )->but_remove($_[1]);
       } ] ],
-  [ 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) ] ] ] ],
+
+  [ 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 {
   my ($self, $solver) = @_;
   $solver->facts->{path_status} = DX::SetOver->new(over => 'path');
+  $solver->facts->{file_content} = DX::SetOver->new(over => 'path');
   $solver->add_predicate(
     catdir => [ qw(DirPath DirName SubDirPath) ],
       [ qw(+ + -) ] => sub {
@@ -95,7 +221,7 @@ sub load_into {
     catfile => [ qw(DirPath FileName FilePath) ],
       [ qw(+ + -) ] => sub {
         my ($vol, $dir) = File::Spec->splitpath($_{DirPath}, 1);
-        my $file_path = File::Spec->catpath($vol, $dir, $_{FilePath});
+        my $file_path = File::Spec->catpath($vol, $dir, $_{FileName});
         +(FilePath => [ value => $file_path ])
       },
       [ qw(- - +) ] => sub {
@@ -104,6 +230,10 @@ sub load_into {
         +(DirPath => [ value => $dir_path ], FileName => [ value => $file ])
       }
   );
+  $solver->add_rule(
+    does => [ qw(Thing RoleName) ],
+      [ constrain => [ qw(Thing RoleName) ], sub { $_[0]->DOES($_[1]) } ]
+  );
   $solver->add_rule(@$_) for @RULES;
 }