| 1 | package FFI::Build::Plugin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | use File::Spec::Functions qw( catdir catfile ); | ||
| 7 | |||
| 8 | # ABSTRACT: Platform and local customizations of FFI::Build | ||
| 9 | our $VERSION = '2.11'; # VERSION | ||
| 10 | |||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my($class) = @_; | ||
| 15 | |||
| 16 | my %plugins; | ||
| 17 | |||
| 18 | foreach my $inc (@INC) | ||
| 19 | { | ||
| 20 | # CAVEAT: won't work with an @INC hook. Plugins must be in a "real" directory. | ||
| 21 | my $path = catdir($inc, 'FFI', 'Build', 'Plugin'); | ||
| 22 | next unless -d $path; | ||
| 23 | my $dh; | ||
| 24 | opendir $dh, $path; | ||
| 25 | my @list = readdir $dh; | ||
| 26 | closedir $dh; | ||
| 27 | |||
| 28 | foreach my $name (map { my $x = $_; $x =~ s/\.pm$//; $x } grep /\.pm$/, @list) | ||
| 29 | { | ||
| 30 | next if defined $plugins{$name}; | ||
| 31 | my $pm = catfile('FFI', 'Build', 'Plugin', "$name.pm"); | ||
| 32 | require $pm; | ||
| 33 | my $class = "FFI::Build::Plugin::$name"; | ||
| 34 | if($class->can("api_version") && $class->api_version == 0) | ||
| 35 | { | ||
| 36 | $plugins{$name} = $class->new; | ||
| 37 | } | ||
| 38 | else | ||
| 39 | { | ||
| 40 | warn "$class is not the correct api version. You may need to upgrade the plugin, platypus or uninstall the plugin"; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | bless \%plugins, $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub call | ||
| 49 | { | ||
| 50 | my($self, $method, @args) = @_; | ||
| 51 | |||
| 52 | foreach my $name (sort keys %$self) | ||
| 53 | { | ||
| 54 | my $plugin = $self->{$name}; | ||
| 55 | $plugin->$method(@args) if $plugin->can($method); | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |