[Rt-commit] rt branch, runtime-plugin, created. rt-3.9.4-160-g2893485

Chia-liang Kao clkao at bestpractical.com
Mon Oct 18 07:28:49 EDT 2010


The branch, runtime-plugin has been created
        at  289348596fe2d05e56c72675741fa6e4a5cb74dc (commit)

- Log -----------------------------------------------------------------
commit 290b8992f2aa52359fc2739c139ba36c03052785
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 15:44:35 2010 +0900

    basic plugin api tests

diff --git a/t/plugins/_plugins/Hello/html/NoAuth/hello b/t/plugins/_plugins/Hello/html/NoAuth/hello
new file mode 100644
index 0000000..e892a58
--- /dev/null
+++ b/t/plugins/_plugins/Hello/html/NoAuth/hello
@@ -0,0 +1 @@
+<% _("Hello") %>
\ No newline at end of file
diff --git a/t/plugins/_plugins/Hello/lib/Hello.pm b/t/plugins/_plugins/Hello/lib/Hello.pm
new file mode 100644
index 0000000..6bff916
--- /dev/null
+++ b/t/plugins/_plugins/Hello/lib/Hello.pm
@@ -0,0 +1,3 @@
+package Hello;
+
+1;
diff --git a/t/plugins/_plugins/Hello/po/zh_tw.po b/t/plugins/_plugins/Hello/po/zh_tw.po
new file mode 100644
index 0000000..4f78110
--- /dev/null
+++ b/t/plugins/_plugins/Hello/po/zh_tw.po
@@ -0,0 +1,13 @@
+msgid ""
+msgstr ""
+"PO-Revision-Date: 2008-10-20 17:11-0400\n"
+"Project-Id-Version: Hello\n"
+"Last-Translator: Chia-liang Kao <clkao at clkao.org>\n"
+"Language-Team: rt-devel <rt-devel at lists.bestpractical.com>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: NOT FOUND IN SOURCE
+msgid "Hello"
+msgstr "你好"
diff --git a/t/plugins/api.t b/t/plugins/api.t
new file mode 100644
index 0000000..5fef1bd
--- /dev/null
+++ b/t/plugins/api.t
@@ -0,0 +1,25 @@
+#!perl
+use Cwd qw(abs_path);
+use File::Basename qw(basename dirname);
+
+require RT;
+$RT::PluginPath = abs_path(dirname($0)).'/_plugins';
+
+use RT::Test nodb => 1, tests => 5;
+
+is_deeply([RT->PluginDirs('lib')], []);
+ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
+RT->Config->Set('Plugins',qw(Hello));
+RT->InitPluginPaths;
+
+ok(grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
+
+is_deeply([RT->PluginDirs('lib')], ["$RT::PluginPath/Hello/lib"]);
+
+require RT::Interface::Web::Handler;
+
+
+is_deeply({RT::Interface::Web::Handler->DefaultHandlerArgs}->{comp_root}[1],
+          ['plugin-Hello', $RT::PluginPath.'/Hello/html']);
+
+

commit 5680f6e8cd960cee085a70745f8a0c0e18541b31
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 19:19:33 2010 +0900

    Avoid calling initplugins directly

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 06b3a67..32041d0 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -190,7 +190,7 @@ sub Init {
     InitSystemObjects();
     InitClasses();
     InitLogging(); 
-    InitPlugins();
+    RT->Plugins();
     RT::I18N->Init;
     RT->Config->PostLoadCheck;
 
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index b71e782..228b830 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -166,8 +166,6 @@ sub import {
     RT::InitLogging();
 
     $class->bootstrap_plugins( %args );
-
-    RT::InitPlugins();
     
     RT::I18N->Init();
     RT->Config->PostLoadCheck;
@@ -420,7 +418,9 @@ sub bootstrap_plugins {
     my $self = shift;
     my %args = @_;
 
-    return unless $args{'requires'};
+    unless ($args{'requires'}) {
+        return RT->Plugins;
+    }
 
     my @plugins = @{ $args{'requires'} };
     push @plugins, $args{'testing'}
@@ -457,8 +457,8 @@ sub bootstrap_plugins {
     ) if @plugins;
 
     require File::Spec;
-    foreach my $name ( @plugins ) {
-        my $plugin = RT::Plugin->new( name => $name );
+    foreach my $plugin ( @{ RT->Plugins } ) {
+        my $name = $plugin->Name;
         Test::More::diag( "Initializing DB for the $name plugin" )
             if $ENV{'TEST_VERBOSE'};
 
@@ -491,6 +491,7 @@ sub bootstrap_plugins {
         $RT::Handle->Connect; # XXX: strange but mysql can loose connection
     }
     $dba_dbh->disconnect if $dba_dbh;
+    return RT->Plugins;
 }
 
 sub _get_dbh {
diff --git a/sbin/rt-server.in b/sbin/rt-server.in
index f5446f6..b04f4f9 100644
--- a/sbin/rt-server.in
+++ b/sbin/rt-server.in
@@ -131,7 +131,7 @@ EOF
     RT->ConnectToDatabase();
     RT->InitSystemObjects();
     RT->InitClasses();
-    RT->InitPlugins();
+    RT->Plugins();
 }
 
 require RT::Interface::Web::Standalone;

commit 4927c099e6dccdc91d2d327ea705a472c613e845
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 19:26:12 2010 +0900

    Make Plugins init only once

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 32041d0..a63a0f7 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -591,14 +591,14 @@ You can define plugins by adding them to the @Plugins list in your RT_SiteConfig
 
 =cut
 
-our @PLUGINS = ();
+my $PLUGINS;
 sub Plugins {
     my $self = shift;
-    unless (@PLUGINS) {
+    unless ($PLUGINS) {
         $self->InitPluginPaths;
-        @PLUGINS = $self->InitPlugins;
+        $PLUGINS = [$self->InitPlugins];
     }
-    return \@PLUGINS;
+    return $PLUGINS;
 }
 
 =head2 PluginDirs

commit 785980d739d0747d8937b31bee8e1ab302704430
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 19:44:29 2010 +0900

    rework plugin path init.

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index a63a0f7..480bc8d 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -635,7 +635,17 @@ In case F<local/lib> isn't in @INC, append them to @INC
 sub InitPluginPaths {
     my $self = shift || __PACKAGE__;
 
-    my @lib_dirs = $self->PluginDirs('lib');
+    if ($PLUGINS) {
+        Carp::carp "reinitializing plugin paths";
+        $PLUGINS = undef;
+    }
+
+    my @lib_dirs;
+    foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
+        my $path = RT::Plugin->BasePathFor( $plugin_name ) . '/lib';
+        next unless -d $path;
+        push @lib_dirs, $path;
+    }
 
     my @tmp_inc;
     my $added;
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index 1705c47..41a3d13 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -104,7 +104,12 @@ sub Path {
 
 sub _BasePath {
     my $self = shift;
-    my $base = $self->{'name'};
+    $self->BasePathFor($self->{'name'});
+}
+
+sub BasePathFor {
+    my ($class, $base) = @_;
+
     $base =~ s/::/-/g;
     my $local_base = $RT::LocalPluginPath."/".$base;
     my $base_base = $RT::PluginPath."/".$base;
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 228b830..b9c6fa1 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -433,10 +433,10 @@ sub bootstrap_plugins {
         $cwd = Cwd::getcwd();
     }
 
-    my $old_func = \&RT::Plugin::_BasePath;
+    my $old_func = \&RT::Plugin::BasePathFor;
     no warnings 'redefine';
-    *RT::Plugin::_BasePath = sub {
-        my $name = $_[0]->{'name'};
+    *RT::Plugin::BasePathFor = sub {
+        my $name = $_[1];
 
         return $cwd if $args{'testing'} && $name eq $args{'testing'};
 
diff --git a/t/plugins/api.t b/t/plugins/api.t
index 5fef1bd..49d9061 100644
--- a/t/plugins/api.t
+++ b/t/plugins/api.t
@@ -14,12 +14,9 @@ RT->InitPluginPaths;
 
 ok(grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
 
-is_deeply([RT->PluginDirs('lib')], ["$RT::PluginPath/Hello/lib"]);
+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']);
-
-

commit 10cd7517a0fbbaf20da62260cb127d19907bc830
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 19:47:06 2010 +0900

    cleanup _BasePath

diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index 41a3d13..f0358e9 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -97,22 +97,24 @@ See also L</ComponentRoot>, L</PoDir> and other shortcut methods.
 sub Path {
     my $self   = shift;
     my $subdir = shift;
-    my $res = $self->_BasePath;
+    my $res = $self->BasePathFor($self->{'name'});
     $res .= "/$subdir" if defined $subdir && length $subdir;
     return $res;
 }
 
-sub _BasePath {
-    my $self = shift;
-    $self->BasePathFor($self->{'name'});
-}
+=head2 $class->BasePathFor($name)
+
+Takes a name of a given plugin and return its base path.
+
+=cut
+
 
 sub BasePathFor {
-    my ($class, $base) = @_;
+    my ($class, $name) = @_;
 
-    $base =~ s/::/-/g;
-    my $local_base = $RT::LocalPluginPath."/".$base;
-    my $base_base = $RT::PluginPath."/".$base;
+    $name =~ s/::/-/g;
+    my $local_base = $RT::LocalPluginPath."/".$name;
+    my $base_base = $RT::PluginPath."/".$name;
 
     return -d $local_base ? $local_base : $base_base;
 }

commit 289348596fe2d05e56c72675741fa6e4a5cb74dc
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Mon Oct 18 20:17:18 2010 +0900

    Make RT::Plugin instance responsible for maintaining @INC

diff --git a/lib/RT.pm.in b/lib/RT.pm.in
index 480bc8d..6257a66 100755
--- a/lib/RT.pm.in
+++ b/lib/RT.pm.in
@@ -617,8 +617,8 @@ sub PluginDirs {
     require RT::Plugin;
 
     my @res;
-    foreach my $plugin (grep $_, RT->Config->Get('Plugins')) {
-        my $path = RT::Plugin->new( name => $plugin )->Path( $subdir );
+    foreach my $plugin (@{ RT->Plugins }) {
+        my $path = $plugin->Path($subdir);
         next unless -d $path;
         push @res, $path;
     }
@@ -639,30 +639,6 @@ sub InitPluginPaths {
         Carp::carp "reinitializing plugin paths";
         $PLUGINS = undef;
     }
-
-    my @lib_dirs;
-    foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
-        my $path = RT::Plugin->BasePathFor( $plugin_name ) . '/lib';
-        next unless -d $path;
-        push @lib_dirs, $path;
-    }
-
-    my @tmp_inc;
-    my $added;
-    for (@INC) {
-        if ( Cwd::realpath($_) eq $RT::LocalLibPath) {
-            push @tmp_inc, $_, @lib_dirs;
-            $added = 1;
-        } else {
-            push @tmp_inc, $_;
-        }
-    }
-
-    # append @lib_dirs in case $RT::LocalLibPath isn't in @INC
-    push @tmp_inc, @lib_dirs unless $added;
-
-    my %seen;
-    @INC = grep !$seen{$_}++, @tmp_inc;
 }
 
 =head2 InitPlugins
@@ -675,10 +651,11 @@ sub InitPlugins {
     my $self    = shift;
     my @plugins;
     require RT::Plugin;
-    foreach my $plugin (grep $_, RT->Config->Get('Plugins')) {
-        $plugin->require;
+    foreach my $plugin_name (grep $_, RT->Config->Get('Plugins')) {
+        my $plugin = RT::Plugin->new(name => $plugin_name);
+        $plugin_name->require;
         die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);
-        push @plugins, RT::Plugin->new(name =>$plugin);
+        push @plugins, $plugin;
     }
     return @plugins;
 }
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index f0358e9..410d8fa 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -65,13 +65,29 @@ it cares about is 'name', the name of this plugin.
 
 =cut
 
+use List::MoreUtils qw(first_index);
+
 sub new {
     my $class = shift;
     my $args ={@_};
     my $self = bless $args, $class;
+
+    my $add = $self->Path("lib");
+    my $local_path = first_index { Cwd::realpath($_) eq $RT::LocalLibPath } @INC;
+    if ($local_path >= 0 ) {
+        splice(@INC, $local_path+1, 0, $add);
+    }
+    else {
+        push @INC, $add;
+    }
+
     return $self;
 }
 
+sub DESTROY {
+    my $self = shift;
+
+}
 
 =head2 Name
 
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index b9c6fa1..7d9a82c 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -157,8 +157,6 @@ sub import {
 
     $class->bootstrap_db( %args );
 
-    RT::InitPluginPaths();
-
     RT::ConnectToDatabase()
         unless $args{nodb};
 
@@ -450,6 +448,8 @@ sub bootstrap_plugins {
 
     RT->Config->Set( Plugins => @plugins );
 
+    RT->InitPluginPaths();
+
     my $dba_dbh;
     $dba_dbh = _get_dbh(
         RT::Handle->DSN,
diff --git a/t/plugins/api.t b/t/plugins/api.t
index 49d9061..b97940d 100644
--- a/t/plugins/api.t
+++ b/t/plugins/api.t
@@ -11,7 +11,7 @@ is_deeply([RT->PluginDirs('lib')], []);
 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');

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


More information about the Rt-commit mailing list