[Rt-commit] rt branch, runtime-plugin, updated. rt-3.9.4-168-g0f3ab08

Chia-liang Kao clkao at bestpractical.com
Tue Oct 19 01:57:16 EDT 2010


The branch, runtime-plugin has been updated
       via  0f3ab0843c12e58eafb2c1bfc0e4e0ece9352ab1 (commit)
       via  096370e6887fb7c4ecab95d0ba53250decaa9a33 (commit)
       via  677054ac719b502318b5ed5b604ae628e19c9cde (commit)
       via  8816bf208b522f944c04341348dd1c345b454626 (commit)
       via  c29c3e03b8d0e2dd93efbf7c621f1828e7cb8751 (commit)
       via  deefada19bb4dcc501e222b4740d1f7d674eaa3c (commit)
      from  97d3b2e50f358156b6dc2f9a8f1cf92b7630c30a (commit)

Summary of changes:
 lib/RT.pm.in                 |   49 ++++++++++++++++++++++------
 lib/RT/Plugin.pm             |   71 ++++++++++++++++++++++++++++++++----------
 sbin/rt-test-dependencies.in |    1 +
 t/plugins/probe.t            |   14 ++++++++
 4 files changed, 107 insertions(+), 28 deletions(-)
 create mode 100644 t/plugins/probe.t

- Log -----------------------------------------------------------------
commit deefada19bb4dcc501e222b4740d1f7d674eaa3c
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 22:23:07 2010 +0900

    basic probe

diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index d0f6fba..3bb31e3 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -128,7 +128,6 @@ Takes a name of a given plugin and return its base path.
 
 =cut
 
