From: Matt S Trout Date: Fri, 28 Oct 2011 20:22:59 +0000 (+0000) Subject: working remote module loading X-Git-Tag: v0.001001~35 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=799b77f3fc1fa536b274a8e976b3c8351c45ad97;p=scpubgit%2FTak.git working remote module loading --- diff --git a/lib/Tak/ModuleLoader.pm b/lib/Tak/ModuleLoader.pm index 8d5b837..924230b 100644 --- a/lib/Tak/ModuleLoader.pm +++ b/lib/Tak/ModuleLoader.pm @@ -5,13 +5,15 @@ use Moo; has remote => (is => 'ro', required => 1); sub inc_callback { - sub { $self->maybe_load_module($_[1]) } + my ($self) = @_; + sub { return if our $In_Reentry; $self->maybe_load_module($_[1]) } } sub maybe_load_module { my ($self, $module) = @_; - my $result = $self->remote->blocking_request(source_for => $module); - if (my $code = $result->{code}) { + local our $In_Reentry = 1; + my ($status, $code) = $self->remote->blocking_request(source_for => $module); + if ($status eq 'RESULT') { open my $fh, '<', \$code; return $fh; } diff --git a/lib/Tak/ModuleSender.pm b/lib/Tak/ModuleSender.pm new file mode 100644 index 0000000..e565a26 --- /dev/null +++ b/lib/Tak/ModuleSender.pm @@ -0,0 +1,16 @@ +package Tak::ModuleSender; + +use IO::All; +use Moo; + +sub handle_source_for { + my ($self, $module) = @_; + my $io = io->dir("$ENV{HOME}/perl5/lib/perl5")->catfile($module); + unless ($io->exists) { + return FAILURE => undef; + } + my $code = $io->all; + return RESULT => $code; +} + +1; diff --git a/takc b/takc index d265345..d9db431 100644 --- a/takc +++ b/takc @@ -3,6 +3,8 @@ use Tak::JSONChannel; use Tak::Router; use IPC::Open2; use Tak::Remote; +use Tak::ModuleSender; +use Tak::ServiceManager; use Term::ReadLine; my $pid = open2(my $out, my $in, $^X, qw(-Ilib takd)) @@ -15,6 +17,11 @@ my $channel = Tak::JSONChannel->new( my $router = Tak::Router->new( channel => $channel, + local_request_handlers => { + MODULE_SENDER => Tak::ServiceManager->new( + service => Tak::ModuleSender->new + ) + }, ); my $remote = Tak::Remote->new( diff --git a/takd b/takd index 4dea863..e29e164 100644 --- a/takd +++ b/takd @@ -2,11 +2,16 @@ use strictures 1; use Tak::JSONChannel; use Tak::ServiceManager; use Tak::EvalService; +use Tak::ModuleLoader; use Tak::Router; +use Tak::Remote; + +open my $stdin, '<&', \*STDIN; +open my $stdout, '>&', \*STDOUT; my $channel = Tak::JSONChannel->new( - read_fh => \*STDIN, - write_fh => \*STDOUT + read_fh => $stdin, + write_fh => $stdout, ); my $eval = Tak::ServiceManager->new( @@ -20,4 +25,18 @@ my $router = Tak::Router->new( } ); +my $remote = Tak::Remote->new( + router => $router, + name => 'MODULE_SENDER' +); + +my $loader = Tak::ModuleLoader->new( + remote => $remote +); + +shift @INC; +shift @INC while $INC[0] =~ /^\Q$ENV{HOME}/; + +unshift @INC, $loader->inc_callback; + $router->run;