File Coverage

blib/lib/Dist/Zilla/Plugin/FFI/Build.pm
Criterion Covered Total %
statement 36 42 85.7
branch 6 14 42.8
condition 2 6 33.3
subroutine 8 8 100.0
pod 0 4 0.0
total 52 74 70.2


line stmt bran cond sub pod time code
1              
2             use 5.020;
3 2     2   315980 use Moose;
  2         16  
4 2     2   1162 use List::Util qw( first );
  2         918804  
  2         13  
5 2     2   15116  
  2         6  
  2         1713  
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 1511 $self->log_fatal("unable to find Makefile.PL")
61 1     3   7 unless $file;
  3         135  
  1         32  
62 1 50       55 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         9 $self->log_fatal('unable to find the correct location to insert prereqs')
65 1         94 unless $ok;
  1         12  
66 1 50       5 $content .= "\n\n$comment_begin$mm_code_postamble$comment_end\n";
67             $file->content($content);
68 1         9 }
69 1         6  
70             my ($self) = @_;
71             $self->zilla->register_prereqs( +{
72             phase => 'configure',
73 1     1 0 2678 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       376 phase => 'runtime',
82             type => 'requires',
83 0         0 },
84             "FFI::Platypus::Lang::@{[ $self->lang ]}" => 0,
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 ]}" => 0,,
95             );
96             }
97 0         0  
98             }
99              
100             {
101             my($self) = @_;
102             my %meta = ( dynamic_config => 1 );
103             \%meta;
104             }
105 1     1 0 5142  
106 1         6 {
107 1         6 my($self) = @_;
108              
109             my $lang = $self->lang;
110              
111             foreach my $file ((), @{ $self->zilla->files })
112 1     1 0 166995 {
113             my $name = $file->name;
114 1         43 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         244 {
119 4         216 $self->log([ 'pruning %s', $name ]);
120 4 50       18 $self->zilla->prune_file($file);
121 4 0 33     15 }
      33        
122 4 50       11 else
123             {
124 0         0 $self->log_debug([ 'NOT pruning %s', $name ]);
125 0         0 }
126             }
127              
128             }
129 4         18  
130             __PACKAGE__->meta->make_immutable;
131             }
132              
133             1;
134              
135              
136             =pod
137              
138             =encoding UTF-8
139              
140             =head1 NAME
141              
142             Dist::Zilla::Plugin::FFI::Build - Add FFI::Build to your Makefile.PL
143              
144             =head1 VERSION
145              
146             version 1.07
147              
148             =head1 SYNOPSIS
149              
150             [FFI::Build]
151              
152             =head1 DESCRIPTION
153              
154             This plugin makes the appropriate modifications to your dist to allow
155             you to bundle code with L<FFI::Platypus::Bundle>. It works with
156             L<FFI::Build::MM>, and only works with L<ExtUtils::MakeMaker>, so
157             don't try to use it with L<Module::Build>.
158              
159             It specifically:
160              
161             =over
162              
163             =item Updates C<Makefile.PL>
164              
165             To call L<FFI::Build::MM> to add the necessary hooks to build and install
166             your bundled code.
167              
168             =item Sets configure-time prereqs
169              
170             For L<FFI::Build::MM>. It also makes the prereqs for your distribution
171             dynamic, which is required for L<FFI::Build::MM>.
172              
173             =item Prunes any build files
174              
175             Removes any files in C<ffi/_build> which may be created when developing
176             an FFI module using the bundle interface.
177              
178             =back
179              
180             This plugin adds the appropriate hooks for L<FFI::Build::MM> into your
181             C<Makefile.PL>. It does not work with L<Module::Build>.
182              
183             =head1 PROPERTIES
184              
185             =head2 lang
186              
187             If you are using a L<language plugin|FFI::Platypus::Lang> then you can
188             specify it here. It will add it as a prereq. This should be the "short"
189             name of the plugin, without the C<FFI::Platypus::Lang> prefix. So for
190             example for L<FFI::Platypus::Lang::Rust> you would just set this to
191             C<Rust>.
192              
193             In addition setting these C<lang> to these languages will have the following
194             additional affects:
195              
196             =over 4
197              
198             =item C<Rust>
199              
200             The paths C<ffi/target> and C<t/ffi/target> will be pruned when building
201             the dist. This is usually what you want.
202              
203             =back
204              
205             =head2 build
206              
207             If you need a language specific builder this is where you specify it.
208             These are classes that live in the C<FFI::Build::File::> namespace.
209             For example for Rust you would specify L<Cargo|FFI::Build::File::Cargo>
210             and for Go you would specify L<GoMod|FFI::Build::File::GoMod>.
211              
212             Setting this property will add the appropriate module as a configure
213             time prereq.
214              
215             You do not usually need this for the C programming language.
216              
217             =head1 AUTHOR
218              
219             Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
220              
221             Contributors:
222              
223             Zaki Mughal (zmughal)
224              
225             =head1 COPYRIGHT AND LICENSE
226              
227             This software is copyright (c) 2018-2022 by Graham Ollis.
228              
229             This is free software; you can redistribute it and/or modify it under
230             the same terms as the Perl 5 programming language system itself.
231              
232             =cut