blib/lib/Dist/Zilla/Plugin/DistBuild.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 216 CC 14 SCAR 24.6
CC
14
Cov
0%
register_prereqs
30.0
setup_installer
20.0
_build_header
12.0
CRAP 11.7
1 package Dist::Zilla::Plugin::DistBuild;
2 $Dist::Zilla::Plugin::DistBuild::VERSION = '0.003';
3 use Moose;
4 with qw/
5 Dist::Zilla::Role::BuildPL
6 Dist::Zilla::Role::TextTemplate
7 Dist::Zilla::Role::PrereqSource
8 Dist::Zilla::Role::FileGatherer
9 /;
10 use namespace::autoclean;
11  
12 use experimental 'signatures', 'postderef';
13  
14 use Module::Metadata;
15 use Moose::Util::TypeConstraints 'enum';
16 use MooseX::Types::Perl qw/StrictVersionStr/;
17 use MooseX::Types::Moose qw/Bool Str ArrayRef/;
18 use List::Util 1.33 qw/first any/;
19  
20 sub mvp_multivalue_args { qw(header_strs footer_strs) }
21  
22 sub mvp_aliases {
23 +{
24 header => 'header_strs',
25 footer => 'footer_strs',
26 }
27 }
28  
29 around BUILDARGS => sub($orig, $class, @args) {
30 my $args = $class->$orig(@args);
31  
32 my $delimiter = delete $args->{delimiter};
33 if (defined $delimiter and length($delimiter)) {
34 for my $arg (grep exists $args->{$_}, qw(header_strs footer_strs)) {
35 s/^\Q$delimiter\E// for $args->{$arg}->@*;
36 }
37 }
38  
39 return $args;
40 };
41  
42 has version_method => (
43 is => 'ro',
44 isa => enum(['installed', 'heuristic']),
45 default => 'heuristic',
46 );
47  
48 has version => (
49 is => 'ro',
50 lazy => 1,
51 isa => StrictVersionStr,
52 default => sub($self) {
53 if ($self->version_method eq 'installed') {
54 return Module::Metadata->new_from_module('Dist::Build')->version->stringify;
55 } else {
56 return '0.019';
57 }
58 },
59 );
60  
61 has minimum_perl => (
62 is => 'ro',
63 isa => StrictVersionStr,
64 lazy => 1,
65 default => sub($self) {
66 my $prereqs = $self->zilla->prereqs->cpan_meta_prereqs;
67 my $reqs = $prereqs->merged_requirements([ qw/configure build test runtime/ ], ['requires']);
68 return $reqs->requirements_for_module('perl') || '5.006';
69 },
70 );
71  
72 has header_strs => (
73 is => 'ro', isa => ArrayRef[Str],
74 traits => ['Array'],
75 lazy => 1,
76 default => sub { [] },
77 documentation => "Additional code lines to include at the beginning of Makefile.PL",
78 );
79  
80 has header_file => (
81 is => 'ro',
82 isa => Str,
83 documentation => 'Additional header content to include from a file',
84 );
85  
86 has header => (
87 is => 'ro',
88 isa => Str,
89 lazy => 1,
90 builder => '_build_header',
91 documentation => "A string included at the beginning of Makefile.PL",
92 );
93  
94 sub _build_header($self) {
95 join "\n",
96 $self->header_strs->@*,
97 ( $self->header_file
98 ? do {
99 my $abs_file = path($self->zilla->root, $self->header_file);
100 $self->log_fatal([ 'header_file %s does not exist!', $self->header_file ])
101 if not $abs_file->exists;
102 $abs_file->slurp_utf8
103 }
104 : () );
105 }
106  
107 has footer_strs => (
108 is => 'ro', isa => ArrayRef[Str],
109 traits => ['Array'],
110 lazy => 1,
111 default => sub { [] },
112 documentation => "Additional code lines to include at the end of Makefile.PL",
113 );
114  
115 has footer_file => (
116 is => 'ro',
117 isa => Str,
118 documentation => 'Additional footer content to include from a file',
119 );
120  
121 has footer => (
122 is => 'ro',
123 isa => Str,
124 lazy => 1,
125 builder => '_build_footer',
126 documentation => "A string included at the end of Makefile.PL",
127 );
128  
129 sub _build_footer($self) {
130 join "\n",
131 $self->footer_strs->@*,
132 ( $self->footer_file
133 ? do {
134 my $abs_file = path($self->zilla->root, $self->footer_file);
135 $self->log_fatal([ 'footer_file %s does not exist!', $self->footer_file ])
136 if not $abs_file->exists;
137 $abs_file->slurp_utf8
138 }
139 : () );
140 }
141  
142 has auto_configure_requires => (
143 is => 'ro',
144 isa => Bool,
145 default => 1,
146 );
147  
148 has auto_plugin_requires => (
149 is => 'ro',
150 isa => Bool,
151 default => 0,
152 );
153  
154 sub register_prereqs($self) {
155 if ($self->auto_configure_requires) {
156 $self->zilla->register_prereqs({ phase => 'configure' }, 'Dist::Build' => $self->version);
157 }
158  
159 if ($self->auto_plugin_requires) {
160 my @plans;
161 File::Find::find(sub { push @plans, $File::Find::name if -f && /\.pl$/ }, 'planner') if -d 'planner';
162  
163 require Perl::PrereqScanner;
164 require Perl::PrereqScanner::Scanner::DistBuild;
165 my $scanner = Perl::PrereqScanner->new({ extra_scanners => ['DistBuild'] });
166 for my $file (@plans) {
167 my $prereqs = $scanner->scan_file($file);
168 $prereqs->clear_requirement($_) for qw/strict warnings/;
169 $self->zilla->register_prereqs({ phase => 'configure' }, %{ $prereqs->as_string_hash });
170 }
171 }
172  
173 return;
174 }
175  
176 my $template = <<'BUILD_PL';
177 # This Build.PL for {{ $dist_name }} was generated by {{ $plugin_title }}.
178 use strict;
179 use warnings;
180  
181 {{ $header }}
182 use {{ $minimum_perl }};
183 use Dist::Build{{ $version ne 0 && " $version" }};
184 Build_PL(\@ARGV, \%ENV);
185 {{ $footer }}
186 BUILD_PL
187  
188 sub gather_files($self) {
189 if (my $file = first { $_->name eq 'Build.PL' } $self->zilla->files->@*) {
190 if ($file->isa('Dist::Zilla::File::OnDisk')) {
191 $self->log('replacing existing Build.PL found in repository');
192 $self->zilla->prune_file($file);
193 }
194 }
195  
196 require Dist::Zilla::File::InMemory;
197 my $file = Dist::Zilla::File::InMemory->new({
198 name => 'Build.PL',
199 content => $template, # template evaluated later
200 });
201  
202 $self->add_file($file);
203 return;
204 }
205  
206 sub setup_installer($self) {
207 my @sharedir;
208  
209 for my $map (map { $_->share_dir_map } $self->zilla->plugins_with(-ShareDir)->@*) {
210 push @sharedir, sprintf "dist_sharedir('%s');", quotemeta $map->{dist} if defined $map->{dist};
211 for my $module (keys %{ $map->{module} }) {
212 push @sharedir, sprintf "module_sharedir('%s', '%s');", $map->{module}{$module} =~ s{[\\']}{\\$1}gr, $module;
213 }
214 }
215  
216 if (@sharedir) {
217 unshift @sharedir, "load_extension('Dist::Build::ShareDir');";
218 my $content = join '', map "$_\n", @sharedir;
219 my $file = Dist::Zilla::File::InMemory->new({
220 name => 'planner/sharedir.pl',
221 content => $content,
222 });
223 $self->add_file($file);
224 }
225  
226 my @exedir = map { sprintf "script_dir('%s');", $_->dir } $self->zilla->plugins_with(-ExecFiles)->@*;
227 if (@exedir) {
228 unshift @exedir, "load_extension('Dist::Build::Core');";
229 my $content = join '', map "$_\n", @exedir;
230 my $file = Dist::Zilla::File::InMemory->new({
231 name => 'planner/scriptdir.pl',
232 content => $content,
233 });
234 $self->add_file($file);
235 }
236  
237 my $file = first { $_->name eq 'Build.PL' } $self->zilla->files->@*;
238 my $content = $file->content;
239  
240 $content = $self->fill_in_string($content, {
241 version => $self->version,
242 minimum_perl => $self->minimum_perl,
243 dist_name => $self->zilla->name,
244 plugin_title => ref($self) . ' ' . ($self->VERSION || '<self>'),
245 header => $self->header,
246 footer => $self->footer,
247 });
248  
249 $self->log_debug([ 'updating contents of Build.PL in memory' ]);
250 $file->content($content);
251  
252 return;
253 }
254  
255 __PACKAGE__->meta->make_immutable;
256  
257 1;
258  
259 # ABSTRACT: Build a Build.PL that uses Dist::Build
260  
261  
262 # vim: set ts=4 sw=4 noet nolist :
263  
264 __END__