-
 sub BasePathFor {
     my ($class, $name) = @_;
 
@@ -139,6 +138,23 @@ sub BasePathFor {
     return -d $local_base ? $local_base : $base_base;
 }
 
+=head2 AvailablePlugins($plugin_path)
+
+=cut
+
+sub AvailablePlugins {
+    my ($class, $plugin_path) = @_;
+    my @res;
+    my @paths = $plugin_path ? ($plugin_path) : ($RT::LocalPluginPath, $RT::PluginPath);
+    for my $abs_path (map { <$_/*> } @paths) {
+        my ($dir, $name) = $abs_path =~ m|(.*)/([^/]+)$|;
+        # ensure no cascading
+        next if $class->BasePathFor($name) ne $abs_path;
+        push @res, $name;
+    }
+    return \@res;
+}
+
 =head2 ComponentRoot
 
 Returns the directory this plugin has installed its L<HTML::Mason> templates into
diff --git a/t/plugins/probe.t b/t/plugins/probe.t
new file mode 100644
index 0000000..4b5bd8c
--- /dev/null
+++ b/t/plugins/probe.t
@@ -0,0 +1,32 @@
+#!perl
+use Cwd qw(abs_path);
+use File::Basename qw(basename dirname);
+
+require RT;
+$RT::PluginPath = abs_path(dirname($0)).'/_plugins';
+$RT::LocalPluginPath = abs_path(dirname($0)).'/_plugins_null';
+
+use RT::Test nodb => 1, tests => 7;
+
+is_deeply( RT::Plugin->AvailablePlugins, ['Hello'] );
+__END__
+is_deeply([RT->AvailablePlugins]);
+
+ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
+RT->Config->Set('Plugins',qw(Hello));
+RT->InitPluginPaths;
+RT->Plugins;
+ok(grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
+
+is_deeply([RT->PluginDirs('lib')], ["$RT::PluginPath/Hello/lib"], 'plugin lib dir found');
+
+require RT::Interface::Web::Handler;
+
+is_deeply({RT::Interface::Web::Handler->DefaultHandlerArgs}->{comp_root}[1],
+          ['plugin-Hello', $RT::PluginPath.'/Hello/html']);
+
+# reset
+RT->Config->Set('Plugins',qw());
+RT->InitPluginPaths;
+ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);
+is({RT::Interface::Web::Handler->DefaultHandlerArgs}->{comp_root}[1][0],'standard');

commit c29c3e03b8d0e2dd93efbf7c621f1828e7cb8751
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Tue Oct 19 13:14:23 2010 +0900

    Do INC maintenance for enabled plugin only.

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index e05543f..306c3ad 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -651,6 +651,7 @@ sub InitPlugins {
     require RT::Plugin;
     foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
         my $plugin = RT::Plugin->new(name => $plugin_name);
+        $plugin->Enable;
         $plugin_name->require;
         die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);
         push @plugins, $plugin;
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index 3bb31e3..bdc37fc 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -51,6 +51,7 @@ use strict;
 
 package RT::Plugin;
 use File::ShareDir;
+use Class::Accessor "antlers";
 
 =head1 NAME
 
@@ -67,11 +68,18 @@ it cares about is 'name', the name of this plugin.
 
 use List::MoreUtils qw(first_index);
 
+has _added_inc_path => (is => "rw", isa => "Str");
+
 sub new {
     my $class = shift;
     my $args ={@_};
     my $self = bless $args, $class;
 
+    return $self;
+}
+
+sub Enable {
+    my $self = shift;
     my $add = $self->Path("lib");
     my $local_path = first_index { Cwd::realpath($_) eq $RT::LocalLibPath } @INC;
     if ($local_path >= 0 ) {
@@ -80,14 +88,13 @@ sub new {
     else {
         push @INC, $add;
     }
-    $self->{_added_inc_path} = $add;
-
-    return $self;
+    $self->_added_inc_path( $add );
 }
 
 sub DESTROY {
     my $self = shift;
-    my $inc_path = first_index { Cwd::realpath($_) eq $self->{_added_inc_path} } @INC;
+    my $added = $self->_added_inc_path or return;
+    my $inc_path = first_index { Cwd::realpath($_) eq $added } @INC;
     if ($inc_path >= 0 ) {
         splice(@INC, $inc_path, 1);
     }

commit 8816bf208b522f944c04341348dd1c345b454626
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Tue Oct 19 13:16:46 2010 +0900

    Make Name an accessor

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 306c3ad..002d2a6 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -650,7 +650,7 @@ sub InitPlugins {
     my @plugins;
     require RT::Plugin;
     foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
-        my $plugin = RT::Plugin->new(name => $plugin_name);
+        my $plugin = RT::Plugin->new(Name => $plugin_name);
         $plugin->Enable;
         $plugin_name->require;
         die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index bdc37fc..b844768 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -69,6 +69,8 @@ it cares about is 'name', the name of this plugin.
 use List::MoreUtils qw(first_index);
 
 has _added_inc_path => (is => "rw", isa => "Str");
+has Name => (is => "rw", isa => "Str");
+
 
 sub new {
     my $class = shift;
@@ -100,17 +102,6 @@ sub DESTROY {
     }
 }
 
-=head2 Name
-
-Returns a human-readable name for this plugin.
-
-=cut
-
-sub Name { 
-    my $self = shift;
-    return $self->{name};
-}
-
 =head2 Path
 
 Takes a name of sub directory and returns its full path, for example:
@@ -124,7 +115,7 @@ See also L</ComponentRoot>, L</PoDir> and other shortcut methods.
 sub Path {
     my $self   = shift;
     my $subdir = shift;
-    my $res = $self->BasePathFor($self->{'name'});
+    my $res = $self->BasePathFor($self->Name);
     $res .= "/$subdir" if defined $subdir && length $subdir;
     return $res;
 }

commit 677054ac719b502318b5ed5b604ae628e19c9cde
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Tue Oct 19 14:30:05 2010 +0900

    Helpers for plugin probing.

diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index b844768..b7bc099 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -52,6 +52,7 @@ use strict;
 package RT::Plugin;
 use File::ShareDir;
 use Class::Accessor "antlers";
+use Parse::CPAN::Meta;
 
 =head1 NAME
 
@@ -70,7 +71,8 @@ use List::MoreUtils qw(first_index);
 
 has _added_inc_path => (is => "rw", isa => "Str");
 has Name => (is => "rw", isa => "Str");
-
+has Description => (is => "rw", isa => "Str");
+has BasePath => (is => "rw", isa => "Str");
 
 sub new {
     my $class = shift;
@@ -115,7 +117,7 @@ See also L</ComponentRoot>, L</PoDir> and other shortcut methods.
 sub Path {
     my $self   = shift;
     my $subdir = shift;
-    my $res = $self->BasePathFor($self->Name);
+    my $res = $self->BasePath || $self->BasePathFor($self->Name);
     $res .= "/$subdir" if defined $subdir && length $subdir;
     return $res;
 }
@@ -148,9 +150,28 @@ sub AvailablePlugins {
         my ($dir, $name) = $abs_path =~ m|(.*)/([^/]+)$|;
         # ensure no cascading
         next if $class->BasePathFor($name) ne $abs_path;
-        push @res, $name;
+        push @res, $class->ProbePlugin($name);
     }
-    return \@res;
+
+    # XXX: look for collision and warn
+    my %seen;
+    return { map { $seen{$_->Name}++ ? () : ($_->Name => $_) } @res };
+}
+
+sub ProbePlugin {
+    my ($class, $name) = @_;
+    my $base_path = $class->BasePathFor($name);
+    my $meta;
+    if (-e "$base_path/META.yml") {
+        ($meta) = Parse::CPAN::Meta::LoadFile( "$base_path/META.yml" ) or return;
+    }
+    else {
+        $meta = { name => $name };
+    }
+
+    return $class->new(Name => $meta->{name},
+                       Description => $meta->{abstract},
+                       BasePath => $base_path);
 }
 
 =head2 ComponentRoot
diff --git a/sbin/rt-test-dependencies.in b/sbin/rt-test-dependencies.in
index 993ec68..b11a14c 100755
--- a/sbin/rt-test-dependencies.in
+++ b/sbin/rt-test-dependencies.in
@@ -212,6 +212,7 @@ List::MoreUtils
 Net::CIDR
 Regexp::Common::net::CIDR
 Regexp::IPv6
+Parse::CPAN::Meta
 .
 
 $deps{'MASON'} = [ text_to_hash( << '.') ];
diff --git a/t/plugins/probe.t b/t/plugins/probe.t
index 4b5bd8c..b349399 100644
--- a/t/plugins/probe.t
+++ b/t/plugins/probe.t
@@ -6,27 +6,9 @@ require RT;
 $RT::PluginPath = abs_path(dirname($0)).'/_plugins';
 $RT::LocalPluginPath = abs_path(dirname($0)).'/_plugins_null';
 
-use RT::Test nodb => 1, tests => 7;
+use RT::Test nodb => 1, tests => 2;
 
-is_deeply( RT::Plugin->AvailablePlugins, ['Hello'] );
-__END__
-is_deeply([RT->AvailablePlugins]);
-
-ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
-RT->Config->Set('Plugins',qw(Hello));
-RT->InitPluginPaths;
-RT->Plugins;
-ok(grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
-
-is_deeply([RT->PluginDirs('lib')], ["$RT::PluginPath/Hello/lib"], 'plugin lib dir found');
-
-require RT::Interface::Web::Handler;
-
-is_deeply({RT::Interface::Web::Handler->DefaultHandlerArgs}->{comp_root}[1],
-          ['plugin-Hello', $RT::PluginPath.'/Hello/html']);
-
-# reset
-RT->Config->Set('Plugins',qw());
-RT->InitPluginPaths;
-ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);
-is({RT::Interface::Web::Handler->DefaultHandlerArgs}->{comp_root}[1][0],'standard');
+my $plugins = RT::Plugin->AvailablePlugins;
+is_deeply( [ keys %$plugins ], [qw(Hello)]);
+my $hello = $plugins->{Hello};
+is($hello->BasePath, "$RT::PluginPath/Hello");

commit 096370e6887fb7c4ecab95d0ba53250decaa9a33
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Tue Oct 19 14:48:37 2010 +0900

    do plugin probing

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 002d2a6..5690536 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -582,6 +582,22 @@ also L</InitSystemObjects>.
 
 sub Nobody { return $Nobody }
 
+my $PLUGINS;
+my $LOADED_PLUGINS;
+
+=head2 ProbePlugins($reprobe)
+
+Probe for available plugins.  By default RT caches the plugins found, use C<$reprobe> to override the behaviour.
+
+=cut
+
+sub ProbePlugins {
+    my $self = shift;
+    my $reprobe = shift;
+    undef $PLUGINS if $reprobe;
+    $PLUGINS ||= RT::Plugin->AvailablePlugins;
+}
+
 =head2 Plugins
 
 Returns a listref of all Plugins currently configured for this RT instance.
@@ -589,14 +605,14 @@ You can define plugins by adding them to the @Plugins list in your RT_SiteConfig
 
 =cut
 
-my $PLUGINS;
 sub Plugins {
     my $self = shift;
-    unless ($PLUGINS) {
-        $self->InitPluginPaths;
-        $PLUGINS = [$self->InitPlugins];
+    $self->ProbePlugins;
+
+    unless ($LOADED_PLUGINS) {
+        $LOADED_PLUGINS = [$self->InitPlugins];
     }
-    return $PLUGINS;
+    return $LOADED_PLUGINS;
 }
 
 =head2 PluginDirs
@@ -608,6 +624,7 @@ is loaded to load plugins' configs.
 
 =cut
 
+
 sub PluginDirs {
     my $self = shift;
     my $subdir = shift;
@@ -631,11 +648,13 @@ In case F<local/lib> isn't in @INC, append them to @INC
 =cut
 
 sub InitPluginPaths {
+    warn "DEPRECATED";
     my $self = shift || __PACKAGE__;
 
-    if ($PLUGINS) {
+    $self->ProbePlugins(1);
+    if ($LOADED_PLUGINS) {
         Carp::carp "reinitializing plugin paths";
-        $PLUGINS = undef;
+        $LOADED_PLUGINS = undef;
     }
 }
 
@@ -650,7 +669,11 @@ sub InitPlugins {
     my @plugins;
     require RT::Plugin;
     foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
-        my $plugin = RT::Plugin->new(Name => $plugin_name);
+        my $plugin = $PLUGINS->{$plugin_name};
+        unless ($plugin) {
+            # XXX: this is mostly for testing for rt plugin dists.
+            $PLUGINS->{$plugin_name} = $plugin = RT::Plugin->new(Name => $plugin_name);
+        }
         $plugin->Enable;
         $plugin_name->require;
         die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);

commit 0f3ab0843c12e58eafb2c1bfc0e4e0ece9352ab1
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Tue Oct 19 14:57:11 2010 +0900

    Make InitPlugins more standalone

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 5690536..b9c984c 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -610,7 +610,7 @@ sub Plugins {
     $self->ProbePlugins;
 
     unless ($LOADED_PLUGINS) {
-        $LOADED_PLUGINS = [$self->InitPlugins];
+        $self->InitPlugins;
     }
     return $LOADED_PLUGINS;
 }
@@ -666,7 +666,7 @@ Initialze all Plugins found in the RT configuration file, setting up their lib a
 
 sub InitPlugins {
     my $self    = shift;
-    my @plugins;
+    $LOADED_PLUGINS ||= [];
     require RT::Plugin;
     foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
         my $plugin = $PLUGINS->{$plugin_name};
@@ -674,12 +674,15 @@ sub InitPlugins {
             # XXX: this is mostly for testing for rt plugin dists.
             $PLUGINS->{$plugin_name} = $plugin = RT::Plugin->new(Name => $plugin_name);
         }
+        next if $plugin->Enabled;
+
         $plugin->Enable;
         $plugin_name->require;
         die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);
-        push @plugins, $plugin;
+        push @$LOADED_PLUGINS, $plugin;
     }
-    return @plugins;
+
+    return @$LOADED_PLUGINS;
 }
 
 
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index b7bc099..41d6c9b 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -71,6 +71,7 @@ use List::MoreUtils qw(first_index);
 
 has _added_inc_path => (is => "rw", isa => "Str");
 has Name => (is => "rw", isa => "Str");
+has Enabled => (is => "rw", isa => "Bool");
 has Description => (is => "rw", isa => "Str");
 has BasePath => (is => "rw", isa => "Str");
 
@@ -92,6 +93,7 @@ sub Enable {
     else {
         push @INC, $add;
     }
+    $self->Enabled(1);
     $self->_added_inc_path( $add );
 }
 

-----------------------------------------------------------------------


More information about the Rt-commit mailing list