File Coverage

blib/lib/Dist/Zilla/Plugin/CheckLib.pm
Criterion Covered Total %
statement 46 48 95.8
branch 12 16 75.0
condition n/a
subroutine 10 10 100.0
pod 0 4 0.0
total 68 78 87.1


line stmt bran cond sub pod time code
1 2     2   6541959 use strict;
  2         5  
  2         66  
2 2     2   10 use warnings;
  2         3  
  2         138  
3             package Dist::Zilla::Plugin::CheckLib; # git description: v0.006-28-g53d21aa
4             # vim: set ts=8 sts=4 sw=4 tw=115 et :
5             # ABSTRACT: Require that your distribution has a particular library available
6             # KEYWORDS: distribution installation require compiler library header resource
7              
8             our $VERSION = '0.007';
9              
10 2     2   13 use Moose;
  2         4  
  2         15  
11             with
12             'Dist::Zilla::Role::FileMunger',
13             'Dist::Zilla::Role::InstallTool',
14             'Dist::Zilla::Role::PrereqSource',
15             ;
16 2     2   13052 use Scalar::Util 'blessed';
  2         5  
  2         110  
17 2     2   13 use namespace::autoclean;
  2         4  
  2         21  
18              
19             my @list_options = qw(header incpath lib libpath);
20 2     2 0 1144 sub mvp_multivalue_args { @list_options }
21              
22             has $_ => (
23             isa => 'ArrayRef[Str]',
24             lazy => 1,
25             default => sub { [] },
26             traits => ['Array'],
27             handles => { $_ => 'sort' },
28             ) foreach @list_options;
29              
30             my @string_options = qw(INC LIBS debug);
31             has \@string_options => (
32             is => 'ro', isa => 'Str',
33             );
34              
35             around dump_config => sub
36             {
37             my ($orig, $self) = @_;
38             my $config = $self->$orig;
39              
40             $config->{+__PACKAGE__} = {
41             ( map { $_ => [ $self->$_ ] } @list_options ),
42             ( map { $_ => $self->$_ } @string_options ),
43             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
44             };
45              
46             return $config;
47             };
48              
49             sub register_prereqs
50             {
51 2     2 0 4129 my $self = shift;
52 2         54 $self->zilla->register_prereqs(
53             {
54             phase => 'configure',
55             type => 'requires',
56             },
57             'Devel::CheckLib' => '0.9',
58             );
59             }
60              
61             my %files;
62             sub munge_files
63             {
64 2     2 0 140412 my $self = shift;
65              
66 2 100       6 my @mfpl = grep { $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' } @{ $self->zilla->files };
  6         410  
  2         83  
67 2         112 for my $mfpl (@mfpl)
68             {
69 2         9 $self->log_debug([ 'munging %s in file gatherer phase', $mfpl->name ]);
70 2         952 $files{$mfpl->name} = $mfpl;
71 2         96 $self->_munge_file($mfpl);
72             }
73 2         478 return;
74             }
75              
76             # XXX - this should really be a separate phase that runs after InstallTool -
77             # until then, all we can do is die if we are run too soon
78             sub setup_installer
79             {
80 2     2 0 32857 my $self = shift;
81              
82 2 100       8 my @mfpl = grep { $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' } @{ $self->zilla->files };
  6         590  
  2         77  
83              
84 2 50       164 $self->log_fatal('No Makefile.PL or Build.PL was found. [CheckLib] should appear in dist.ini after [MakeMaker] or variant!') unless @mfpl;
85              
86 2         7 for my $mfpl (@mfpl)
87             {
88 2 50       24 next if exists $files{$mfpl->name};
89 0         0 $self->log_debug([ 'munging %s in setup_installer phase', $mfpl->name ]);
90 0         0 $self->_munge_file($mfpl);
91             }
92 2         84 return;
93             }
94              
95             sub _munge_file
96             {
97 2     2   6 my ($self, $file) = @_;
98              
99 2         14 my $orig_content = $file->content;
100 2 50       186 $self->log_fatal([ 'could not find position in %s to modify!', $file->name ])
101             if not $orig_content =~ m/use strict;\nuse warnings;\n\n/g;
102              
103 2         8 my $pos = pos($orig_content);
104              
105             # build a list of tuples: field name => string
106             my @options = (
107             (map {
108 8         325 my @stuff = map { '\'' . $_ . '\'' } $self->$_;
  14         53  
109             @stuff
110 8 100       59 ? [ $_ => @stuff > 1 ? ('[ ' . join(', ', @stuff) . ' ]') : $stuff[0] ]
    50          
111             : ()
112             } @list_options),
113             (map {
114 2 100       10 defined $self->$_
  6         173  
115             ? [ $_ => '\'' . $self->$_ . '\'' ]
116             : ()
117             } @string_options),
118             );
119              
120             $file->content(
121             substr($orig_content, 0, $pos)
122             . "# inserted by " . blessed($self) . ' ' . $self->VERSION . "\n"
123             . "use Devel::CheckLib;\n"
124             . "check_lib_or_exit(\n"
125             . join('',
126 2         49 map { ' ' . $_->[0] . ' => ' . $_->[1] . ",\n" } @options
  12         68  
127             )
128             . ");\n\n"
129             . substr($orig_content, $pos)
130             );
131             }
132              
133             __PACKAGE__->meta->make_immutable;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             Dist::Zilla::Plugin::CheckLib - Require that your distribution has a particular library available
144              
145             =head1 VERSION
146              
147             version 0.007
148              
149             =head1 SYNOPSIS
150              
151             In your F<dist.ini>:
152              
153             [CheckLib]
154             lib = jpeg
155             header = jpeglib.h
156              
157             =head1 DESCRIPTION
158              
159             This is a L<Dist::Zilla> plugin that modifies the F<Makefile.PL> or
160             F<Build.PL> in your distribution to contain a L<Devel::CheckLib> call, that
161             asserts that a particular library and/or header is available. If it is not
162             available, the program exits with a status of zero, which on a
163             L<CPAN Testers|cpantesters.org> machine will result in a NA result.
164              
165             =for Pod::Coverage mvp_multivalue_args register_prereqs munge_files setup_installer
166              
167             =head1 CONFIGURATION OPTIONS
168              
169             All options are as documented in L<Devel::CheckLib>:
170              
171             =head2 C<lib>
172              
173             A string with the name of a single library name. Can be used more than once.
174             Depending on the compiler found, library names will be fed to the compiler
175             either as C<-l> arguments or as C<.lib> file names. (e.g. C<-ljpeg> or
176             C<jpeg.lib>)
177              
178             =head2 C<libpath>
179              
180             Additional path to search for libraries. Can be used more than once.
181              
182             =head2 C<LIBS>
183              
184             A L<ExtUtils::MakeMaker>-style space-separated list of libraries (each preceded by C<-l>) and directories (preceded by C<-L>).
185              
186             =head2 C<debug>
187              
188             If true, emit information during processing that can be used for debugging.
189             B<Note>: as this is an arbitrary string that is inserted directly into
190             F<Makefile.PL> or F<Build.PL>, this can be any arbitrary expression,
191             for example:
192              
193             [CheckLib]
194             debug = $ENV{AUTOMATED_TESTING} || $^O eq 'MSWin32'
195              
196             =head2 C<header>
197              
198             The name of a single header file name. Can be used more than once.
199              
200             =head2 C<incpath>
201              
202             An additional path to search for headers. Can be used more than once.
203              
204             =head2 C<INC>
205              
206             =for stopwords incpaths
207              
208             A L<ExtUtils::MakeMaker>-style space-separated list of incpaths, each preceded by C<-I>.
209              
210             =head1 SEE ALSO
211              
212             =over 4
213              
214             =item *
215              
216             L<Devel::CheckLib>
217              
218             =item *
219              
220             L<Devel::AssertOS> and L<Dist::Zilla::Plugin::AssertOS>
221              
222             =item *
223              
224             L<Devel::CheckBin> and L<Dist::Zilla::Plugin::CheckBin>
225              
226             =back
227              
228             =head1 SUPPORT
229              
230             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-CheckLib>
231             (or L<bug-Dist-Zilla-Plugin-CheckLib@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-CheckLib@rt.cpan.org>).
232              
233             There is also a mailing list available for users of this distribution, at
234             L<http://dzil.org/#mailing-list>.
235              
236             There is also an irc channel available for users of this distribution, at
237             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
238              
239             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
240              
241             =head1 AUTHOR
242              
243             Karen Etheridge <ether@cpan.org>
244              
245             =head1 COPYRIGHT AND LICENCE
246              
247             This software is copyright (c) 2014 by Karen Etheridge.
248              
249             This is free software; you can redistribute it and/or modify it under
250             the same terms as the Perl 5 programming language system itself.
251              
252             =cut