1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package Module::Build::Pluggable;
use strict;
use warnings;
use 5.008001;
our $VERSION = '0.10';
use Module::Build;
our $SUBCLASS;
our $OPTIONS;
our @REQUIRED_PLUGINS;
use Data::OptList;
use Data::Dumper; # as serializer.
use Module::Load ();
use Module::Build::Pluggable::Util;
sub import {
my $class = shift;
my $pkg = caller(0);
return unless @_;
my $optlist = Data::OptList::mkopt(\@_);
@REQUIRED_PLUGINS = map { _mkpluginname($_) } grep !/^\+/, map { $_->[0] } @$optlist;
$optlist = [map { [ _mkpluginname($_->[0]), $_->[1] ] } @$optlist];
_author_requires(map { $_->[0] } @$optlist);
push @$OPTIONS, @$optlist;
$SUBCLASS = Module::Build->subclass(
code => _mksrc(),
);
}
sub _author_requires {
my @devmods = @_;
my @not_available;
for my $mod (@devmods) {
## no critic.
eval qq{require $mod} or push @not_available, $mod;
# need to diag $@ if an error message is not "Can't locate..."?
}
if (@not_available) {
print qq{# The following modules are not available.\n};
print qq{# `$^X $0 | cpanm` will install them:\n};
print $_, "\n" for @not_available;
print "\n";
exit -1;
}
}
sub _mksrc {
my $data = do {
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
Data::Dumper::Dumper($OPTIONS);
};
return sprintf(q{
use Module::Build::Pluggable;
sub resume {
my $class = shift;
my $self = $class->SUPER::resume(@_);
Module::Build::Pluggable->call_triggers_all('build', $self, %s);
$self;
}
}, $data);
}
sub _mkpluginname {
my $module = shift;
$module = $module =~ s/^\+// ? $module : "Module::Build::Pluggable::$module";
$module;
}
sub new {
my $class = shift;
my %args = @_;
$class->call_triggers_all('prepare', undef, $OPTIONS, \%args);
my $builder = $SUBCLASS->new(%args);
my $self = bless { builder => $builder }, $class;
$self->_init();
$self->call_triggers_all('configure', $builder, $OPTIONS);
return $self;
}
sub _init {
my $self = shift;
# setup (build|configure) requires
for my $module (@REQUIRED_PLUGINS) {
for my $type (qw/configure_requires build_requires/) {
Module::Build::Pluggable::Util->add_prereqs(
$self->{builder},
$type,
$module, $module->VERSION,
);
}
}
}
sub call_triggers_all {
my ($class, $type, $builder, $options, $args) =@_;
for my $opt (@$options) {
my ($module, $opts) = @$opt;
$class->call_trigger($type, $builder, $module, $opts, $args);
}
}
sub call_trigger {
my ($class, $type, $builder, $module, $opts, $args) =@_;
Module::Load::load($module);
my $plugin = $module->new(builder => $builder, %{ $opts || +{} });
my $method = "HOOK_$type";
if ($plugin->can($method)) {
$plugin->$method($args);
}
}
sub DESTROY { }
our $AUTOLOAD;
sub AUTOLOAD {
my $self = shift;
$AUTOLOAD =~ s/.*:://;
return $self->{builder}->$AUTOLOAD(@_);
}
1;
__END__
=encoding utf8
=for stopwords pluggability
=head1 NAME
Module::Build::Pluggable - Module::Build meets plugins
=head1 SYNOPSIS
use Module::Build::Pluggable (
'Repository',
'ReadmeMarkdownFromPod',
'PPPort',
);
my $builder = Module::Build::Pluggable->new(
... # normal M::B args
);
$builder->create_build_script();
=head1 DESCRIPTION
Module::Build::Pluggable adds pluggability for Module::Build.
=head1 HOW CAN I WRITE MY OWN PLUGIN?
Module::Build::Pluggable call B<HOOK_prepare> on preparing arguments for C<< Module::Build->new >>, B<HOOK_configure> on configuration step, and B<HOOK_build> on build step.
That's all.
And if you want a help, you can use L<Module::Build::Pluggable::Base> as base class.
=head1 AUTHOR
Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF@ GMAIL COME<gt>
=head1 SEE ALSO
This module built on L<Module::Build>.
=head1 LICENSE
Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|