Move to Moo for fast bootstrapping.
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Script.pm
index dc24a6a..dcc6245 100644 (file)
@@ -1,50 +1,95 @@
 package Devel::REPL::Script;
 
-use Moose;
+use Moo;
 use Devel::REPL;
 use File::HomeDir;
 use File::Spec;
-use namespace::clean -except => [ qw(meta) ];
-
-with 'MooseX::Getopt';
+use vars qw($CURRENT_SCRIPT);
+use namespace::sweep;
+use Getopt::Long;
+use MooX::Types::MooseLike::Base qw(Str InstanceOf);
+use Module::Load ();
+use Carp qw(confess);
 
 has 'rcfile' => (
-  is => 'ro', isa => 'Str', required => 1, default => sub { 'repl.rc' },
+  is => 'rw',
+  isa => Str,
+  required => 1,
+);
+
+has 'profile' => (
+  is       => 'rw',
+  isa      => Str,
+  required => 1,
 );
 
 has '_repl' => (
-  is => 'ro', isa => 'Devel::REPL', required => 1,
+  is => 'ro', isa => InstanceOf('Devel::REPL'), required => 1,
   default => sub { Devel::REPL->new() }
 );
 
+sub new_with_options {
+  my ($class) = @_;
+
+  my $rcfile  = 'repl.rc';
+  my $profile = $ENV{DEVEL_REPL_PROFILE} || 'Default';
+  GetOptions(
+      'rcfile=s'  => \$rcfile,
+      'profile=s' => \$profile,
+  );
+  $class->new(profile => $profile, rcfile => $rcfile);
+}
+
 sub BUILD {
   my ($self) = @_;
-  $self->load_rcfile;
+  $self->load_profile($self->profile);
+  $self->load_rcfile($self->rcfile);
 }
 
-sub load_rcfile {
-  my ($self) = @_;
+sub load_profile {
+  my ($self, $profile) = @_;
+  $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
+  Module::Load::load($profile);
+  confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
+    unless $profile->does('Devel::REPL::Profile');
+  $profile->new->apply_profile($self->_repl);
+}
 
-  my $rc_file = $self->rcfile;
+sub load_rcfile {
+  my ($self, $rc_file) = @_;
 
   # plain name => ~/.re.pl/${rc_file}
   if ($rc_file !~ m!/!) {
     $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
   }
 
-  if (-r $rc_file) {
-    open RCFILE, '<', $rc_file || die "Couldn't open ${rc_file}: $!";
-    my $rc_data;
-    { local $/; $rc_data = <RCFILE>; }
-    close RCFILE; # Don't care if this fails
-    $self->eval_rcdata($rc_data);
-    warn "Error executing rc file ${rc_file}: $@\n" if $@;
+  $self->apply_script($rc_file);
+}
+
+sub apply_script {
+  my ($self, $script, $warn_on_unreadable) = @_;
+
+  if (!-e $script) {
+    warn "File '$script' does not exist" if $warn_on_unreadable;
+    return;
   }
+  elsif (!-r _) {
+    warn "File '$script' is unreadable" if $warn_on_unreadable;
+    return;
+  }
+
+  open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
+  my $rc_data;
+  { local $/; $rc_data = <RCFILE>; }
+  close RCFILE; # Don't care if this fails
+  $self->eval_script($rc_data);
+  warn "Error executing script ${script}: $@\n" if $@;
 }
 
-sub eval_rcdata {
-  my $_REPL = $_[0]->_repl;
-  eval $_[1];
+sub eval_script {
+  my ($self, $data) = @_;
+  local $CURRENT_SCRIPT = $self;
+  $self->_repl->eval($data);
 }
 
 sub run {
@@ -58,4 +103,11 @@ sub import {
   $class->new_with_options->run;
 }
 
+sub current {
+  confess "->current should only be called as class method" if ref($_[0]);
+  confess "No current instance (valid only during rc parse)"
+    unless $CURRENT_SCRIPT;
+  return $CURRENT_SCRIPT;
+}
+
 1;