From 34218fd2af78b254dd424de2289981621ee85f94 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 30 May 2013 02:23:27 -0500 Subject: use ini configuration, not perl --- bin/repl | 30 +++++++++++---------------- lib/App/REPL.pm | 64 +++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/bin/repl b/bin/repl index 9b41333..f41f4fd 100644 --- a/bin/repl +++ b/bin/repl @@ -7,7 +7,7 @@ use App::REPL; my $cfg = "$ENV{HOME}/.replrc"; -my %args = (script => $cfg); +my %args = (config => $cfg); if (!-e $cfg) { print("$cfg not found. Generating a default...\n"); if (open my $fh, '>', $cfg) { @@ -23,20 +23,14 @@ if (!-e $cfg) { App::REPL->new(%args)->run; __DATA__ -load_plugin - 'Interrupt', - - 'FancyPrompt', - 'DataDumper', - - 'Colors', - 'ReadLine', - 'Hints', - 'Packages', - 'LexicalPersistence', - ; - -postlude < App::REPL::Plugin::Defaults->new, }, $class; - my @plugins; - my $postlude; - if (exists $opts{script}) { - my $script = do { - open my $fh, '<', $opts{script} - or die "Can't open $opts{script}: $!"; - local $/ = undef; - <$fh> - }; - local *main::load_plugin = sub { - push @plugins, @_; - }; - local *main::postlude = sub { - $postlude .= $_[0]; - }; - print "Loading configuration from $opts{script}... "; - $self->_eval($script); - print "done\n"; - } - - $self->load_plugin($_) for @{ $opts{plugins} || [] }, @plugins; + $self->load_plugin($_) for @{ $opts{plugins} || [] }; - if (defined $postlude) { - $self->_eval($postlude); + if (defined $opts{config}) { + print "Loading configuration from $opts{config}... "; + $self->load_config($opts{config}); + print "done\n"; } return $self; @@ -48,19 +31,50 @@ sub new { sub load_plugin { my $self = shift; - my ($plugin) = @_; + my ($plugin, $opts) = @_; if (!blessed($plugin)) { $plugin = compose_module_name("App::REPL::Plugin", $plugin); use_package_optimistically($plugin); die "$plugin is not a valid plugin" unless $plugin->isa("App::REPL::Plugin"); - $plugin = $plugin->new; + $plugin = $plugin->new(%$opts); } push @{ $self->{plugins} }, $plugin; } +sub load_config { + my $self = shift; + my ($file) = @_; + + my $data = Config::INI::Reader::Ordered->new->read_file($file); + + my $root_config; + for my $section (@$data) { + my ($name, $data) = @$section; + if ($name eq '_') { + $root_config = $data; + } + else { + $self->load_plugin($name => $data); + } + } + + for my $line (sort grep { /^script_line/ } keys %$root_config) { + $self->_eval($root_config->{$line}); + } + + if (defined(my $file = $root_config->{script_file})) { + my $contents = do { + open my $fh, '<', $file or die "Couldn't open $file: $!"; + local $/ = undef; + <$fh> + }; + $self->_eval($contents); + } +} + sub plugins { my $self = shift; -- cgit v1.2.3-54-g00ecf