[0337203] | 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"); |
---|
[be98ba5] | 8 | use PAR (BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
[0337203] | 9 | |
---|
[5987370] | 10 | our %modules; |
---|
| 11 | |
---|
[0337203] | 12 | sub load_all { |
---|
[5987370] | 13 | my $class = shift; |
---|
| 14 | $class->rescan_modules; |
---|
[5f8d8a1] | 15 | PAR::reload_libs(); |
---|
[5987370] | 16 | |
---|
| 17 | for my $mod (keys %modules) { |
---|
| 18 | if(!defined eval "use BarnOwl::Module::$mod") { |
---|
| 19 | BarnOwl::error("Unable to load module $mod: \n$@\n") if $@; |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | $BarnOwl::Hooks::startup->add(\®ister_keybindings); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | sub rescan_modules { |
---|
[e005862] | 27 | PAR->import(BarnOwl::get_data_dir() . "/modules/*.par"); |
---|
| 28 | PAR->import(BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
[0337203] | 29 | my @modules; |
---|
[be98ba5] | 30 | |
---|
[5987370] | 31 | %modules = (); |
---|
| 32 | |
---|
[be98ba5] | 33 | my @moddirs = (); |
---|
| 34 | push @moddirs, BarnOwl::get_data_dir() . "/modules"; |
---|
| 35 | push @moddirs, BarnOwl::get_config_dir() . "/modules"; |
---|
[0337203] | 36 | |
---|
[be98ba5] | 37 | for my $dir (@moddirs) { |
---|
[0337203] | 38 | opendir(my $dh, $dir) or next; |
---|
[836e6263] | 39 | while(defined(my $f = readdir($dh))) { |
---|
| 40 | next if $f =~ /^\./; |
---|
| 41 | if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) { |
---|
| 42 | $modules{$1} = 1; |
---|
| 43 | } elsif(-d "$dir/$f" && -d "$dir/$f/lib") { |
---|
[b4fcc06] | 44 | unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
---|
[836e6263] | 45 | $modules{$f} = 1; |
---|
| 46 | } |
---|
| 47 | } |
---|
[0337203] | 48 | @modules = grep /\.par$/, readdir($dh); |
---|
| 49 | closedir($dh); |
---|
| 50 | for my $mod (@modules) { |
---|
| 51 | my ($class) = ($mod =~ /^(.+)\.par$/); |
---|
| 52 | $modules{$class} = 1; |
---|
| 53 | } |
---|
| 54 | } |
---|
[5987370] | 55 | } |
---|
| 56 | |
---|
| 57 | sub reload_module { |
---|
| 58 | my $class = shift; |
---|
| 59 | my $module = shift; |
---|
| 60 | |
---|
| 61 | $class->rescan_modules(); |
---|
| 62 | |
---|
| 63 | for my $m (keys %INC) { |
---|
| 64 | delete $INC{$m} if $m =~ m{^BarnOwl/Module/$module}; |
---|
[0337203] | 65 | } |
---|
[4c2ec6c] | 66 | |
---|
[5f8d8a1] | 67 | my $parfile; |
---|
| 68 | for my $p (@PAR::PAR_INC) { |
---|
| 69 | if($p =~ m/\Q$module\E[.]par$/) { |
---|
| 70 | $parfile = $p; |
---|
| 71 | last; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[5987370] | 75 | local $SIG{__WARN__} = \&squelch_redefine; |
---|
[5f8d8a1] | 76 | |
---|
| 77 | if(defined $parfile) { |
---|
| 78 | PAR::reload_libs($parfile); |
---|
[41bbb8a] | 79 | $class->run_startup_hooks(); |
---|
[5f8d8a1] | 80 | } elsif(!defined eval "use BarnOwl::Module::$module") { |
---|
[5987370] | 81 | BarnOwl::error("Unable to load module $module: \n$@\n") if $@; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
[a82e091] | 85 | sub reload_module_cmd { |
---|
[5987370] | 86 | my $class = shift; |
---|
| 87 | shift; # Command |
---|
| 88 | my $module = shift; |
---|
| 89 | unless(defined($module)) { |
---|
[a82e091] | 90 | die("Usage: reload-module [MODULE]"); |
---|
[5987370] | 91 | }; |
---|
| 92 | $class->reload_module($module); |
---|
[4c2ec6c] | 93 | } |
---|
| 94 | |
---|
| 95 | sub register_keybindings { |
---|
| 96 | BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, { |
---|
| 97 | summary => 'Reload all modules', |
---|
[b0c8011] | 98 | usage => 'reload-modules', |
---|
[4c2ec6c] | 99 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
---|
| 100 | }); |
---|
[a82e091] | 101 | BarnOwl::new_command('reload-module', sub {BarnOwl::ModuleLoader->reload_module_cmd(@_)}, { |
---|
[5987370] | 102 | summary => 'Reload one module', |
---|
[a82e091] | 103 | usage => 'reload-module [MODULE]', |
---|
[5987370] | 104 | description => q{Reloads a single module located in ~/.owl/modules or the system modules directory} |
---|
| 105 | }); |
---|
[0337203] | 106 | } |
---|
| 107 | |
---|
[836e6263] | 108 | sub reload { |
---|
| 109 | my $class = shift; |
---|
| 110 | for my $m (keys %INC) { |
---|
| 111 | delete $INC{$m} if $m =~ m{^BarnOwl/}; |
---|
| 112 | } |
---|
| 113 | # Restore core modules from perlwrap.pm |
---|
| 114 | $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
| 115 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
[41bbb8a] | 116 | $class->run_startup_hooks(); |
---|
| 117 | } |
---|
[836e6263] | 118 | |
---|
[41bbb8a] | 119 | sub run_startup_hooks { |
---|
| 120 | my $class = shift; |
---|
[836e6263] | 121 | local $SIG{__WARN__} = \&squelch_redefine; |
---|
| 122 | $class->load_all; |
---|
| 123 | $BarnOwl::Hooks::startup->run(1); |
---|
| 124 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | sub squelch_redefine { |
---|
| 128 | my $warning = shift; |
---|
[9729dba] | 129 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
[836e6263] | 130 | } |
---|
| 131 | |
---|
[0337203] | 132 | 1; |
---|