File Coverage

blib/lib/Dist/Zilla/Plugin/FFI/CheckLib.pm
Criterion Covered Total %
statement 42 45 93.3
branch 11 16 68.7
condition 1 2 50.0
subroutine 9 9 100.0
pod 0 4 0.0
total 63 76 82.8


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::FFI::CheckLib;
2             $Dist::Zilla::Plugin::FFI::CheckLib::VERSION = '0.002001';
3 2     2   2735480 use strict; use warnings;
  2     2   2  
  2         57  
  2         6  
  2         3  
  2         43  
4              
5 2     2   6 use Moose;
  2         3  
  2         12  
6             with
7             'Dist::Zilla::Role::FileMunger',
8             'Dist::Zilla::Role::InstallTool',
9             'Dist::Zilla::Role::PrereqSource',
10             ;
11              
12 2     2   8030 use namespace::autoclean;
  2         4  
  2         14  
13              
14             my @list_options = qw/
15             lib
16             libpath
17             symbol
18             systempath
19             /;
20 2     2 0 852 sub mvp_multivalue_args { @list_options }
21              
22             has $_ => (
23             isa => 'ArrayRef[Str]',
24             lazy => 1,
25             default => sub { [] },
26             traits => ['Array'],
27             handles => { $_ => 'elements' },
28             ) for @list_options;
29              
30             has recursive => (
31             is => 'rw',
32             lazy => 1,
33             default => sub { 0 },
34             );
35              
36             around dump_config => sub {
37             my ($orig, $self) = @_;
38             my $config = $self->$orig;
39              
40             $config->{+__PACKAGE__} = +{
41             (map {; $_ => [ $self->$_ ] } @list_options),
42             recursive => $self->recursive,
43             };
44              
45             $config
46             };
47              
48             sub register_prereqs {
49 2     2 0 2063 my ($self) = @_;
50 2         44 $self->zilla->register_prereqs( +{
51             phase => 'configure',
52             type => 'requires',
53             },
54             'FFI::CheckLib' => '0.11',
55             );
56             }
57              
58             my %files;
59             sub munge_files {
60 2     2 0 83819 my ($self) = @_;
61              
62             my @mfpl = grep
63 6 100       262 {; $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' }
  2         46  
64 2         3 @{ $self->zilla->files };
65              
66 2         72 for my $mfpl (@mfpl) {
67 2         5 $self->log_debug('munging ' . $mfpl->name . ' in file gatherer phase');
68 2         439 $files{ $mfpl->name } = $mfpl;
69 2         68 $self->_munge_file($mfpl);
70             }
71              
72             ()
73 2         374 }
74              
75             sub setup_installer {
76 2     2 0 54129 my ($self) = @_;
77              
78             my @mfpl = grep
79 6 100       246 {; $_->name eq 'Makefile.PL' or $_->name eq 'Build.PL' }
  2         43  
80 2         5 @{ $self->zilla->files };
81              
82 2 50       76 unless (@mfpl) {
83 0         0 $self->log_fatal(
84             'No Makefile.PL or Build.PL was found.'
85             .' [FFI::CheckLib] should appear in dist.ini'
86             .' after [MakeMaker] or variant!'
87             );
88             }
89              
90 2         4 for my $mfpl (@mfpl) {
91 2 50       5 next if exists $files{ $mfpl->name };
92 0         0 $self->log_debug('munging ' . $mfpl->name . ' in setup_installer phase');
93 0         0 $self->_munge_file($mfpl);
94             }
95              
96             ()
97 2         81 }
98              
99             sub _munge_file {
100 2     2   6 my ($self, $file) = @_;
101              
102 2         8 my $orig_content = $file->content;
103 2 50       122 $self->log_fatal('could not find position in ' . $file->name . ' to modify!')
104             unless $orig_content =~ m/use strict;\nuse warnings;\n\n/g;
105 2         4 my $pos = pos($orig_content);
106              
107 2 50       53 my @options = map {;
108 10         336 my @stuff = map {; '\'' . $_ . '\'' } $self->$_;
  14         32  
109 10 100       32 @stuff ?
    50          
110             [
111             $_ => @stuff > 1 ? ('[ ' . join(', ', @stuff) . ' ]') : $stuff[0]
112             ] : ()
113             } @list_options, ( $self->recursive ? 'recursive' : () );
114              
115             $file->content(
116             substr($orig_content, 0, $pos)
117             . "# inserted by " . blessed($self)
118             . ' ' . ($self->VERSION || '<self>') . "\n"
119             . "use FFI::CheckLib;\n"
120             . "check_lib_or_exit(\n"
121 2   50     30 . join('', map {; ' 'x4 . $_->[0] . ' => ' . $_->[1] . ",\n" } @options )
  10         34  
122             . ");\n\n"
123             . substr($orig_content, $pos)
124             );
125             }
126              
127             __PACKAGE__->meta->make_immutable;
128              
129             __END__
130              
131             =pod
132              
133             =head1 NAME
134              
135             Dist::Zilla::Plugin::FFI::CheckLib - FFI::CheckLib alternative to Dist::Zilla::Plugin::CheckLib
136              
137             =head1 SYNOPSIS
138              
139             In your F<dist.ini>:
140              
141             [FFI::CheckLib]
142             lib = zmq
143              
144             =head1 DESCRIPTION
145              
146             This is a L<Dist::Zilla> plugin that modifies the F<Makefile.PL> or
147             F<Build.PL> in your distribution to check for a dynamic library L<FFI::Raw> (or
148             similar) can access; uses L<FFI::CheckLib> to perform the check.
149              
150             If the library is not available, the program exits with a status of zero,
151             which will result in a NA result on a CPAN test reporter.
152              
153             Derived from L<Dist::Zilla::Plugin::CheckLib> (see L</AUTHOR>) -- look there
154             for non-FFI applications.
155              
156             =for Pod::Coverage mvp_multivalue_args register_prereqs munge_files setup_installer
157              
158             =head1 CONFIGURATION OPTIONS
159              
160             All options are as documented in L<FFI::CheckLib>:
161              
162             =head2 C<lib>
163              
164             The name of a single dynamic library (for example, C<zmq>).
165             Can be used more than once.
166              
167             L<FFI::CheckLib> will prepend C<lib> and append an appropriate dynamic library
168             suffix as needed.
169              
170             =head2 C<symbol>
171              
172             A symbol that must be found. Can be used more than once.
173              
174             =head2 C<systempath>
175              
176             The system search path to use (instead of letting L<FFI::CheckLib> determine
177             paths). Can be used more than once.
178              
179             =head2 C<libpath>
180              
181             Additional path to search for libraries. Can be used more than once.
182              
183             =head2 C<recursive>
184              
185             If set to true, directories specified in C<libpath> will be searched
186             recursively.
187              
188             Defaults to false.
189              
190             =head1 SEE ALSO
191              
192             =over 4
193              
194             =item *
195              
196             L<FFI::CheckLib>
197              
198             =item *
199              
200             L<Devel::CheckLib> and L<Dist::Zilla::Plugin::CheckLib>
201              
202             =item *
203              
204             L<Devel::AssertOS> and L<Dist::Zilla::Plugin::AssertOS>
205              
206             =back
207              
208             =head1 AUTHOR
209              
210             Ported to L<FFI::CheckLib> by Jon Portnoy <avenj@cobaltirc.org>
211              
212             This module is adapted directly from L<Dist::Zilla::Plugin::CheckLib>,
213             copyright (c) 2014 by Karen Etheridge (CPAN: ETHER).
214              
215             This is free software; you can redistribute it and/or modify it under
216             the same terms as the Perl 5 programming language system itself.
217              
218             =cut