summaryrefslogtreecommitdiff
path: root/lib/Dist/Zilla/Plugin
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2011-09-27 21:56:40 +0100
committerdakkar <dakkar@thenautilus.net>2011-09-27 21:56:40 +0100
commit4d2d09de0c6ca4f47d3dd02452fc16ad4de60366 (patch)
tree8281bfe764dd738d224d559cf22030a23461857b /lib/Dist/Zilla/Plugin
downloaddzil-boilerplate-4d2d09de0c6ca4f47d3dd02452fc16ad4de60366.tar.gz
dzil-boilerplate-4d2d09de0c6ca4f47d3dd02452fc16ad4de60366.tar.bz2
dzil-boilerplate-4d2d09de0c6ca4f47d3dd02452fc16ad4de60366.zip
first stab
Diffstat (limited to 'lib/Dist/Zilla/Plugin')
-rw-r--r--lib/Dist/Zilla/Plugin/Boilerplate.pm127
1 files changed, 127 insertions, 0 deletions
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;