[Rt-commit] rt branch, runtime-plugin, created. rt-3.9.4-418-g511b2a6
Chia-liang Kao
clkao at bestpractical.com
Sun Nov 7 23:04:28 EST 2010
The branch, runtime-plugin has been created
at 511b2a6cabf7608f008b09363d41c001d7135102 (commit)
- Log -----------------------------------------------------------------
commit 391e25ae6f9b98b6a4b2d752d5e29c89cb812a07
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 a7d3cb50a83b836e031003a555a31c57fda3ca8a
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 b/lib/RT.pm
index c8c1df5..eaa038a 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -146,7 +146,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 0695da2..350d7f2 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -150,8 +150,6 @@ sub import {
$class->bootstrap_plugins( %args );
- RT->Plugins;
-
RT::I18N->Init();
RT->Config->PostLoadCheck;
@@ -403,7 +401,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'}
@@ -441,8 +441,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'};
@@ -475,6 +475,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 {
commit 0666e9fd4da0fd0eb590397d756b908b895f03ff
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 b/lib/RT.pm
index eaa038a..9a9daac 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -547,14 +547,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 e9bed99a8a7d298b5ed8268c8cfb5480c62b3d16
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 b/lib/RT.pm
index 9a9daac..09aef6e 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -591,7 +591,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 350d7f2..5c7032f 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -416,10 +416,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 b8131db2ab6389c1b26c456fd94910922dbe606f
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 c31f7fac1b238004aea60379752f89c78e932834
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 b/lib/RT.pm
index 09aef6e..f38830d 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -573,8 +573,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;
}
@@ -595,30 +595,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
@@ -631,10 +607,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 5c7032f..c62893a 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -140,8 +140,6 @@ sub import {
$class->bootstrap_db( %args );
- RT::InitPluginPaths();
-
RT::ConnectToDatabase()
unless $args{nodb};
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');
commit d57286c9f993dc1d6a35b56e7f2a9c33b7dea33d
Author: Chia-liang Kao <clkao at bestpractical.com>
Date: Mon Oct 18 21:08:07 2010 +0900
Don't need InitPluginPaths to be called in Init anymore
diff --git a/lib/RT.pm b/lib/RT.pm
index f38830d..2e49d0f 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -139,8 +139,6 @@ sub Init {
CheckPerlRequirements();
- InitPluginPaths();
-
#Get a database connection
ConnectToDatabase();
InitSystemObjects();
commit e388331dfe983cf06a5aa6a845783347465d73a3
Author: Chia-liang Kao <clkao at bestpractical.com>
Date: Mon Oct 18 21:44:35 2010 +0900
inc path maintenance
diff --git a/lib/RT/Plugin.pm b/lib/RT/Plugin.pm
index 410d8fa..d0f6fba 100644
--- a/lib/RT/Plugin.pm
+++ b/lib/RT/Plugin.pm
@@ -80,13 +80,17 @@ sub new {
else {
push @INC, $add;
}
+ $self->{_added_inc_path} = $add;
return $self;
}
sub DESTROY {
my $self = shift;
-
+ my $inc_path = first_index { Cwd::realpath($_) eq $self->{_added_inc_path} } @INC;
+ if ($inc_path >= 0 ) {
+ splice(@INC, $inc_path, 1);
+ }
}
=head2 Name
diff --git a/t/plugins/api.t b/t/plugins/api.t
index b97940d..f666ee5 100644
--- a/t/plugins/api.t
+++ b/t/plugins/api.t
@@ -5,7 +5,7 @@ use File::Basename qw(basename dirname);
require RT;
$RT::PluginPath = abs_path(dirname($0)).'/_plugins';
-use RT::Test nodb => 1, tests => 5;
+use RT::Test nodb => 1, tests => 7;
is_deeply([RT->PluginDirs('lib')], []);
ok(!grep { $_ eq "$RT::PluginPath/Hello/lib" } @INC);;
@@ -20,3 +20,9 @@ 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 a1162e2000e4c2b2f231d431082053ee5bed2ddd
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 63a0b495af3cc2f4ab1a055ba672d5ac82625e7b
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 b/lib/RT.pm
index 2e49d0f..1b1a077 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -607,6 +607,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 fe7034add024f874565619d9fa64bd65a0996c30
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 b/lib/RT.pm
index 1b1a077..c916a8c 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -606,7 +606,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 fea120e89f4f9def945180dec0220f2855da0f83
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 4fdb63d..100d095 100755
--- a/sbin/rt-test-dependencies.in
+++ b/sbin/rt-test-dependencies.in
@@ -211,6 +211,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 9ba4ae9a55104c282ca6f185e3abd6c2b0827408
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 b/lib/RT.pm
index c916a8c..c82dda9 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -538,6 +538,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.
@@ -545,14 +561,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
@@ -564,6 +580,7 @@ is loaded to load plugins' configs.
=cut
+
sub PluginDirs {
my $self = shift;
my $subdir = shift;
@@ -587,11 +604,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;
}
}
@@ -606,7 +625,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 511b2a6cabf7608f008b09363d41c001d7135102
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 b/lib/RT.pm
index c82dda9..ee2f6e2 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -566,7 +566,7 @@ sub Plugins {
$self->ProbePlugins;
unless ($LOADED_PLUGINS) {
- $LOADED_PLUGINS = [$self->InitPlugins];
+ $self->InitPlugins;
}
return $LOADED_PLUGINS;
}
@@ -622,7 +622,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};
@@ -630,12 +630,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