File Coverage

blib/lib/Dist/Zilla/Plugin/ModuleBuildTiny.pm
Criterion Covered Total %
statement 66 75 88.0
branch 9 20 45.0
condition 5 11 45.4
subroutine 20 20 100.0
pod 0 7 0.0
total 100 133 75.1


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::ModuleBuildTiny;
2             $Dist::Zilla::Plugin::ModuleBuildTiny::VERSION = '0.017';
3 4     4   12788909 use Moose;
  4         14  
  4         24  
4             with qw/
5             Dist::Zilla::Role::BuildPL
6             Dist::Zilla::Role::TextTemplate
7             Dist::Zilla::Role::PrereqSource
8             Dist::Zilla::Role::FileGatherer
9             Dist::Zilla::Role::MetaProvider
10             /;
11              
12 4     4   26415 use Dist::Zilla 4.300039;
  4         118  
  4         118  
13 4     4   1309 use Module::Metadata;
  4         12372  
  4         144  
14 4     4   31 use Moose::Util::TypeConstraints 'enum';
  4         12  
  4         53  
15 4     4   2099 use MooseX::Types::Perl qw/StrictVersionStr/;
  4         26  
  4         81  
16 4     4   10163 use MooseX::Types::Moose qw(Str ArrayRef HashRef);
  4         23  
  4         43  
17 4     4   21125 use List::Util 1.33 qw/first any/;
  4         94  
  4         6728  
