summaryrefslogtreecommitdiff
path: root/lib/Dist/Zilla/Plugin/Boilerplate.pm
blob: 0a0d25e1ac4654f45837dfb32fa0fbecef60137d (plain)
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
package Dist::Zilla::Plugin::Boilerplate;{ 
use Dist::Zilla 4 ();
use Moose;
use namespace::autoclean;
use PPI;
use Moose::Autobox 0.09;
use Path::Class;
#use This::Is::A::Test 'foo','bar'; 
#use This::Is::A::Test ('foo','bar'); 
#use This::Is::A::Test qw(foo bar); 
 
# ABSTRACT: foo 
 
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 (); # 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;