blib/lib/Dist/Build/XS/Import.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 101 CC 7 SCAR 33.7
CC
7
Cov
0%
add_methods
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 29.0
1 package Dist::Build::XS::Import;
2 $Dist::Build::XS::Import::VERSION = '0.028';
3 use strict;
4 use warnings;
5  
6 use parent 'ExtUtils::Builder::Planner::Extension';
7  
8 use Carp 'croak';
9 use File::Spec::Functions qw/catfile catdir/;
10 use Dist::Build::Util 'module_dir';
11 use Parse::CPAN::Meta;
12  
13 my $json_backend = Parse::CPAN::Meta->json_backend;
14 my $json = $json_backend->new->canonical->pretty->utf8;
15  
16 sub add_methods {
17 my ($self, $planner, %args) = @_;
18  
19 my $add_xs = $planner->can('add_xs') or croak 'XS must be loaded before imports can be done';
20  
21 $planner->add_delegate('add_xs', sub {
22 my ($planner, %args) = @_;
23  
24 if (my $import = delete $args{import}) {
25 my @modules = ref $import ? @{ $import } : $import;
26 for my $module (@modules) {
27 my $module_dir = module_dir($module);
28 my $found = 0;
29  
30 my $include = catdir($module_dir, 'include');
31 if (-d $include) {
32 unshift @{ $args{include_dirs} }, $include;
33 $found++;
34 }
35  
36 my $typemap = catfile($module_dir, 'typemap');
37 if (-e $typemap) {
38 unshift @{ $args{typemap} }, $typemap;
39 $found++;
40 }
41  
42 my $config = catfile($module_dir, 'compile.json');
43 if (-e $config) {
44 open my $fh, '<:raw', $config or die "Could not open $config: $!";
45 my $content = do { local $/; <$fh> };
46 my $payload = $json->decode($content);
47  
48 for my $key (qw/include_dirs library_dirs libraries extra_compiler_flags extra_linker_flags/) {
49 unshift @{ $args{$key} }, @{ $payload->{$key} || [] };
50 }
51  
52 for my $key (keys %{ $payload->{defines} // {} }) {
53 $args{defines}{$key} //= $payload->{defines}{$key};
54 }
55 $found++;
56 }
57  
58 warn "No such import $module\n" if $found == 0;
59 }
60 }
61  
62 $planner->$add_xs(%args);
63 });
64 }
65  
66 1;
67  
68 # ABSTRACT: Dist::Build extension to import headers for other XS modules
69  
70 __END__