| 1 | { | ||
| 2 | package Workflow::Aline::ConveyorBelt::Factory; | ||
| 3 | |||
| 4 | use IO::Extended qw(:all); | ||
| 5 | |||
| 6 | use strict; use warnings; | ||
| 7 | |||
| 8 | Class::Maker::class | ||
| 9 | { | ||
| 10 | public => | ||
| 11 | { | ||
| 12 | string => [qw( shall_create mixin_classes )], | ||
| 13 | }, | ||
| 14 | |||
| 15 | private => | ||
| 16 | { | ||
| 17 | string => [qw(can_create)], | ||
| 18 | }, | ||
| 19 | }; | ||
| 20 | |||
| 21 | sub _preinit : method | ||
| 22 | { | ||
| 23 | my $this = shift; | ||
| 24 | |||
| 25 | $this->_can_create( 'ConveyorBelt' ); | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | # ->create( 'Workflow::Aline::ConveyorBelt::Switch', 'Workflow::Aline::Pluggable::OneProximalOneDistal', proximal => $p, distal => $d ) | ||
| 30 | |||
| 31 | sub create_new : method | ||
| 32 | { | ||
| 33 | my $this = shift; | ||
| 34 | |||
| 35 | |||
| 36 | unshift @{ $this->mixin_classes }, "Workflow::Aline::ConveyorBelt::Preliminary"; | ||
| 37 | |||
| 38 | diefln "%S cannot create %S, but only %S", __PACKAGE__, $this->shall_create, $this->_can_create() unless $this->shall_create =~ $this->_can_create; | ||
| 39 | |||
| 40 | unless( defined Class::Maker::Reflection::reflect( $this->shall_create )->{def} ) | ||
| 41 | { | ||
| 42 | my $string = join ' ', @{ $this->mixin_classes }; | ||
| 43 | |||
| 44 | warnfln "%S creates %S (which isa %s)", __PACKAGE__, $this->shall_create, $string; | ||
| 45 | |||
| 46 | my $shall_create = $this->shall_create; | ||
| 47 | |||
| 48 | eval <<END_HERE; | ||
| 49 | |||
| 50 | package $shall_create; | ||
| 51 | |||
| 52 | Class::Maker::class( { isa => [qw($string)] } ); | ||
| 53 | END_HERE | ||
| 54 | |||
| 55 | warn $@ if $@; | ||
| 56 | |||
| 57 | unless( defined Class::Maker::Reflection::reflect( $this->shall_create )->{def} ) | ||
| 58 | { | ||
| 59 | diefln "Class::Maker failed to reflect %s", Class::Maker::Reflection::reflect( $this->shall_create ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | if( $Workflow::Aline::DEBUG->{basic} ) | ||
| 65 | { | ||
| 66 | warnfln "%S will not create %S again, because it already exists", __PACKAGE__, $this->shall_create ; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | warnfln " create %s with new and args: %s", $this->shall_create, scalar @_ ? join( ', ', @_ ) : '<no args>' ; | ||
| 71 | |||
| 72 | return $this->shall_create->new( @_ ); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | { | ||
| 77 | package Workflow::Aline::ConveyorBelt::Preliminary; | ||
| 78 | |||
| 79 | use IO::Extended qw(:all); | ||
| 80 | |||
| 81 | use strict; | ||
| 82 | |||
| 83 | Class::Maker::class | ||
| 84 | { | ||
| 85 | isa => [qw(Workflow::Aline::Pluggable)], | ||
| 86 | |||
| 87 | public => | ||
| 88 | { | ||
| 89 | ref => [qw( master source_dir target_dir )], | ||
| 90 | |||
| 91 | array => [qw( robots )], | ||
| 92 | }, | ||
| 93 | |||
| 94 | private => | ||
| 95 | { | ||
| 96 | ref => [qw( cursor )], | ||
| 97 | }, | ||
| 98 | }; | ||
| 99 | |||
| 100 | sub _postinit : method | ||
| 101 | { | ||
| 102 | my $this = shift; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub run : method | ||
| 106 | { | ||
| 107 | my $this = shift; | ||
| 108 | |||
| 109 | unless( -e $this->source_dir ) | ||
| 110 | { | ||
| 111 | warnfln( __PACKAGE__." source_dir '%s' not found - creating", $this->source_dir ); | ||
| 112 | |||
| 113 | my $ok = $this->source_dir->mkpath; | ||
| 114 | |||
| 115 | warnfln( __PACKAGE__."$ok created" ); | ||
| 116 | |||
| 117 | unless( $ok ) | ||
| 118 | { | ||
| 119 | warnfln( __PACKAGE__." source_dir '%s' not creatable", $this->source_dir ); | ||
| 120 | |||
| 121 | return; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | printfln "ConveyorBelt dir '%s': '%s'", $_, $this->$_ for (qw( source_dir target_dir )); | ||
| 126 | |||
| 127 | $this->signal_robots( 'conveyor_belt_start' ); | ||
| 128 | |||
| 129 | println "Scanning dirs"; | ||
| 130 | |||
| 131 | my @subdirs = map { println; Path::Class::Dir->new( $_ ) } File::Find::Rule->directory->in( $this->source_dir->stringify ); | ||
| 132 | |||
| 133 | # first dir '.' off | ||
| 134 | |||
| 135 | shift @subdirs; | ||
| 136 | |||
| 137 | println "Scanning dirs"; | ||
| 138 | |||
| 139 | indn; | ||
| 140 | |||
| 141 | for( @subdirs ) | ||
| 142 | { | ||
| 143 | # relative requires absolute path's | ||
| 144 | |||
| 145 | my $rel_dir = $_->absolute->relative( $this->source_dir->absolute ); | ||
| 146 | |||
| 147 | println my $final_target = $this->target_dir->subdir( $rel_dir )->absolute; | ||
| 148 | indn; | ||
| 149 | |||
| 150 | foreach ( $this->robots ) | ||
| 151 | { | ||
| 152 | printfln "robot %s", ref $_ if $Workflow::Aline::DEBUG->{robots}; | ||
| 153 | |||
| 154 | last if $_->Class::Listener::signal( 'dir', $this, $final_target ); | ||
| 155 | } | ||
| 156 | |||
| 157 | indb; | ||
| 158 | } | ||
| 159 | |||
| 160 | indb; | ||
| 161 | |||
| 162 | printfln "Scanning files in %s", $this->master->source_dir->stringify; | ||
| 163 | |||
| 164 | my @files = map { Path::Class::File->new( $_ ) } File::Find::Rule->file->in( $this->source_dir->stringify ); | ||
| 165 | |||
| 166 | indn; | ||
| 167 | |||
| 168 | for( @files ) | ||
| 169 | { | ||
| 170 | my $rel_file = $_->absolute->relative( $this->source_dir->absolute ); | ||
| 171 | |||
| 172 | my $src = $_->absolute; | ||
| 173 | |||
| 174 | my $dst = Path::Class::File->new( $this->target_dir, $rel_file )->absolute; | ||
| 175 | |||
| 176 | warnfln "File: %s", $src; | ||
| 177 | |||
| 178 | indn; | ||
| 179 | |||
| 180 | foreach ( $this->robots ) | ||
| 181 | { | ||
| 182 | printfln "robot %s", ref $_ if $Workflow::Aline::DEBUG->{robots}; | ||
| 183 | |||
| 184 | last if $_->Class::Listener::signal( 'file', $this, $src, $dst ); | ||
| 185 | } | ||
| 186 | |||
| 187 | indb; | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | indb; | ||
| 192 | |||
| 193 | $this->signal_robots( 'conveyor_belt_end' ); | ||
| 194 | } | ||
| 195 | |||
| 196 | sub signal_robots : method | ||
| 197 | { | ||
| 198 | my $this = shift; | ||
| 199 | |||
| 200 | my $what = shift || die; | ||
| 201 | |||
| 202 | foreach ( $this->robots ) | ||
| 203 | { | ||
| 204 | $_->Class::Listener::signal( $what, $this->master, $this ); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; |