1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::ModuleLoader; |
---|
5 | |
---|
6 | use lib (BarnOwl::get_data_dir() . "/modules/"); |
---|
7 | use PAR (BarnOwl::get_data_dir() . "/modules/*.par"); |
---|
8 | use PAR ($ENV{HOME} . "/.owl/modules/*.par"); |
---|
9 | |
---|
10 | sub 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(\®ister_keybindings); |
---|
41 | } |
---|
42 | |
---|
43 | sub 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 | |
---|
51 | sub 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 | |
---|
67 | sub squelch_redefine { |
---|
68 | my $warning = shift; |
---|
69 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
70 | } |
---|
71 | |
---|
72 | 1; |
---|