File Coverage

blib/lib/Dist/Zilla/Plugin/FFI/Mint.pm
Criterion Covered Total %
statement 70 70 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 0 3 0.0
total 82 86 95.3


line stmt bran cond sub pod time code
1 2     2   3473399 use strict;
  2         26  
  2         58  
2 2     2   10 use warnings;
  2         5  
  2         48  
3 2     2   43 use 5.020;
  2         6  
4              
5             package Dist::Zilla::Plugin::FFI::Mint 0.02 {
6              
7 2     2   478 use Moose;
  2         394077  
  2         22  
8             with 'Dist::Zilla::Role::FileMunger', 'Dist::Zilla::Role::FileGatherer', 'Dist::Zilla::Role::ModuleMaker';
9 2     2   13138 use experimental qw( signatures postderef );
  2         3158  
  2         15  
10 2     2   1389 use Dist::Zilla::File::InMemory;
  2         530581  
  2         87  
11 2     2   20 use namespace::autoclean;
  2         5  
  2         14  
12              
13             # ABSTRACT: Generate module and modify dist.ini for use with FFI
14              
15             has lib_name => (
16             is => 'ro',
17             isa => 'Str',
18             lazy => 1,
19             default => sub ($self) {
20             $self->zilla->chrome->prompt_str("Library name (for libfoo.so or foo.dll enter 'foo')");
21             },
22             );
23              
24             has alien_name => (
25             is => 'ro',
26             isa => 'Str',
27             lazy => 1,
28             default => sub ($self) {
29             $self->zilla->chrome->prompt_str("Fallback Alien name");
30             },
31             );
32              
33             has module_package => (
34             is => 'rw',
35             isa => 'Str',
36             init_arg => undef,
37             );
38              
39             has module_filename => (
40             is => 'rw',
41             isa => 'Str',
42             init_arg => undef,
43             );
44              
45             has lib_package => (
46             is => 'ro',
47             isa => 'Str',
48             init_arg => undef,
49             lazy => 1,
50             default => sub ($self) {
51             $self->module_package . "::Lib";
52             },
53             );
54              
55             has lib_filename => (
56             is => 'ro',
57             isa => 'Str',
58             init_arg => undef,
59             lazy => 1,
60             default => sub ($self) {
61             $self->module_filename =~ s/\.pm$/\/Lib.pm/r;
62             },
63             );
64              
65             has test_filename => (
66             is => 'ro',
67             isa => 'Str',
68             init_arg => undef,
69             lazy => 1,
70             default => sub ($self) {
71             't/' . lc($self->module_package =~ s/::/_/gr) . ".t";
72             },
73             );
74              
75             sub munge_files ($self)
76 1     1 0 7833 {
  1         2  
  1         2  
77 1         2 my($dist_ini) = grep { $_->name eq 'dist.ini' } @{ $self->zilla->files };
  5         183  
  1         27  
78 1 50       39 $self->log_fatal("unable to find dist.ini") unless defined $dist_ini;
79              
80 1         2 $self->log("Munge: @{[ $dist_ini->name ]}");
  1         5  
81              
82 1         349 my $code = $dist_ini->code;
83              
84             # this is hilariously stupid but this is what we are doing.
85             $dist_ini->code(sub {
86 1     1   7453 my $content = $code->();
87 1         2808 $content =~ s/##LIB_FILENAME##/$self->lib_filename/eg;
  1         37  
88 1         8 $content =~ s/##LIB_MODULENAME##/$self->lib_package/eg;
  1         29  
89 1         8 $content =~ s/##ALIEN_NAME##/$self->alien_name/eg;
  1         28  
90 1         4 $content;
91 1         82 });
92             }
93              
94             sub gather_files ($self)
95 1     1 0 551 {
  1         3  
  1         2  
96 1         2 $self->log("Generate: @{[ $self->lib_filename ]}");
  1         35  
97 1         296 $self->add_file(
98             Dist::Zilla::File::InMemory->new({
99             name => $self->lib_filename,
100             content => <<"END1",
101 1         30 package @{[ $self->lib_package ]};
102              
103             use strict;
104             use warnings;
105             use FFI::CheckLib 0.28 qw( find_lib );
106              
107             sub lib {
108 1         44 find_lib lib => '@{[ $self->lib_name ]}', alien => '@{[ $self->alien_name ]}';
  1         36  
109             }
110              
111             1;
112              
113             =head1 NAME
114              
115 1         28 @{[ $self->lib_package ]} - Private class for @{[ $self->module_package ]}
  1         29  
116              
117             =head1 SYNOPSIS
118              
119 1         26 perldoc @{[ $self->module_package ]}
120              
121             =head1 DESCRIPTION
122              
123             This class is private.
124              
125             =head1 SEE ALSO
126              
127             =over 4
128              
129 1         27 =item @{[ $self->module_package ]}
130              
131             =back
132              
133             =cut
134             END1
135             })
136             );
137              
138 1         562 $self->log("Generate: @{[ $self->test_filename ]}");
  1         29  
139 1         266 $self->add_file(
140             Dist::Zilla::File::InMemory->new({
141             name => $self->test_filename,
142             content => <<"END2",
143             use Test2::V0;
144 1         28 use @{[ $self->module_package ]};
145              
146             ok 1;
147              
148             done_testing;
149             END2
150             }),
151             );
152             }
153              
154 1         3 sub make_module ($self, $arg)
155 1     1 0 202550 {
  1         2  
  1         1  
156 1         31 $self->module_package($arg->{name});
157 1         35 $self->module_filename("lib/" . ($arg->{name} =~ s/::/\//gr) . ".pm");
158 1         3 $self->log("Creating main module: @{[ $self->module_filename ]}");
  1         29  
159              
160 1         265 my $file = Dist::Zilla::File::InMemory->new({
161             name => $self->module_filename,
162             content => <<"END3",
163 1         27 package @{[ $self->module_package ]};
164              
165             use strict;
166             use warnings;
167             use FFI::Platypus 1.00;
168 1         26 use @{[ $self->module_package ]}::Lib;
169              
170             my \$ffi = FFI::Platypus->new(
171             api => 1,
172 1         29 lib => [@{[ $self->module_package ]}::Lib->lib],
173             );
174              
175             #\$ffi->attach( ... );
176              
177             1;
178              
179             =head1 NAME
180              
181 1         27 @{[ $self->module_package ]} - Bindings for ...
182              
183             =head1 SYNOPSIS
184              
185             ...
186              
187             =head1 DESCRIPTION
188              
189             ...
190              
191             =cut
192             END3
193             });
194 1         348 $self->add_file($file);
195             }
196              
197             __PACKAGE__->meta->make_immutable;
198             }
199              
200             1;
201              
202             __END__
203              
204             =pod
205              
206             =encoding UTF-8
207              
208             =head1 NAME
209              
210             Dist::Zilla::Plugin::FFI::Mint - Generate module and modify dist.ini for use with FFI
211              
212             =head1 VERSION
213              
214             version 0.02
215              
216             =head1 SYNOPSIS
217              
218             dzil new -P FFI Foo::FFI
219              
220             =head1 DESCRIPTION
221              
222             This plugin will prompt you for a library which will be used by your
223             FFI module. Its intended use if by the FFI minting profile, but may
224             be useful in your own FFI related profiles.
225              
226             =head1 SEE ALSO
227              
228             =over 4
229              
230             =item L<FFI::Platypus>
231              
232             =item L<Dist::Zilla::MintingProfile::FFI>
233              
234             =item L<Dist::Zilla>
235              
236             =back
237              
238             =head1 AUTHOR
239              
240             Graham Ollis <plicease@cpan.org>
241              
242             =head1 COPYRIGHT AND LICENSE
243              
244             This software is copyright (c) 2021 by Graham Ollis.
245              
246             This is free software; you can redistribute it and/or modify it under
247             the same terms as the Perl 5 programming language system itself.
248              
249             =cut