@@ -7,6 +7,7 @@ use Data::Walk;
77use Data::Dumper;
88use File::Path qw( make_path remove_tree) ;
99use Try::Tiny;
10+ use Path::Tiny;
1011
1112with ' BioX::Workflow::Command::Utils::Files::TrackChanges' ;
1213use BioX::Workflow::Command::Utils::Traits qw( ArrayRefOfStrs) ;
519520 $self -> app_log-> fatal($rule_example );
520521}
521522
522- =head3 carry_directives
523-
524- At the beginning of each rule the previous outdir should be the new indir, and the previous OUTPUT should be the new INPUT
525-
526- Stash should be carried over
527-
528- Outdir should be global_attr->outdir/rule_name
529-
530- =cut
531-
532- sub carry_directives {
533- my $self = shift ;
534-
535- $self -> local_attr-> outdir(
536- $self -> global_attr-> outdir . ' /' . $self -> rule_name );
537-
538- return unless $self -> has_p_rule_name;
539-
540- $self -> local_attr-> indir( dclone( $self -> p_local_attr-> outdir ) );
541-
542- if ( $self -> p_local_attr-> has_OUTPUT ) {
543- if ( ref ( $self -> p_local_attr-> OUTPUT ) ) {
544- $self -> local_attr-> INPUT( dclone( $self -> p_local_attr-> OUTPUT ) );
545- }
546- else {
547- $self -> local_attr-> INPUT( $self -> p_local_attr-> OUTPUT );
548- }
549- }
550-
551- $self -> local_attr-> stash( dclone( $self -> p_local_attr-> stash ) );
552- }
553523
554524=head3 template_process
555525
@@ -570,6 +540,7 @@ sub template_process {
570540
571541 my $dummy_sample = $self -> dummy_sample;
572542 my $dummy_texts = $self -> check_iterables( $dummy_sample , [] );
543+
573544 foreach my $sample ( $self -> all_samples ) {
574545 foreach my $text ( @{$dummy_texts } ) {
575546 my $new_text = $text ;
@@ -582,15 +553,6 @@ sub template_process {
582553
583554 $self -> process_obj-> { $self -> rule_name }-> {meta } =
584555 $self -> write_rule_meta(' before_meta' );
585-
586- # return unless $self->use_timestamps;
587- # if ( $self->local_attr->{_modified} ) {
588- # $self->app_log->info(
589- # 'One or more files were modified or are not logged for this rule');
590- # }
591- # else {
592- # $self->app_log->info('Zero files were modified for this rule');
593- # }
594556}
595557
596558sub use_iterables {
@@ -632,12 +594,14 @@ sub check_iterables {
632594 # First check the global for any lists
633595 my $use_iters = $self -> use_iterables;
634596
635- if ( ! $use_iters ) {
597+ $self -> walk_indir_outdir($use_iters );
598+
599+ if ( !$use_iters ) {
636600 $texts = $self -> in_template_process( $sample , $texts );
637601 return $texts ;
638602 }
639603
640- my $all = $use_iters -> [0];
604+ my $all = $use_iters -> [0];
641605 my $elem = $use_iters -> [1];
642606
643607 # #TODO This should be a separate function
@@ -653,7 +617,6 @@ sub check_iterables {
653617 push ( @$texts , $new_text );
654618 }
655619 }
656- # #
657620
658621 return $texts ;
659622}
@@ -673,6 +636,35 @@ sub in_template_process {
673636 return $texts ;
674637}
675638
639+ sub walk_attr {
640+ my $self = shift ;
641+
642+ my $attr = dclone( $self -> local_attr );
643+ $self -> check_indir_outdir($attr );
644+
645+ $DB::single = 2;
646+
647+ $attr -> walk_process_data( $self -> rule_keys );
648+
649+ return $attr ;
650+ }
651+
652+ sub eval_process {
653+ my $self = shift ;
654+
655+ my $attr = $self -> walk_attr;
656+ $attr -> sample( $self -> sample ) if $self -> has_sample;
657+
658+ my $process = $self -> local_rule-> { $self -> rule_name }-> {process };
659+ my $text = $attr -> interpol_directive($process );
660+ $text = clean_text($text );
661+
662+ $self -> walk_FILES($attr );
663+ $self -> clear_files;
664+
665+ return $text ;
666+ }
667+
676668sub get_global_keys {
677669 my $self = shift ;
678670 my @global_keys = ();
@@ -709,45 +701,68 @@ sub get_keys {
709701 $self -> rule_keys( \@global_keys );
710702}
711703
712- sub walk_attr {
713- my $self = shift ;
704+ # #TODO Clean this up and merge with the other walk_iterables
705+ # #TODO Write more tests
706+ sub walk_indir_outdir {
707+ my $self = shift ;
708+ my $use_iters = shift ;
714709
710+ # #TODO This is redundant...
715711 my $attr = dclone( $self -> local_attr );
716- $self -> check_indir_outdir($attr );
717-
718- $DB::single = 2;
712+ my $dummy_sample = $self -> dummy_sample;
713+ $attr -> sample($dummy_sample );
719714
720- $attr -> walk_process_data( $self -> rule_keys );
715+ if ( $attr -> outdir =~ m /\{\$ / ) {
716+ $attr -> walk_process_data( $self -> rule_keys );
717+ }
721718
722- if ( $attr -> create_outdir && ! $attr -> outdir -> is_dir ) {
719+ my $text = $attr -> interpol_directive( $self -> local_attr -> outdir );
723720
724- try {
725- $attr -> outdir-> mkpath;
721+ if ( !$use_iters ) {
722+ foreach my $sample ( $attr -> all_samples ) {
723+ my $new_text = $text ;
724+ $new_text =~ s / $dummy_sample/ $sample / g ;
725+ $new_text = path($new_text )-> absolute if $attr -> coerce_abs_dir;
726+ $new_text = path($new_text ) if !$attr -> coerce_abs_dir;
727+ $self -> decide_create_outdir( $attr , $new_text );
726728 }
727- catch {
728- $self -> app_log-> fatal( " We were not able to make the directory.\n\t "
729- . $attr -> outdir
730- . " \n\t Error: $! " );
731- };
729+ return ;
732730 }
733731
734- return $attr ;
735- }
732+ my $all = $use_iters -> [0] ;
733+ my $elem = $use_iters -> [1];
736734
737- sub eval_process {
738- my $self = shift ;
735+ # #TODO This should be a separate function
736+ my $dummy_iter = $self -> dummy_iterable;
737+ $attr -> $elem ($dummy_iter );
739738
740- my $attr = $self -> walk_attr;
741- $attr -> sample( $self -> sample ) if $self -> has_sample;
739+ # #BUG FIX - If outdirs have iterables this will be bad
742740
743- my $process = $self -> local_rule-> { $self -> rule_name }-> {process };
744- my $text = $attr -> interpol_directive($process );
745- $text = clean_text($text );
741+ foreach my $chunk ( $self -> local_attr-> $all ) {
742+ my $new_text = $text ;
743+ $new_text =~ s / $dummy_iter/ $chunk / g ;
744+ $new_text = path($new_text )-> absolute if $attr -> coerce_abs_dir;
745+ $new_text = path($new_text ) if !$attr -> coerce_abs_dir;
746+ $self -> decide_create_outdir( $attr , $new_text );
747+ }
748+ }
746749
747- $self -> walk_FILES($attr );
748- $self -> clear_files;
750+ sub decide_create_outdir {
751+ my $self = shift ;
752+ my $attr = shift ;
753+ my $dir = shift ;
749754
750- return $text ;
755+ return unless $attr -> create_outdir;
756+ return unless $dir ;
757+
758+ try {
759+ $dir -> mkpath;
760+ }
761+ catch {
762+ $self -> app_log-> fatal( " We were not able to make the directory.\n\t "
763+ . $attr -> outdir
764+ . " \n\t Error: $! " );
765+ };
751766}
752767
753768sub clean_text {
@@ -812,6 +827,8 @@ sub print_rule {
812827 return $print_rule ;
813828}
814829
830+ # #This is not necessary without the use_timestamps
831+ # #But I will leave it in as a placeholder
815832sub print_within_rule {
816833 my $self = shift ;
817834
@@ -851,6 +868,7 @@ sub check_indir_outdir {
851868
852869 # If indir/outdir is specified in the local config
853870 # then we don't evaluate it
871+
854872 foreach my $dir ( ( ' indir' , ' outdir' ) ) {
855873 if ( grep /$dir /, @{ $self -> local_rule_keys } ) {
856874 next ;
@@ -875,4 +893,36 @@ sub check_indir_outdir {
875893
876894}
877895
896+ =head3 carry_directives
897+
898+ At the beginning of each rule the previous outdir should be the new indir, and the previous OUTPUT should be the new INPUT
899+
900+ Stash should be carried over
901+
902+ Outdir should be global_attr->outdir/rule_name
903+
904+ =cut
905+
906+ sub carry_directives {
907+ my $self = shift ;
908+
909+ $self -> local_attr-> outdir(
910+ $self -> global_attr-> outdir . ' /' . $self -> rule_name );
911+
912+ return unless $self -> has_p_rule_name;
913+
914+ $self -> local_attr-> indir( dclone( $self -> p_local_attr-> outdir ) );
915+
916+ if ( $self -> p_local_attr-> has_OUTPUT ) {
917+ if ( ref ( $self -> p_local_attr-> OUTPUT ) ) {
918+ $self -> local_attr-> INPUT( dclone( $self -> p_local_attr-> OUTPUT ) );
919+ }
920+ else {
921+ $self -> local_attr-> INPUT( $self -> p_local_attr-> OUTPUT );
922+ }
923+ }
924+
925+ $self -> local_attr-> stash( dclone( $self -> p_local_attr-> stash ) );
926+ }
927+
8789281;
0 commit comments