18              
19 7     7 0 188247 sub mvp_multivalue_args { qw(header_strs footer_strs) }
20              
21             sub mvp_aliases {
22             +{
23 7     7 0 1271 header => 'header_strs',
24             footer => 'footer_strs',
25             }
26             }
27              
28             around BUILDARGS => sub {
29             my $orig = shift;
30             my $class = shift;
31              
32             my $args = $class->$orig(@_);
33              
34             my $delimiter = delete $args->{delimiter};
35             if (defined $delimiter and length($delimiter)) {
36             foreach my $arg (grep exists $args->{$_}, qw(header_strs footer_strs)) {
37             s/^\Q$delimiter\E// foreach @{$args->{$arg}};
38             }
39             }
40              
41             return $args;
42             };
43              
44             has version_method => (
45             is => 'ro',
46             isa => enum(['installed', 'conservative']),
47             default => 'conservative',
48             );
49              
50             has has_pl => (
51             is => 'ro',
52             isa => 'Bool',
53             lazy => 1,
54             default => sub {
55             my $self = shift;
56             return any { $_->name =~ /^lib\/.*\.PL$/ } @{ $self->zilla->files };
57             },
58             );
59              
60             has has_xs => (
61             is => 'ro',
62             isa => 'Bool',
63             lazy => 1,
64             default => sub {
65             my $self = shift;
66             return any { $_->name =~ /^lib\/.*\.xs$/ } @{ $self->zilla->files };
67             },
68             );
69              
70             has static => (
71             is => 'ro',
72             isa => enum([qw/no yes auto/]),
73             default => 'no',
74             );
75              
76             has version => (
77             is => 'ro',
78             lazy => 1,
79             isa => StrictVersionStr,
80             default => sub {
81             my $self = shift;
82             if ($self->version_method eq 'installed') {
83             return Module::Metadata->new_from_module('Module::Build::Tiny')->version->stringify;
84             }
85             elsif (-e 'include/' or any { $_->name =~ /^src\/.*\.c$/} @{ $self->zilla->files }) {
86             return '0.044';
87             }
88             elsif ($self->has_pl) {
89             return '0.039';
90             }
91             elsif ($self->has_xs) {
92             return '0.036';
93             }
94             return '0.034'; # _build_params format
95             },
96             );
97              
98             has minimum_perl => (
99             is => 'ro',
100             isa => StrictVersionStr,
101             lazy => 1,
102             default => sub {
103             my $self = shift;
104             my $prereqs = $self->zilla->prereqs->cpan_meta_prereqs;
105             my $reqs = $prereqs->merged_requirements([ qw/configure build test runtime/ ], ['requires']);
106             return $reqs->requirements_for_module('perl') || '5.006';
107             },
108             );
109              
110             has header_strs => (
111             is => 'ro', isa => ArrayRef[Str],
112             traits => ['Array'],
113             lazy => 1,
114             default => sub { [] },
115             documentation => "Additional code lines to include at the beginning of Makefile.PL",
116             );
117              
118             has header_file => (
119             is => 'ro', isa => Str,
120             documentation => 'Additional header content to include from a file',
121             );
122              
123             has header => (
124             is => 'ro',
125             isa => Str,
126             lazy => 1,
127             builder => '_build_header',
128             documentation => "A string included at the beginning of Makefile.PL",
129             );
130              
131             sub _build_header {
132 5     5   25 my $self = shift;
133             join "\n",
134 5         198 @{$self->header_strs},
135             ( $self->header_file
136 5 50       14 ? do {
137 0         0 my $abs_file = path($self->zilla->root, $self->header_file);
138 0 0       0 $self->log_fatal([ 'header_file %s does not exist!', $self->header_file ])
139             if not $abs_file->exists;
140 0         0 $abs_file->slurp_utf8
141             }
142             : () );
143             }
144              
145             has footer_strs => (
146             is => 'ro', isa => ArrayRef[Str],
147             traits => ['Array'],
148             lazy => 1,
149             default => sub { [] },
150             documentation => "Additional code lines to include at the end of Makefile.PL",
151             );
152              
153             has footer_file => (
154             is => 'ro', isa => Str,
155             documentation => 'Additional footer content to include from a file',
156             );
157              
158             has footer => (
159             is => 'ro',
160             isa => Str,
161             lazy => 1,
162             builder => '_build_footer',
163             documentation => "A string included at the end of Makefile.PL",
164             );
165              
166             sub _build_footer {
167 5     5   18 my $self = shift;
168             join "\n",
169 5         203 @{$self->footer_strs},
170             ( $self->footer_file
171 5 50       17 ? do {
172 0         0 my $abs_file = path($self->zilla->root, $self->footer_file);
173 0 0       0 $self->log_fatal([ 'footer_file %s does not exist!', $self->footer_file ])
174             if not $abs_file->exists;
175 0         0 $abs_file->slurp_utf8
176             }
177             : () );
178             }
179              
180              
181             my $template = <<'BUILD_PL';
182             # This Build.PL for {{ $dist_name }} was generated by {{ $plugin_title }}.
183             use strict;
184             use warnings;
185              
186             {{ $header }}
187             use {{ $minimum_perl }};
188             use Module::Build::Tiny{{ $version ne 0 && " $version" }};
189             Build_PL();
190             {{ $footer }}
191             BUILD_PL
192              
193             sub register_prereqs {
194 7     7 0 191693 my ($self) = @_;
195              
196 7         254 $self->zilla->register_prereqs({ phase => 'configure' }, 'Module::Build::Tiny' => $self->version);
197              
198 7         5406 return;
199             }
200              
201             sub can_static {
202 3     3 0 19 my $self = shift;
203 3   33     104 return !$self->has_pl && !$self->has_xs;
204             }
205              
206             sub metadata {
207 7     7 0 48153 my $self = shift;
208 7   66     312 my $static = $self->static eq 'yes' || $self->static eq 'auto' && $self->can_static;
209 7 100       60 return $static ? { x_static_install => 1 } : ();
210             }
211              
212             sub gather_files {
213 7     7 0 456533 my ($self) = @_;
214              
215 7 50   4   49 if (my $file = first { $_->name eq 'Build.PL' } @{$self->zilla->files})
  4         175  
  7         237  
216             {
217             # if it's another type, some other plugin added it, so it's better to
218             # error out and let the developer sort out what went wrong.
219 0 0       0 if ($file->isa('Dist::Zilla::File::OnDisk'))
220             {
221 0         0 $self->log('replacing existing Build.PL found in repository');
222 0         0 $self->zilla->prune_file($file);
223             }
224             }
225              
226 7         2807 require Dist::Zilla::File::InMemory;
227 7         370792 my $file = Dist::Zilla::File::InMemory->new({
228             name => 'Build.PL',
229             content => $template, # template evaluated later
230             });
231              
232 7         3112 $self->add_file($file);
233 7         3134 return;
234             }
235              
236             sub setup_installer {
237 7     7 0 49098 my ($self, $arg) = @_;
238              
239 7 50       269 confess 'Module::Build::Tiny is currently incompatible with dynamic_config' if $self->zilla->distmeta->{dynamic_config};
240              
241 7         294 for my $map (map { $_->share_dir_map } @{$self->zilla->plugins_with(-ShareDir)}) {
  2         1149  
  7         196  
242 2 100       367 $self->log_fatal('Unsupported use of a module sharedir') if exists $map->{module};
243 1 50 33     17 $self->log_fatal('Sharedir location must be share/') if defined $map->{dist} and $map->{dist} ne 'share';
244             }
245              
246 5     5   2808 my $file = first { $_->name eq 'Build.PL' } @{$self->zilla->files};
  5         244  
  5         175  
247 5         329 my $content = $file->content;
248              
249 5   50     624 $content = $self->fill_in_string($content, {
250             version => $self->version,
251             minimum_perl => $self->minimum_perl,
252             dist_name => $self->zilla->name,
253             plugin_title => ref($self) . ' ' . ($self->VERSION || '<self>'),
254             header => $self->header,
255             footer => $self->footer,
256             });
257              
258 5         9779 $self->log_debug([ 'updating contents of Build.PL in memory' ]);
259 5         484 $file->content($content);
260              
261 5         1700 return;
262             }
263              
264             __PACKAGE__->meta->make_immutable;
265 4     4   38 no Moose::Util::TypeConstraints;
  4         10  
  4         33  
