File Coverage

blib/lib/Dist/Zilla/Plugin/FFI/Build.pm
Criterion Covered Total %
statement 35 41 85.3
branch 6 14 42.8
condition 2 6 33.3
subroutine 8 8 100.0
pod 0 4 0.0
total 51 73 69.8


line stmt bran cond sub pod time code
1              
2             use 5.020;
3 2     2   314932 use Moose;
  2         14  
4 2     2   1028 use List::Util qw( first );
  2         907401  
  2         15  
5 2     2   15209  
  2         5  
  2         1690  
6             # ABSTRACT: Add FFI::Build to your Makefile.PL
7              
8              
9             # TODO: also add build and test prereqs for aliens
10             # TODO: release as separate CPAN dist
11             with 'Dist::Zilla::Role::FileMunger',
12             'Dist::Zilla::Role::MetaProvider',
13             'Dist::Zilla::Role::PrereqSource',
14             'Dist::Zilla::Role::FilePruner',
15             ;
16              
17             my $mm_code_prereqs = <<'EOF1';
18             use FFI::Build::MM 0.83;
19             my $fbmm = FFI::Build::MM->new;
20             %WriteMakefileArgs = $fbmm->mm_args(%WriteMakefileArgs);
21             EOF1
22              
23             my $mm_code_postamble = <<'EOF2';
24             BEGIN {
25             # append to any existing postamble.
26             if(my $old = MY->can('postamble'))
27             {
28             no warnings 'redefine';
29             *MY::postamble = sub {
30             $old->(@_) .
31             "\n" .
32             $fbmm->mm_postamble;
33             };
34             }
35             else
36             {
37             *MY::postamble = sub {
38             $fbmm->mm_postamble;
39             };
40             }
41             }
42             EOF2
43              
44             my $comment_begin = "# BEGIN code inserted by @{[ __PACKAGE__ ]}\n";
45             my $comment_end = "# END code inserted by @{[ __PACKAGE__ ]}\n";
46              
47             has lang => (
48             is => 'ro',
49             isa => 'Maybe[Str]',
50             );
51              
52             has build => (
53             is => 'ro',
54             isa => 'Maybe[Str]',
55             );
56              
57             {
58             my($self) = @_;
59             my $file = first { $_->name eq 'Makefile.PL' } @{ $self->zilla->files };
60 1     1 0 1390 $self->log_fatal("unable to find Makefile.PL")
61 1     3   7 unless $file;
  3         128  
  1         32  
62 1 50       48 my $content = $file->content;
63             my $ok = $content =~ s/(unless \( eval \{ ExtUtils::MakeMaker)/"$comment_begin$mm_code_prereqs$comment_end\n\n$1"/e;
64 1         8 $self->log_fatal('unable to find the correct location to insert prereqs')
65 1         93 unless $ok;
  1         8  
66 1 50       5 $content .= "\n\n$comment_begin$mm_code_postamble$comment_end\n";
67             $file->content($content);
68 1         6 }
69 1         4  
70             my ($self) = @_;
71             $self->zilla->register_prereqs( +{
72             phase => 'configure',
73 1     1 0 2409 type => 'requires',
74 1         28 },
75             'FFI::Build::MM' => '0.83',
76             );
77              
78             if($self->lang)
79             {
80             $self->zilla->register_prereqs( +{
81 1 50       353 phase => 'runtime',
82             type => 'requires',
83 0         0 },
84             "FFI::Platypus::Lang::@{[ $self->lang ]}",
85             );
86             }
87 0         0  
88             if($self->build)
89             {
90             $self->zilla->register_prereqs( +{
91 1 50       35 phase => 'configure',
92             type => 'requires',
93 0         0 },
94             "FFI::Build::File::@{[ $self->build ]}",
95             );
96             }
97 0         0  
98             }
99              
100             {
101             my($self) = @_;
102             my %meta = ( dynamic_config => 1 );
103             \%meta;
104             }
105 1     1 0 5414  
106 1         4 {
107 1         6 my($self) = @_;
108              
109             my $lang = $self->lang;
110              
111             foreach my $file (@{ $self->zilla->files })
112 1     1 0 163377 {
113             my $name = $file->name;
114 1         39 my $prune = 0;
115             $prune = 1 if $name =~ m!^ffi/_build/!;
116 1         3 $prune = 1 if defined $lang && $lang eq 'Rust' && $name =~ m!^(t/|)ffi/target/!;
  1         30  
117             if($prune)
118 4         86 {
119 4         221 $self->log_debug([ 'pruning %s', $name ]);
120 4 50       13 $self->zilla->prune_file($file);
121 4 0 33     13 }
      33        
122 4 50       17 }
123              
124 0           }
125 0            
126             __PACKAGE__->meta->make_immutable;
127             }
128              
129             1;
130              
131              
132             =pod
133              
134             =encoding UTF-8
135              
136             =head1 NAME
137              
138             Dist::Zilla::Plugin::FFI::Build - Add FFI::Build to your Makefile.PL
139              
140             =head1 VERSION
141              
142             version 1.06
143              
144             =head1 SYNOPSIS
145              
146             [FFI::Build]
147              
148             =head1 DESCRIPTION
149              
150             This plugin makes the appropriate modifications to your dist to allow
151             you to bundle code with L<FFI::Platypus::Bundle>. It works with
152             L<FFI::Build::MM>, and only works with L<ExtUtils::MakeMaker>, so
153             don't try to use it with L<Module::Build>.
154              
155             It specifically:
156              
157             =over
158              
159             =item Updates C<Makefile.PL>
160              
161             To call L<FFI::Build::MM> to add the necessary hooks to build and install
162             your bundled code.
163              
164             =item Sets configure-time prereqs
165              
166             For L<FFI::Build::MM>. It also makes the prereqs for your distribution
167             dynamic, which is required for L<FFI::Build::MM>.
168              
169             =item Prunes any build files
170              
171             Removes any files in C<ffi/_build> which may be created when developing
172             an FFI module using the bundle interface.
173              
174             =back
175              
176             This plugin adds the appropriate hooks for L<FFI::Build::MM> into your
177             C<Makefile.PL>. It does not work with L<Module::Build>.
178              
179             =head1 PROPERTIES
180              
181             =head2 lang
182              
183             If you are using a L<language plugin|FFI::Platypus::Lang> then you can
184             specify it here. It will add it as a prereq. This should be the "short"
185             name of the plugin, without the C<FFI::Platypus::Lang> prefix. So for
186             example for L<FFI::Platypus::Lang::Rust> you would just set this to
187             C<Rust>.
188              
189             In addition setting these C<lang> to these languages will have the following
190             additional affects:
191              
192             =over 4
193              
194             =item C<Rust>
195              
196             The paths C<ffi/target> and C<t/ffi/target> will be pruned when building
197             the dist. This is usually what you want.
198              
199             =back
200              
201             =head2 build
202              
203             If you need a language specific builder this is where you specify it.
204             These are classes that live in the C<FFI::Build::File::> namespace.
205             For example for Rust you would specify L<Cargo|FFI::Build::File::Cargo>
206             and for Go you would specify L<GoMod|FFI::Build::File::GoMod>.
207              
208             Setting this property will add the appropriate module as a configure
209             time prereq.
210              
211             You do not usually need this for the C programming language.
212              
213             =head1 AUTHOR
214              
215             Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
216              
217             Contributors:
218              
219             Zaki Mughal (zmughal)
220              
221             =head1 COPYRIGHT AND LICENSE
222              
223             This software is copyright (c) 2018-2022 by Graham Ollis.
224              
225             This is free software; you can redistribute it and/or modify it under
226             the same terms as the Perl 5 programming language system itself.
227              
228             =cut