| 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 (BarnOwl::get_config_dir() . "/modules/*.par"); |
|---|
| 9 | |
|---|
| 10 | our %modules; |
|---|
| 11 | |
|---|
| 12 | sub load_all { |
|---|
| 13 | my $class = shift; |
|---|
| 14 | $class->rescan_modules; |
|---|
| 15 | PAR::reload_libs(); |
|---|
| 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 { |
|---|
| 27 | PAR->import(BarnOwl::get_data_dir() . "/modules/*.par"); |
|---|
| 28 | PAR->import(BarnOwl::get_config_dir() . "/modules/*.par"); |
|---|
| 29 | my @modules; |
|---|
| 30 | |
|---|
| 31 | %modules = (); |
|---|
| 32 | |
|---|
| 33 | my @moddirs = (); |
|---|
| 34 | push @moddirs, BarnOwl::get_data_dir() . "/modules"; |
|---|
| 35 | push @moddirs, BarnOwl::get_config_dir() . "/modules"; |
|---|
| 36 | |
|---|
| 37 | for my $dir (@moddirs) { |
|---|
| 38 | opendir(my $dh, $dir) or next; |
|---|
| 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") { |
|---|
| 44 | unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
|---|
| 45 | $modules{$f} = 1; |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 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 | } |
|---|
| 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}; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 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 | |
|---|
| 75 | local $SIG{__WARN__} = \&squelch_redefine; |
|---|
| 76 | |
|---|
| 77 | if(defined $parfile) { |
|---|
| 78 | PAR::reload_libs($parfile); |
|---|
| 79 | $class->run_startup_hooks(); |
|---|
| 80 | } elsif(!defined eval "use BarnOwl::Module::$module") { |
|---|
| 81 | BarnOwl::error("Unable to load module $module: \n$@\n") if $@; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | sub reload_module_cmd { |
|---|
| 86 | my $class = shift; |
|---|
| 87 | shift; # Command |
|---|
| 88 | my $module = shift; |
|---|
| 89 | unless(defined($module)) { |
|---|
| 90 | die("Usage: reload-module [MODULE]"); |
|---|
| 91 | }; |
|---|
| 92 | $class->reload_module($module); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sub register_keybindings { |
|---|
| 96 | BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, { |
|---|
| 97 | summary => 'Reload all modules', |
|---|
| 98 | usage => 'reload-modules', |
|---|
| 99 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
|---|
| 100 | }); |
|---|
| 101 | BarnOwl::new_command('reload-module', sub {BarnOwl::ModuleLoader->reload_module_cmd(@_)}, { |
|---|
| 102 | summary => 'Reload one module', |
|---|
| 103 | usage => 'reload-module [MODULE]', |
|---|
| 104 | description => q{Reloads a single module located in ~/.owl/modules or the system modules directory} |
|---|
| 105 | }); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 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)); |
|---|
| 116 | $class->run_startup_hooks(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | sub run_startup_hooks { |
|---|
| 120 | my $class = shift; |
|---|
| 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; |
|---|
| 129 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | 1; |
|---|