File Coverage

blib/lib/Dist/Zilla/Plugin/ModuleBuild.pm
Criterion Covered Total %
statement 79 79 100.0
branch 12 14 85.7
condition 1 2 50.0
subroutine 17 17 100.0
pod 0 6 0.0
total 109 118 92.3


line stmt bran cond sub pod time code
1             # ABSTRACT: build a Build.PL that uses Module::Build
2              
3             use Moose;
4 3     3   2825 with (
  3         8  
  3         26  
5             'Dist::Zilla::Role::BuildPL',
6             'Dist::Zilla::Role::PrereqSource',
7             'Dist::Zilla::Role::FileGatherer',
8             'Dist::Zilla::Role::TextTemplate',
9             );
10              
11             use Dist::Zilla::Pragmas;
12 3     3   20752  
  3         9  
  3         28  
13             use namespace::autoclean;
14 3     3   23  
  3         9  
  3         28  
15             use CPAN::Meta::Requirements 2.121; # requirements_for_module
16 3     3   290 use Dist::Zilla::File::InMemory;
  3         88  
  3         72  
17 3     3   393 use List::Util 'first';
  3         8  
  3         120  
18 3     3   22 use Data::Dumper;
  3         6  
  3         249  
19 3     3   1328  
  3         12212  
  3         4371  
20             #pod =head1 DESCRIPTION
21             #pod
22             #pod This plugin will create a F<Build.PL> for installing the dist using
23             #pod L<Module::Build>.
24             #pod
25             #pod =cut
26              
27             #pod =attr mb_version
28             #pod
29             #pod B<Optional:> Specify the minimum version of L<Module::Build> to depend on.
30             #pod
31             #pod Defaults to 0.3601 if a sharedir is being used, otherwise 0.28.
32             #pod
33             #pod =attr mb_class
34             #pod
35             #pod B<Optional:> Specify the class to use to create the build object. Defaults
36             #pod to C<Module::Build> itself. If another class is specified, then the value
37             #pod of mb_lib will be used to generate a line like C<use lib 'inc'> to be added
38             #pod to the generated Build.PL file.
39             #pod
40             #pod =attr mb_lib
41             #pod
42             #pod B<Optional:> Specify the list of directories to be passed to lib when using
43             #pod mb_class. Defaults to C<inc>.
44             #pod
45             #pod =attr build_element
46             #pod
47             #pod Anything given in a C<build_element> option will be added as a build element
48             #pod with Module::Build's L<add_build_element> method.
49             #pod
50             #pod =cut
51              
52             has 'mb_version' => (
53             isa => 'Str',
54             is => 'rw',
55             lazy => 1,
56             default => sub {
57             my $self = shift;
58             keys %{$self->zilla->_share_dir_map} ? '0.3601' : '0.28';
59             },
60             );
61              
62             has 'mb_class' => (
63             isa => 'Str',
64             is => 'rw',
65             default => 'Module::Build',
66             );
67              
68             has 'mb_lib' => (
69             isa => 'Str',
70             is => 'rw',
71             default => 'inc'
72             );
73              
74             has 'build_element' => (
75             isa => 'ArrayRef[Str]',
76             is => 'rw',
77             default => sub { [] },
78             );
79              
80              
81 7     7 0 1631 my $template = q|
82             # This file was automatically generated by {{ $generated_by }}.
83             use strict;
84             use warnings;
85              
86             use Module::Build {{ $plugin->mb_version }};
87             {{ $plugin->_use_custom_class }}
88              
89             my {{ $module_build_args }}
90              
91             my {{ $fallback_build_prereqs }}
92              
93             unless ( eval { Module::Build->VERSION(0.4004) } ) {
94             delete $module_build_args{test_requires};
95             $module_build_args{build_requires} = \%fallback_build_requires;
96             }
97              
98             my $build = {{ $plugin->mb_class }}->new(%module_build_args);
99             {{ $plugin->_add_build_elements }}
100              
101             $build->create_build_script;
102             |;
103              
104             my ($self) = @_;
105             my $class = $self->mb_class;
106             if ( $class eq 'Module::Build' ) {
107 10     10   19389 return "";
108 10         346 }
109 10 100       36 else {
110 6         47 return sprintf "use lib qw{%s}; use $class;", join ' ', split ',', $self->mb_lib;
111             }
112             }
113 4         114  
114             my ($self, $ref, $name) = @_;
115             require Data::Dumper;
116             my $dumper = Data::Dumper->new( [ $ref ], [ $name ] );
117             $dumper->Sortkeys( 1 );
118 14     14   693 $dumper->Indent( 1 );
119 14         84 $dumper->Useqq( 1 );
120 14         122 return $dumper->Dump;
121 14         547 }
122 14         115  
123 14         190 my @elems = @{ shift->build_element } or return '';
124 14         125 return '$build->add_build_element($_) for qw(' . join(' ', @elems) . ');';
125             }
126              
127             my ($self) = @_;
128 8 100   8   469  
  8         275  
