From 4d2d09de0c6ca4f47d3dd02452fc16ad4de60366 Mon Sep 17 00:00:00 2001 From: dakkar Date: Tue, 27 Sep 2011 21:56:40 +0100 Subject: first stab --- dist.ini | 9 +++ lib/Dist/Zilla/Plugin/Boilerplate.pm | 127 +++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 dist.ini create mode 100644 lib/Dist/Zilla/Plugin/Boilerplate.pm diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..798db7f --- /dev/null +++ b/dist.ini @@ -0,0 +1,9 @@ +name = Dist-Zilla-Plugin-Boilerplate +author = Gianni Ceccarelli +license = Perl_5 +copyright_holder = Gianni Ceccarelli +copyright_year = 2011 + +version = 0.001 + +[@Basic] diff --git a/lib/Dist/Zilla/Plugin/Boilerplate.pm b/lib/Dist/Zilla/Plugin/Boilerplate.pm new file mode 100644 index 0000000..87e84a8 --- /dev/null +++ b/lib/Dist/Zilla/Plugin/Boilerplate.pm @@ -0,0 +1,127 @@ +package Dist::Zilla::Plugin::Boilerplate;{ +use Dist::Zilla 4 (); +use Moose; +use namespace::autoclean; +use PPI; +use Moose::Autobox 0.09; +#use This::Is::A::Test 'foo','bar'; +#use This::Is::A::Test ('foo','bar'); +#use This::Is::A::Test qw(foo bar); + +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) = @_; + 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 eval_in { + my ($self,$package) = @_; + + my $code = $self->code;my $name = $self->plugin_name; + local $@; + eval "package $package;$code;1" + 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 (); # ehm +} +} +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; -- cgit v1.2.3