source: perl/lib/BarnOwl/ModuleLoader.pm @ 48609ce

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 48609ce was 4c2ec6c, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Adding a reload-modules command
  • Property mode set to 100644
File size: 2.2 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::ModuleLoader;
5
6use lib (BarnOwl::get_data_dir() . "/modules/");
7use PAR (BarnOwl::get_data_dir() . "/modules/*.par");
8use PAR ($ENV{HOME} . "/.owl/modules/*.par");
9
10sub load_all {
11    my %modules;
12    my @modules;
13   
14    for my $dir ( BarnOwl::get_data_dir() . "/modules",
15                  $ENV{HOME} . "/.owl/modules" ) {
16        opendir(my $dh, $dir) or next;
17        while(defined(my $f = readdir($dh))) {
18            next if $f =~ /^\./;
19            if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
20                $modules{$1} = 1;
21            } elsif(-d "$dir/$f" && -d "$dir/$f/lib") {
22                push @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
23                $modules{$f} = 1;
24            }
25        }
26        @modules = grep /\.par$/, readdir($dh);
27        closedir($dh);
28        for my $mod (@modules) {
29            my ($class) = ($mod =~ /^(.+)\.par$/);
30            $modules{$class} = 1;
31        }
32    }
33    for my $class (keys %modules) {
34        if(!defined eval "use BarnOwl::Module::$class") {
35            BarnOwl::error("Unable to load module $class: $!") if $!;
36            BarnOwl::error("Unable to load module $class: $@") if $@;
37        }
38    }
39
40    $BarnOwl::Hooks::startup->add(\&register_keybindings);
41}
42
43sub register_keybindings {
44    BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, {
45                           summary => 'Reload all modules',
46                           usage   => 'reload',
47                           description => q{Reloads all modules located in ~/.owl/modules and the system modules directory}
48                          });
49}
50
51sub reload {
52    my $class = shift;
53    for my $m (keys %INC) {
54        delete $INC{$m} if $m =~ m{^BarnOwl/};
55    }
56    # Restore core modules from perlwrap.pm
57    $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm
58                         BarnOwl/Message.pm BarnOwl/Style.pm));
59
60    $BarnOwl::Hooks::startup->clear;
61    local $SIG{__WARN__} = \&squelch_redefine;
62    $class->load_all;
63    $BarnOwl::Hooks::startup->run(1);
64    BarnOwl::startup() if *BarnOwl::startup{CODE};
65}
66
67sub squelch_redefine {
68    my $warning = shift;
69    warn $warning unless $warning =~ /^Subroutine .+ redefined at/;
70}
71
721;
Note: See TracBrowser for help on using the repository browser.