129 2         16 $self->zilla->register_prereqs(
130             { phase => 'configure' },
131             'Module::Build' => $self->mb_version,
132             );
133 7     7 0 24  
134             $self->zilla->register_prereqs(
135 7         222 { phase => 'build' },
136             'Module::Build' => $self->mb_version,
137             );
138             }
139              
140 7         230 my ($self) = @_;
141              
142             my @exe_files = map { $_->name }
143             @{ $self->zilla->find_files(':ExecFiles') };
144              
145             $self->log_fatal("can't install files with whitespace in their names")
146             if grep { /\s/ } @exe_files;
147 7     7 0 19  
148             my $prereqs = $self->zilla->prereqs;
149 1         5 my %prereqs = (
150 7         16 configure_requires => $prereqs->requirements_for(qw(configure requires)),
  7         183  
151             build_requires => $prereqs->requirements_for(qw(build requires)),
152             test_requires => $prereqs->requirements_for(qw(test requires)),
153 7 50       39 requires => $prereqs->requirements_for(qw(runtime requires)),
  1         7  
154             recommends => $prereqs->requirements_for(qw(runtime recommends)),
155 7         214 );
156 7         45  
157             my $name = $self->zilla->name =~ s/-/::/gr;
158              
159             return {
160             module_name => $name,
161             license => $self->zilla->license->meta_yml_name,
162             dist_abstract => $self->zilla->abstract,
163             dist_name => $self->zilla->name,
164 7         575 dist_version => $self->zilla->version,
165             dist_author => [ @{ $self->zilla->authors } ],
166             @exe_files ? ( script_files => [ sort @exe_files ] ) : (),
167             ( keys %{$self->zilla->_share_dir_map} ? (share_dir => $self->zilla->_share_dir_map) : ()),
168              
169             (map {; my $modules = $prereqs{$_}->as_string_hash; keys %$modules ? ( $_ => $modules ) : () } keys %prereqs),
170             recursive_test_files => 1,
171             };
172 7         188 }
173              
174 7         179 my $self = shift;
175             my $prereqs = $self->zilla->prereqs;
176 7 100       206 my $merged = CPAN::Meta::Requirements->new;
  35 100       97  
  35 100       1440  
