File Coverage

blib/lib/Dist/Zilla/Plugin/CheckBin.pm
Criterion Covered Total %
statement 42 44 95.4
branch 7 10 70.0
condition n/a
subroutine 10 10 100.0
pod 0 4 0.0
total 59 68 86.7


line stmt bran cond sub pod time code
1 2     2   5773190 use strict;
  2         5  
  2         61  
2 2     2   10 use warnings;
  2         4  
  2         106  
3             package Dist::Zilla::Plugin::CheckBin; # git description: v0.007-26-g6b1e5af
4             # vim: set ts=8 sts=4 sw=4 tw=115 et :
5             # ABSTRACT: Require that our distribution has a particular command available
6             # KEYWORDS: distribution installation require binary program executable
7              
8             our $VERSION = '0.008';
9              
10 2     2   11 use Moose;
  2         2  
  2         14  
11             with
12             'Dist::Zilla::Role::FileMunger',
13             'Dist::Zilla::Role::InstallTool',
14             'Dist::Zilla::Role::PrereqSource',
15             ;
16 2     2   11321 use Scalar::Util 'blessed';
  2         6  
  2         111  
17 2     2   12 use namespace::autoclean;
  2         3  
  2         18  
18              
19 2     2 0 1009 sub mvp_multivalue_args { 'command' }
20              
21             has command => (
22             isa => 'ArrayRef[Str]',
23             lazy => 1,
24             default => sub { [] },
25             traits => ['Array'],
26             handles => { command => 'sort' }, # sorted elements
27             );
28              
29             around dump_config => sub
30             {
31             my ($orig, $self) = @_;
32             my $config = $self->$orig;
33              
34             $config->{+__PACKAGE__} = {
35             command => [ $self->command ],
36             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
37             };
38              
39             return $config;
40             };
41              
42             sub register_prereqs
43             {
44 2     2 0 3770 my $self = shift;
45 2         49 $self->zilla->register_prereqs(
46             {
47             phase => 'configure',
48             type => 'requires',
49             },
50             'Devel::CheckBin' => '0',
51             );
52             }
53              
54             my %files;
55             sub munge_files
56             {
57 2     2 0 127001 my $self = shift;
58              
59 2 100       4 my @mfpl = grep { $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' } @{ $self->zilla->files };
  6         407  
  2         58  
60 2         115 for my $mfpl (@mfpl)
61             {
62 2         10 $self->log_debug([ 'munging %s in file gatherer phase', $mfpl->name ]);
63 2         776 $files{$mfpl->name} = $mfpl;
64 2         91 $self->_munge_file($mfpl);
65             }
66 2         458 return;
67             }
68              
69             # XXX - this should really be a separate phase that runs after InstallTool -
70             # until then, all we can do is die if we are run too soon
71             sub setup_installer
72             {
73 2     2 0 31267 my $self = shift;
74              
75 2 100       6 my @mfpl = grep { $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' } @{ $self->zilla->files };
  6         388  
  2         48  
76              
77 2 50       117 $self->log_fatal('No Makefile.PL or Build.PL was found. [CheckBin] should appear in dist.ini after [MakeMaker] or variant!') unless @mfpl;
78              
79 2         7 for my $mfpl (@mfpl)
80             {
81 2 50       8 next if exists $files{$mfpl->name};
82 0         0 $self->log_debug([ 'munging %s in setup_installer phase', $mfpl->name ]);
83 0         0 $self->_munge_file($mfpl);
84             }
85 2         76 return;
86             }
87              
88             sub _munge_file
89             {
90 2     2   7 my ($self, $file) = @_;
91              
92 2         10 my $orig_content = $file->content;
93 2 50       144 $self->log_fatal('could not find position in ' . $file->name . ' to modify!')
94             if not $orig_content =~ m/use strict;\nuse warnings;\n\n/g;
95              
96 2         6 my $pos = pos($orig_content);
97              
98             my $content =
99             '# inserted by ' . blessed($self) . ' ' . $self->VERSION . "\n"
100             . "use Devel::CheckBin;\n"
101 2         96 . join('', map { 'check_bin(\'' . $_ . "\');\n" } $self->command)
  4         17  
102             . "\n";
103              
104 2         21 $file->content(
105             substr($orig_content, 0, $pos)
106             . $content
107             . substr($orig_content, $pos)
108             );
109             }
110              
111             __PACKAGE__->meta->make_immutable;
112              
113             __END__
114              
115             =pod
116              
117             =encoding UTF-8
118              
119             =head1 NAME
120              
121             Dist::Zilla::Plugin::CheckBin - Require that our distribution has a particular command available
122              
123             =head1 VERSION
124              
125             version 0.008
126              
127             =head1 SYNOPSIS
128              
129             In your F<dist.ini>:
130              
131             [CheckBin]
132             command = ls
133              
134             =head1 DESCRIPTION
135              
136             L<Dist::Zilla::Plugin::CheckBin> is a L<Dist::Zilla> plugin that modifies the
137             F<Makefile.PL> or F<Build.PL> in your distribution to contain a
138             L<Devel::CheckBin> call, that asserts that a particular command is available.
139             If it is not available, the program exits with a status of zero, which on a
140             L<CPAN Testers|cpantesters.org> machine will result in a NA result.
141              
142             =for Pod::Coverage mvp_multivalue_args register_prereqs munge_files setup_installer
143              
144             =head1 CONFIGURATION OPTIONS
145              
146             =head2 C<command>
147              
148             Identifies the name of the command that is searched for. Can be used more than once.
149              
150             =head1 SEE ALSO
151              
152             =over 4
153              
154             =item *
155              
156             L<Devel::CheckBin>
157              
158             =item *
159              
160             L<Devel::AssertOS> and L<Dist::Zilla::Plugin::AssertOS>
161              
162             =item *
163              
164             L<Devel::CheckLib> and L<Dist::Zilla::Plugin::CheckLib>
165              
166             =back
167              
168             =head1 SUPPORT
169              
170             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-CheckBin>
171             (or L<bug-Dist-Zilla-Plugin-CheckBin@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-CheckBin@rt.cpan.org>).
172              
173             There is also a mailing list available for users of this distribution, at
174             L<http://dzil.org/#mailing-list>.
175              
176             There is also an irc channel available for users of this distribution, at
177             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
178              
179             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
180              
181             =head1 AUTHOR
182              
183             Karen Etheridge <ether@cpan.org>
184              
185             =head1 COPYRIGHT AND LICENCE
186              
187             This software is copyright (c) 2014 by Karen Etheridge.
188              
189             This is free software; you can redistribute it and/or modify it under
190             the same terms as the Perl 5 programming language system itself.
191              
192             =cut