Changeset 77

Show
Ignore:
Timestamp:
02/17/06 15:55:16
Author:
miyagawa
Message:

'require' plugin file only when the module is enabled in config file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger.pm

    r73 r77  
    1111 
    1212use base qw( Class::Accessor::Fast ); 
    13 __PACKAGE__->mk_accessors( qw(conf stash update subscription) ); 
     13__PACKAGE__->mk_accessors( qw(conf stash update subscription plugins_path) ); 
    1414 
    1515use Plagger::Date; 
     
    3636        update => Plagger::Update->new, 
    3737        subscription => Plagger::Subscription->new, 
     38        plugins_path => {}, 
    3839    }, $class; 
    3940 
     
    5657 
    5758    if ($self->conf->{plugin_path}) { 
    58         my $rule = File::Find::Rule->new; 
    59            $rule->file; 
    60            $rule->name( qr/^\w[\w\.]*$/ ); 
    61         my @files = $rule->in(@{ $self->conf->{plugin_path} }); 
    62  
    63         for my $file (@files) { 
    64             next if $file =~ /\W(?:\.svn|CVS)\b/; 
    65             eval { require $file }; 
    66             die "loading plugin $file failed: $@" if $@; 
     59        for my $path (@{ $self->conf->{plugin_path} }) { 
     60            my $rule = File::Find::Rule->new; 
     61               $rule->file; 
     62               $rule->name( qr/^\w[\w\.]*$/ ); 
     63            my @files = $rule->in($path); 
     64 
     65            for my $file (@files) { 
     66                next if $file =~ /\W(?:\.svn|CVS)\b/; 
     67                my $pkg = $self->extract_package($file) 
     68                    or die "Can't find package from $file"; 
     69 
     70                (my $base = $file) =~ s!^$path/!!; 
     71                $self->plugins_path->{$pkg} = $file; 
     72            } 
    6773        } 
    6874    } 
     
    7177        $self->load_plugin($plugin) unless $plugin->{disable}; 
    7278    } 
     79} 
     80 
     81sub extract_package { 
     82    my($self, $file) = @_; 
     83 
     84    open my $fh, $file or die "$file: $!"; 
     85    while (<$fh>) { 
     86        /^package (Plagger::Plugin::.*?);/ and return $1; 
     87    } 
     88 
     89    return; 
    7390} 
    7491 
     
    8097    $module = "Plagger::Plugin::$module"; 
    8198 
    82     unless ($module->isa('Plagger::Plugin')) { 
     99    if (my $path = $self->plugins_path->{$module}) { 
     100        eval { require $path } or die $@; 
     101    } else { 
    83102        $module->require or die $@; 
    84103    }