Commit | Line | Data |
59aedffc |
1 | package Devel::REPL::Script; |
2 | |
3 | use Moose; |
4 | use Devel::REPL; |
5 | use File::HomeDir; |
6 | use File::Spec; |
4d33251a |
7 | use vars qw($CURRENT_SCRIPT); |
aa8b7647 |
8 | use namespace::autoclean; |
59aedffc |
9 | |
10 | with 'MooseX::Getopt'; |
11 | |
12 | has 'rcfile' => ( |
b595a818 |
13 | is => 'ro', isa => 'Str', |
14 | default => sub { 'repl.rc' }, |
59aedffc |
15 | ); |
16 | |
4d33251a |
17 | has 'profile' => ( |
cdbe3d31 |
18 | is => 'ro', |
19 | isa => 'Str', |
cdbe3d31 |
20 | default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Default' }, |
4d33251a |
21 | ); |
22 | |
59aedffc |
23 | has '_repl' => ( |
b595a818 |
24 | is => 'ro', isa => 'Devel::REPL', |
59aedffc |
25 | default => sub { Devel::REPL->new() } |
26 | ); |
27 | |
28 | sub BUILD { |
29 | my ($self) = @_; |
4d33251a |
30 | $self->load_profile($self->profile); |
31 | $self->load_rcfile($self->rcfile); |
59aedffc |
32 | } |
33 | |
4d33251a |
34 | sub load_profile { |
35 | my ($self, $profile) = @_; |
36 | $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/; |
37 | Class::MOP::load_class($profile); |
3bcf4eb8 |
38 | confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'" |
39 | unless $profile->does('Devel::REPL::Profile'); |
4d33251a |
40 | $profile->new->apply_profile($self->_repl); |
41 | } |
59aedffc |
42 | |
4d33251a |
43 | sub load_rcfile { |
44 | my ($self, $rc_file) = @_; |
59aedffc |
45 | |
46 | # plain name => ~/.re.pl/${rc_file} |
47 | if ($rc_file !~ m!/!) { |
48 | $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file); |
49 | } |
50 | |
cb3b891f |
51 | $self->apply_script($rc_file); |
59aedffc |
52 | } |
53 | |
cb3b891f |
54 | sub apply_script { |
55 | my ($self, $script, $warn_on_unreadable) = @_; |
fa626608 |
56 | |
cb3b891f |
57 | if (!-e $script) { |
58 | warn "File '$script' does not exist" if $warn_on_unreadable; |
59 | return; |
fa626608 |
60 | } |
cb3b891f |
61 | elsif (!-r _) { |
62 | warn "File '$script' is unreadable" if $warn_on_unreadable; |
63 | return; |
64 | } |
65 | |
66 | open RCFILE, '<', $script or die "Couldn't open ${script}: $!"; |
67 | my $rc_data; |
68 | { local $/; $rc_data = <RCFILE>; } |
69 | close RCFILE; # Don't care if this fails |
70 | $self->eval_script($rc_data); |
71 | warn "Error executing script ${script}: $@\n" if $@; |
fa626608 |
72 | } |
73 | |
cb3b891f |
74 | sub eval_script { |
4d33251a |
75 | my ($self, $data) = @_; |
76 | local $CURRENT_SCRIPT = $self; |
77 | $self->_repl->eval($data); |
59aedffc |
78 | } |
79 | |
80 | sub run { |
81 | my ($self) = @_; |
82 | $self->_repl->run; |
83 | } |
84 | |
85 | sub import { |
86 | my ($class, @opts) = @_; |
87 | return unless (@opts == 1 && $opts[0] eq 'run'); |
88 | $class->new_with_options->run; |
89 | } |
90 | |
4d33251a |
91 | sub current { |
92 | confess "->current should only be called as class method" if ref($_[0]); |
93 | confess "No current instance (valid only during rc parse)" |
94 | unless $CURRENT_SCRIPT; |
95 | return $CURRENT_SCRIPT; |
96 | } |
97 | |
59aedffc |
98 | 1; |