Skip to content

Commit

Permalink
Added a plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
bingos committed Feb 4, 2012
1 parent 3cb8b66 commit 31651f1
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dist.ini
@@ -1,5 +1,5 @@
name = Devel-PatchPerl
version = 0.62
version = 0.64
author = Chris Williams <chris@bingosnet.co.uk>
license = Perl_5
copyright_holder = Chris Williams and Marcus Holland-Moritz
Expand All @@ -12,3 +12,4 @@ File::pushd = 1.00
IO::File = 0
IPC::Cmd = 0.40
MIME::Base64 = 0
Module::Pluggable = 0
39 changes: 39 additions & 0 deletions lib/Devel/PatchPerl.pm
Expand Up @@ -9,6 +9,7 @@ use File::Spec;
use IO::File;
use IPC::Cmd qw[can_run run];
use Devel::PatchPerl::Hints qw[hint_file];
use Module::Pluggable search_path => ['Devel::PatchPerl::Plugin'];
use vars qw[@ISA @EXPORT_OK];

@ISA = qw(Exporter);
Expand Down Expand Up @@ -161,9 +162,41 @@ sub patch_source {
$sub->(@args);
}
}
_process_plugin( version => $vers, source => $source, patchexe => $patch_exe );
}
}

sub _process_plugin {
my %args = @_;
return unless my $possible = $ENV{PERL5_PATCHPERL_PLUGIN};
my ($plugin) = grep { $possible eq $_ or /\Q$possible\E$/ } __PACKAGE__->plugins;
unless ( $plugin ) {
warn "# You specified a plugin '", $ENV{PERL5_PATCHPERL_PLUGIN},
"' that isn't installed, just thought you might be interested.\n";
return;
}
{
local $@;
eval "require $plugin";
if ($@) {
die "# I tried to load '", $ENV{PERL5_PATCHPERL_PLUGIN},
"' but it didn't work out. Here is what happened '$@'\n";
}
}
{
local $@;
eval {
$plugin->patchperl(
%args,
);
};
if ($@) {
warn "# Warnings from the plugin: '$@'\n";
}
}
return 1;
}

sub _is
{
my($s1, $s2) = @_;
Expand Down Expand Up @@ -1778,9 +1811,15 @@ current working directory.
=back
=head1 PLUGIN SYSTEM
See L<Devel::PatchPerl::Plugin> for details of Devel::PatchPerl's plugin system.
=head1 SEE ALSO
L<Devel::PPPort>
L<Devel::PatchPerl::Plugin>
=cut

67 changes: 67 additions & 0 deletions lib/Devel/PatchPerl/Plugin.pm
@@ -0,0 +1,67 @@
package Devel::PatchPerl::Plugin;

#ABSTRACT: Devel::PatchPerl plugins explained

use strict;
use warnings;

qq[Plug it in];

=pod
=head1 DESCRIPTION
This document explains the L<Devel::PatchPerl> plugin system.
Plugins are a mechanism for providing additional functionality to
L<Devel::PatchPerl>.
Plugins are searched for in the L<Devel::PatchPerl::Plugin> namespace.
=head1 INITIALISATION
The plugin constructor is C<patchperl>.
A plugin is specified using the C<PERL5_PATCHPERL_PLUGIN> environment
variable. It may either be specified in full (ie. C<Devel::PatchPerl::Plugin::Feegle>)
or as the short part (ie. C<Feegle>).
$ export PERL5_PATCHPERL_PLUGIN=Devel::PatchPerl::Plugin::Feegle
$ export PERL5_PATCHPERL_PLUGIN=Feegle
When L<Devel::PatchPerl> has identified the perl source patch and done its patching
it will attempt to load the plugin identified. It will then call the class method
C<patchperl> for the plugin package, with the following parameters:
'version', the Perl version of the source tree;
'source', the absolute path to the Perl source tree;
'patchexe', the 'patch' utility that can be used;
Plugins are called with the current working directory being the root of the
Perl source tree, ie. C<source>.
Summarised:
$ENV{PERL5_PATCHPERL_PLUGIN} = 'Devel::PatchPerl::Plugin::Feegle';
my $plugin = $ENV{PERL5_PATCHPERL_PLUGIN};
eval "require $plugin";
eval {
$plugin->patchperl( version => $vers, source => $srcdir, patchexe => $patch );
};
=head1 WHAT CAN PLUGINS DO?
Anything you desire to a Perl source tree.
=head1 WHY USE AN ENVIRONMENT VARIABLE TO SPECIFY PLUGINS?
So that indicating a plugin to use can be specified independently of whatever mechanism is
calling L<Devel::PatchPerl> to do its bidding.
Think L<perlbrew>.
=cut
11 changes: 11 additions & 0 deletions t/03_plugin.t
@@ -0,0 +1,11 @@
use strict;
use warnings;
use lib 't/lib';
BEGIN {
$ENV{PERL5_PATCHPERL_PLUGIN} = 'TEST';
}
use Test::More qq'no_plan';
use File::Spec;
use Devel::PatchPerl;
my $result = Devel::PatchPerl::_process_plugin(version => '5.14.2');
ok( $result, 'The result was okay' );
10 changes: 10 additions & 0 deletions t/lib/Devel/PatchPerl/Plugin/TEST.pm
@@ -0,0 +1,10 @@
package Devel::PatchPerl::Plugin::TEST;

use strict;
use warnings;

sub patchperl {
warn "I AM A TEST PLUGIN\n";
}

qq[1];

0 comments on commit 31651f1

Please sign in to comment.