package Dist::Zilla::Plugin::Boilerplate;{
use Dist::Zilla 4 ();
use Moose;
use namespace::autoclean;
use PPI;
use Moose::Autobox 0.09;
use Path::Class;
with(
'Dist::Zilla::Role::FileMunger',
'Dist::Zilla::Role::FileFinderUser' => {
default_finders => [ ':InstallModules', ':ExecFiles' ],
},
'Dist::Zilla::Role::PPI',
);
has code => (
is => 'ro',
isa => 'Str',
required => 1,
);
has _parsed_code => (
is => 'ro',
isa => 'PPI::Element',
init_arg => undef,
builder => '_build_parsed_code',
);
sub _build_parsed_code {
my ($self) = @_;
warn "parsing ".$self->code."\n";
PPI::Document->new(\$self->code);
}
sub import {
my ($class,@tags) = @_;
my ($caller,$filename) = caller();
require Dist::Zilla::Dist::Builder;
my $zilla = Dist::Zilla::Dist::Builder->from_config({
chrome => Dist::Zilla::Plugin::Boilerplate::Chrome->new(),
dist_root => $class->_find_dzil_root($filename),
});
my %tags;@tags{@tags}=();
my @useful = $zilla->plugins->grep(
sub{
$_->isa($class) && exists $tags{$_->plugin_name}
}
)->flatten;
for my $instance (@useful) {
$instance->eval_in($caller);
}
1;
}
sub _find_dzil_root {
my ($self,$filename) = @_;
my $d = file($filename)->parent;
while ($d && ! glob($d->file('dist.*'))) { $d=$d->parent }
return $d;
}
sub eval_in {
my ($self,$package) = @_;
my $code = $self->code;my $name = $self->plugin_name;
my $str = "{package $package;$code;1}";
warn "evaling $str\n";
local $@;
eval $str
or $self->log_error("Couldn't eval code for boilerplate $name into $package: $@");
}
sub munge_files {
my ($self) = @_;
$self->munge_file($_) for $self->found_files->flatten;
}
sub munge_file {
my ($self, $file) = @_;
my $doc = $self->ppi_document_for_file($file);
my $my_uses = $doc->find(
sub{
my $element = $_[1];
$element->isa('PPI::Statement::Include') &&
$element->type eq 'use' &&
$element->module eq __PACKAGE__
}
);
for my $use ($my_uses->flatten) {
my $tags = $self->_find_tags_from_ppi($use->arguments);
if ($tags->first(sub{$_ eq $self->plugin_name})) {
$use->insert_after($self->_parsed_code);
$use->delete;
}
}
$self->save_ppi_document_to_file($doc,$file);
}
sub _find_tags_from_ppi {
my ($self,$node) = @_;
return ();
}
}
package Dist::Zilla::Plugin::Boilerplate::Chrome;{
use Moose;
has logger => (
is => 'ro',
default => sub {
Log::Dispatchouli->new({
ident => 'Dist::Zilla::Plugin::Boilerplate',
log_pid => 0,
to_self => 0,
to_stdout => 0,
to_stderr => 0,
to_file => 0,
muted => 1,
quiet_fatal => [],
});
}
);
with 'Dist::Zilla::Role::Chrome';
sub prompt_str { return '' }
sub prompt_yn { return 'n' }
sub prompt_any_key { return }
}
1;