266 4     4   679 no Moose;
  4         24  
  4         34  
267             1;
268              
269             # ABSTRACT: Build a Build.PL that uses Module::Build::Tiny
270              
271              
272             # vim: set ts=4 sw=4 noet nolist :
273              
274             __END__
275              
276             =pod
277              
278             =encoding UTF-8
279              
280             =head1 NAME
281              
282             Dist::Zilla::Plugin::ModuleBuildTiny - Build a Build.PL that uses Module::Build::Tiny
283              
284             =head1 VERSION
285              
286             version 0.017
287              
288             =head1 DESCRIPTION
289              
290             This plugin will create a F<Build.PL> for installing the dist using L<Module::Build::Tiny|Module::Build::Tiny>.
291              
292             =head1 ATTRIBUTES
293              
294             =head2 version
295              
296             B<Optional:> Specify the minimum version of L<Module::Build::Tiny|Module::Build::Tiny> to depend on.
297              
298             Defaults to the version determined by C<version_method>.
299              
300             =head2 version_method
301              
302             This attribute determines how the default minimum perl is detected. It has two possible values:
303              
304             =over 4
305              
306             =item * installed
307              
308             This will give the version installed on the author's perl installation.
309              
310             =item * conservative
311              
312             This will return a heuristically determined minimum version of MBT.
313              
314             =back
315              
316             =head2 minimum_perl
317              
318             B<Optional:> Specify the minimum version of perl to require in the F<Build.PL>.
319              
320             This is normally taken from dzil's prereq metadata.
321              
322             =head2 static
323              
324             This is an option to set the B<HIGHLY EXPERIMENTAL> C<x_static_install>
325             metadata field. B<DO NOT USE THIS OPTION> if you are not involved in its
326             testing with the Perl Toolchain Gang.
327              
328             It has three possible values:
329              
330             =over 4
331              
332             =item * no
333              
334             No extra metadata is added. This is the default setting.
335              
336             =item * yes
337              
338             Sets C<x_static_install = 1> in metadata.
339              
340             =item * auto
341              
342             Sets C<x_static_install = 1> in metadata if the distribution appears to be
343             compatible - presently only the existence of F<.PL> and F<.xs> files are
344             checked.
345              
346             =back
347              
348             =head2 header
349              
350             A line of code which is included near the top of F<Build.PL>. Can be used more than once.
351              
352             =head2 footer
353              
354             A line of code which is included at the bottom of F<Build.PL>. Can be used more than once.
355              
356             =head2 delimiter
357              
358             A string, usually a single character, which is stripped from the beginning of
359             all C<header>, and C<footer> lines. This is because the
360             INI file format strips all leading whitespace from option values, so including
361             this character at the front allows you to use leading whitespace in an option
362             string. This is helpful for the formatting of F<Build.PL>s, but a nice thing
363             to have when inserting any block of code.
364              
365             =head1 AUTHOR
366              
367             Leon Timmermans <fawaka@gmail.com>
368              
369             =head1 COPYRIGHT AND LICENSE
370              
371             This software is copyright (c) 2011 by Leon Timmermans.
372              
373             This is free software; you can redistribute it and/or modify it under
374             the same terms as the Perl 5 programming language system itself.
375              
376             =cut