177             for my $phase ( qw/build test/ ) {
178             my $req = $prereqs->requirements_for($phase, 'requires');
179             $merged->add_requirements( $req );
180             };
181             return $self->_dump_as( $merged->as_string_hash, '*fallback_build_requires' );
182 7     7 0 15 }
183 7         227  
184 7         29 my ($self) = @_;
185 7         117  
186 14         1113 require Dist::Zilla::File::InMemory;
187 14         582  
188             my $file = Dist::Zilla::File::InMemory->new({
189 7         419 name => 'Build.PL',
190             content => $template, # template evaluated later
191             });
192              
193 7     7 0 26 $self->add_file($file);
194             return;
195 7         58 }
196              
197 7         283 my ($self) = @_;
198              
199             $self->log_fatal("can't build Build.PL; license has no known META.yml value")
200             unless $self->zilla->license->meta_yml_name;
201              
202 7         41 my $module_build_args = $self->module_build_args;
203 7         26  
204             $self->__module_build_args($module_build_args);
205              
206             my $dumped_args = $self->_dump_as($module_build_args, '*module_build_args');
207 7     7 0 24  
208             my $fallback_build_requires = $self->fallback_build_requires;
209 7 50       251  
210             my $file = first { $_->name eq 'Build.PL' } @{$self->zilla->files};
211              
212 7         92 $self->log_debug([ 'updating contents of Build.PL in memory' ]);
213             my $content = $self->fill_in_string(
214 7         269 $file->content,
215             {
216 7         40 plugin => \$self,
217             module_build_args => \$dumped_args,
218 7         728 fallback_build_prereqs => \$fallback_build_requires,
219             generated_by => \sprintf("%s v%s",
220 7     34   281 ref($self), $self->VERSION || '(dev)'),
  34         125  
  7         231  
221             },
222 7         69 );
223 7   50     301  
224             $file->content($content);
225              
226             return;
227             }
228              
229             # XXX: Just here to facilitate testing. -- rjbs, 2010-03-20
230             has __module_build_args => (
231             is => 'rw',
232             isa => 'HashRef',
233             );
234 7         45  
235             __PACKAGE__->meta->make_immutable;
236 7         33 1;
237              
238             #pod =head1 SEE ALSO
239             #pod
240             #pod Core Dist::Zilla plugins:
241             #pod L<@Basic|Dist::Zilla::PluginBundle::Basic>,
242             #pod L<@Filter|Dist::Zilla::PluginBundle::Filter>,
243             #pod L<MakeMaker|Dist::Zilla::Plugin::MakeMaker>,
244             #pod L<Manifest|Dist::Zilla::Plugin::Manifest>.
245             #pod
246             #pod Dist::Zilla roles:
247             #pod L<BuildPL|Dist::Zilla::Role::BuildPL>.
248             #pod
249             #pod =cut
250              
251              
252             =pod
253              
254             =encoding UTF-8
255              
256             =head1 NAME
257              
258             Dist::Zilla::Plugin::ModuleBuild - build a Build.PL that uses Module::Build
259              
260             =head1 VERSION
261              
262             version 6.028
263              
264             =head1 DESCRIPTION
265              
266             This plugin will create a F<Build.PL> for installing the dist using
267             L<Module::Build>.
268              
269             =head1 PERL VERSION
270              
271             This module should work on any version of perl still receiving updates from
272             the Perl 5 Porters. This means it should work on any version of perl released
273             in the last two to three years. (That is, if the most recently released
274             version is v5.40, then this module should work on both v5.40 and v5.38.)
275              
276             Although it may work on older versions of perl, no guarantee is made that the
277             minimum required version will not be increased. The version may be increased
278             for any reason, and there is no promise that patches will be accepted to lower
279             the minimum required perl.
280              
281             =head1 ATTRIBUTES
282              
283             =head2 mb_version
284              
285             B<Optional:> Specify the minimum version of L<Module::Build> to depend on.
286              
287             Defaults to 0.3601 if a sharedir is being used, otherwise 0.28.
288              
289             =head2 mb_class
290              
291             B<Optional:> Specify the class to use to create the build object. Defaults
292             to C<Module::Build> itself. If another class is specified, then the value
293             of mb_lib will be used to generate a line like C<use lib 'inc'> to be added
294             to the generated Build.PL file.
295              
296             =head2 mb_lib
297              
298             B<Optional:> Specify the list of directories to be passed to lib when using
299             mb_class. Defaults to C<inc>.
300              
301             =head2 build_element
302              
303             Anything given in a C<build_element> option will be added as a build element
304             with Module::Build's L<add_build_element> method.
305              
306             =head1 SEE ALSO
307              
308             Core Dist::Zilla plugins:
309             L<@Basic|Dist::Zilla::PluginBundle::Basic>,
310             L<@Filter|Dist::Zilla::PluginBundle::Filter>,
311             L<MakeMaker|Dist::Zilla::Plugin::MakeMaker>,
312             L<Manifest|Dist::Zilla::Plugin::Manifest>.
313              
314             Dist::Zilla roles:
315             L<BuildPL|Dist::Zilla::Role::BuildPL>.
316              
317             =head1 AUTHOR
318              
319             Ricardo SIGNES 😏 <cpan@semiotic.systems>
320              
321             =head1 COPYRIGHT AND LICENSE
322              
323             This software is copyright (c) 2022 by Ricardo SIGNES.
324              
325             This is free software; you can redistribute it and/or modify it under
326             the same terms as the Perl 5 programming language system itself.
327              
328             =cut