blib/lib/Dist/Zilla/Plugin/ModuleBuild.pm

filter: Statementstmt 100% 92 / 92 Branchbran 87.5% 14 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 6 totaltotal 92.6% 126 / 136 CC 11 SCAR 7.8
CC
11
Cov
97%
module_build_args
5.0
setup_installer
3.0
__ANON__
2.0
CRAP 2.2
« blib/lib/Dist/Zilla/Plugin/GatherFile.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/RemovePrereqs.pm »
1 package Dist::Zilla::Plugin::ModuleBuild 6.040;
2 # ABSTRACT: build a Build.PL that uses Module::Build
3  
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 with (
6 'Dist::Zilla::Role::BuildPL',
7 'Dist::Zilla::Role::PrereqSource',
8 'Dist::Zilla::Role::FileGatherer',
9 'Dist::Zilla::Role::TextTemplate',
10 );
11  
12 3 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use CPAN::Meta::Requirements 2.121; # requirements_for_module
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #pod =head1 DESCRIPTION
22 #pod
23 #pod This plugin will create a F<Build.PL> for installing the dist using
24 #pod L<Module::Build>.
25 #pod
26 #pod =cut
27  
28 #pod =attr mb_version
29 #pod
30 #pod B<Optional:> Specify the minimum version of L<Module::Build> to depend on.
31 #pod
32 #pod Defaults to 0.3601 if a sharedir is being used, otherwise 0.28.
33 #pod
34 #pod =attr mb_class
35 #pod
36 #pod B<Optional:> Specify the class to use to create the build object. Defaults
37 #pod to C<Module::Build> itself. If another class is specified, then the value
38 #pod of mb_lib will be used to generate a line like C<use lib 'inc'> to be added
39 #pod to the generated Build.PL file.
40 #pod
41 #pod =attr mb_lib
42 #pod
43 #pod B<Optional:> Specify the list of directories to be passed to lib when using
44 #pod mb_class. Defaults to C<inc>.
45 #pod
46 #pod =attr build_element
47 #pod
48 #pod Anything given in a C<build_element> option will be added as a build element
49 #pod with Module::Build's L<add_build_element> method.
50 #pod
51 #pod =cut
52  
53 has 'mb_version' => (
54 isa => 'Str',
55 is => 'rw',
56 lazy => 1,
57 7 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 7 my $self = shift;
59 7 ▶ keys %{$self->zilla->_share_dir_map} ? '0.3601' : '0.28';
Branch: 1 branch
BranchTrueFalseTotal
keys %{$self->zilla->_share_dir_map;} ? : 3 4 7
60 },
61 3 );
62  
63 3 has 'mb_class' => (
64 isa => 'Str',
65 is => 'rw',
66 default => 'Module::Build',
67 );
68  
69 3 has 'mb_lib' => (
70 isa => 'Str',
71 is => 'rw',
72 default => 'inc'
73 );
74  
75 has 'build_element' => (
76 isa => 'ArrayRef[Str]',
77 is => 'rw',
78 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 3 );
80  
81 7 ▶sub mvp_multivalue_args { return qw(build_element) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
82  
83 3 my $template = q|
84 # This file was automatically generated by {{ $generated_by }}
85 use strict;
86 use warnings;
87  
88 use Module::Build {{ $plugin->mb_version }};
89 {{ $plugin->_use_custom_class }}
90  
91 my {{ $module_build_args }}
92  
93 my {{ $fallback_build_prereqs }}
94  
95 unless ( eval { Module::Build->VERSION(0.4004) } ) {
96 delete $module_build_args{test_requires};
97 $module_build_args{build_requires} = \%fallback_build_requires;
98 }
99  
100 my $build = {{ $plugin->mb_class }}->new(%module_build_args);
101 {{ $plugin->_add_build_elements }}
102  
103 $build->create_build_script;
104 |;
105  
106 10 ▶sub _use_custom_class {
Subroutine _use_custom_class: called CC=2 SCAR=6.9
107 10 my ($self) = @_;
108 10 my $class = $self->mb_class;
109 10 ▶ if ( $class eq 'Module::Build' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Module::Build') { } 6 4 10
110 6 return "";
111 }
112 else {
113 4 return sprintf "use lib qw{%s}; use $class;", join ' ', split ',', $self->mb_lib;
114 }
115 }
116  
117 14 ▶sub _dump_as {
Subroutine _dump_as: called CC=1 SCAR=0.0
118 14 my ($self, $ref, $name) = @_;
119 14 require Data::Dumper;
120 14 my $dumper = Data::Dumper->new( [ $ref ], [ $name ] );
121 14 $dumper->Sortkeys( 1 );
122 14 $dumper->Indent( 1 );
123 14 $dumper->Useqq( 1 );
124 14 return $dumper->Dump;
125 }
126  
127 8 ▶sub _add_build_elements {
Subroutine _add_build_elements: called CC=2 SCAR=6.9
128 8 ▶ my @elems = @{ shift->build_element } or return '';
Branch: 1 branch
BranchTrueFalseTotal
my(@elems) = @{(shift())->build_element;} or return "" 2 6 8
129 2 return '$build->add_build_element($_) for qw(' . join(' ', @elems) . ');';
130 }
131  
132 7 ▶sub register_prereqs {
Subroutine register_prereqs: called CC=1 SCAR=0.0
Pod undocumented
133 7 my ($self) = @_;
134  
135 7 $self->zilla->register_prereqs(
136 { phase => 'configure' },
137 'Module::Build' => $self->mb_version,
138 );
139  
140 7 $self->zilla->register_prereqs(
141 { phase => 'build' },
142 'Module::Build' => $self->mb_version,
143 );
144 }
145  
146 7 ▶sub module_build_args {
Subroutine module_build_args: called CC=5 SCAR=16.1
Pod undocumented
147 7 my ($self) = @_;
148  
149 1 my @exe_files = map { $_->name }
150 7 @{ $self->zilla->find_files(':ExecFiles') };
151  
152 $self->log_fatal("can't install files with whitespace in their names")
153 7 ▶ if grep { /\s/ } @exe_files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\s/u;} @exe_files 0 7 7
154  
155 7 my $prereqs = $self->zilla->prereqs;
156 7 my %prereqs = (
157 configure_requires => $prereqs->requirements_for(qw(configure requires)),
158 build_requires => $prereqs->requirements_for(qw(build requires)),
159 test_requires => $prereqs->requirements_for(qw(test requires)),
160 requires => $prereqs->requirements_for(qw(runtime requires)),
161 recommends => $prereqs->requirements_for(qw(runtime recommends)),
162 );
163  
164 7 my $name = $self->zilla->name =~ s/-/::/gr;
165  
166 return {
167 module_name => $name,
168 license => $self->zilla->license->meta_yml_name,
169 dist_abstract => $self->zilla->abstract,
170 dist_name => $self->zilla->name,
171 dist_version => $self->zilla->version,
172 7 ▶ dist_author => [ @{ $self->zilla->authors } ],
Branch: 2 branches
BranchTrueFalseTotal
@exe_files ? : 1 6 7
keys %{$self->zilla->_share_dir_map;} ? : 3 4 7
173 @exe_files ? ( script_files => [ sort @exe_files ] ) : (),
174 7 ( keys %{$self->zilla->_share_dir_map} ? (share_dir => $self->zilla->_share_dir_map) : ()),
175  
176 7 ▶ (map {; my $modules = $prereqs{$_}->as_string_hash; keys %$modules ? ( $_ => $modules ) : () } keys %prereqs),
Branch: 1 branch
BranchTrueFalseTotal
keys %$modules ? : 24 11 35
177 recursive_test_files => 1,
178 };
179 }
180  
181 7 ▶sub fallback_build_requires {
Subroutine fallback_build_requires: called CC=2 SCAR=6.9
Pod undocumented
182 7 my $self = shift;
183 7 my $prereqs = $self->zilla->prereqs;
184 7 my $merged = CPAN::Meta::Requirements->new;
185 7 for my $phase ( qw/build test/ ) {
186 14 my $req = $prereqs->requirements_for($phase, 'requires');
187 14 $merged->add_requirements( $req );
188 };
189 7 return $self->_dump_as( $merged->as_string_hash, '*fallback_build_requires' );
190 }
191  
192 7 ▶sub gather_files {
Subroutine gather_files: called CC=1 SCAR=0.0
Pod undocumented
193 7 my ($self) = @_;
194  
195 7 require Dist::Zilla::File::InMemory;
196  
197 7 my $file = Dist::Zilla::File::InMemory->new({
198 name => 'Build.PL',
199 content => $template, # template evaluated later
200 });
201  
202 7 $self->add_file($file);
203 7 return;
204 }
205  
206 7 ▶sub setup_installer {
Subroutine setup_installer: called CC=3 SCAR=11.0
Pod undocumented
207 7 my ($self) = @_;
208  
209 7 ▶ $self->log_fatal("can't build Build.PL; license has no known META.yml value")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->zilla->license->meta_yml_name 0 7 7
210 unless $self->zilla->license->meta_yml_name;
211  
212 7 my $module_build_args = $self->module_build_args;
213  
214 7 $self->__module_build_args($module_build_args);
215  
216 7 my $dumped_args = $self->_dump_as($module_build_args, '*module_build_args');
217  
218 7 my $fallback_build_requires = $self->fallback_build_requires;
219  
220 7 ▶ my $file = first { $_->name eq 'Build.PL' } @{$self->zilla->files};
Subroutine __ANON__: called CC=1 SCAR=0.0
221  
222 7 $self->log_debug([ 'updating contents of Build.PL in memory' ]);
223 7 ▶ my $content = $self->fill_in_string(
Condition50%1/2
$self->VERSION || '(dev)'
l!l
70
MC/DC0%0/1
$self->VERSION || '(dev)'
$self->VERSION
Truth table50%1/2
A
A= $self->VERSION
Aresult
11
00
224 $file->content,
225 {
226 plugin => \$self,
227 module_build_args => \$dumped_args,
228 fallback_build_prereqs => \$fallback_build_requires,
229 generated_by => \sprintf("%s v%s",
230 ref($self), $self->VERSION || '(dev)'),
231 },
232 );
233  
234 7 $file->content($content);
235  
236 7 return;
237 }
238  
239 # XXX: Just here to facilitate testing. -- rjbs, 2010-03-20
240 3 has __module_build_args => (
241 is => 'rw',
242 isa => 'HashRef',
243 );
244  
245 3 __PACKAGE__->meta->make_immutable;
246 3 1;
247  
248 #pod =head1 SEE ALSO
249 #pod
250 #pod Core Dist::Zilla plugins:
251 #pod L<@Basic|Dist::Zilla::PluginBundle::Basic>,
252 #pod L<@Filter|Dist::Zilla::PluginBundle::Filter>,
253 #pod L<MakeMaker|Dist::Zilla::Plugin::MakeMaker>,
254 #pod L<Manifest|Dist::Zilla::Plugin::Manifest>.
255 #pod
256 #pod Dist::Zilla roles:
257 #pod L<BuildPL|Dist::Zilla::Role::BuildPL>.
258 #pod
259 #pod =cut
260  
261 __END__
« blib/lib/Dist/Zilla/Plugin/GatherFile.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/RemovePrereqs.pm »