| 1 | package Devel::IPerl::Kernel::Backend::DevelREPL; | ||
| 2 | $Devel::IPerl::Kernel::Backend::DevelREPL::VERSION = '0.012'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Log::Any qw($log); | ||
| 7 | use Devel::IPerl::ExecutionResult; | ||
| 8 | use Devel::IPerl::ReadLine::String; | ||
| 9 | use Capture::Tiny ':all'; | ||
| 10 | use Moo; | ||
| 11 | |||
| 12 | has repl => ( is => 'lazy' ); | ||
| 13 | |||
| 14 | sub _build_repl { | ||
| 15 | my ($self) = @_; | ||
| 16 | require Devel::REPL; | ||
| 17 | $log->trace('Creating REPL: Devel::REPL'); | ||
| 18 | my $repl = Devel::REPL->new; | ||
| 19 | |||
| 20 | my $term = Devel::IPerl::ReadLine::String->new; | ||
| 21 | $repl->term( $term ); | ||
| 22 | Moo::Role->apply_roles_to_object($repl, 'Devel::IPerl::ReadLine::Role::DevelREPL'); | ||
| 23 | |||
| 24 | # Devel::REPL::Plugin::LexEnv | ||
| 25 | $repl->load_plugin('LexEnv'); | ||
| 26 | # Devel::REPL::Plugin::OutputCache | ||
| 27 | $repl->load_plugin('OutputCache'); | ||
| 28 | |||
| 29 | # Devel::REPL::Plugin::Completion, etc. | ||
| 30 | $repl->load_plugin('Completion'); | ||
| 31 | $repl->no_term_class_warning(1); | ||
| 32 | # Plugin::Completion | ||
| 33 | # do not warn that the ReadLine is not isa | ||
| 34 | # Term::ReadLine::Gnu or Term::ReadLine::Perl | ||
| 35 | $repl->load_plugin($_) for ( | ||
| 36 | 'CompletionDriver::Keywords', # substr, while, etc | ||
| 37 | 'CompletionDriver::LexEnv', # current environment | ||
| 38 | 'CompletionDriver::Globals', # global variables | ||
| 39 | 'CompletionDriver::INC', # loading new modules | ||
| 40 | 'CompletionDriver::Methods', # class method completion | ||
| 41 | ); | ||
| 42 | |||
| 43 | $repl->eval("no strict;"); | ||
| 44 | |||
| 45 | $repl; | ||
| 46 | } | ||
| 47 | |||
| 48 | # return the STDOUT, STDERR, and Devel::REPL's Term::Readline output | ||
| 49 | sub run_line { | ||
| 50 | my ($self, $cmd) = @_; | ||
| 51 | my $repl = $self->repl; | ||
| 52 | $log->tracef('Running command: %s', $cmd); | ||
| 53 | $repl->term->cmd($cmd); | ||
| 54 | my ($stdout, $stderr) = capture { | ||
| 55 | $repl->run_once; | ||
| 56 | }; | ||
| 57 | |||
| 58 | my $exec_result = Devel::IPerl::ExecutionResult->new(); | ||
| 59 | $exec_result->stdout( $stdout ); | ||
| 60 | $exec_result->stderr( $stderr ); | ||
| 61 | |||
| 62 | $exec_result->last_output( $repl->last_output ); | ||
| 63 | $exec_result->results( $repl->results ); | ||
| 64 | |||
| 65 | my $exception; | ||
| 66 | if( defined $self->repl->error ) { | ||
| 67 | $exception = $self->repl->error; | ||
| 68 | $exec_result->status_error; | ||
| 69 | $exec_result->exception_name( $exception->type ); | ||
| 70 | $exec_result->exception_value( $exception->message ); | ||
| 71 | |||
| 72 | # TODO get an actual traceback | ||
| 73 | $exec_result->exception_traceback( [$exception->message] ); | ||
| 74 | } else { | ||
| 75 | # no exception | ||
| 76 | $exec_result->status_ok; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $exec_result; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | __END__ |