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

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 118 CC 5 SCAR 24.8
CC
5
Cov
0%
add_methods
30.0
copy_header
2.0
BEGIN
2.0
CRAP 12.0
1 package Dist::Build::XS::Export;
2 $Dist::Build::XS::Export::VERSION = '0.028';
3 use strict;
4 use warnings;
5  
6 use parent 'ExtUtils::Builder::Planner::Extension';
7  
8 use Carp 'croak';
9 use ExtUtils::Builder::Util qw/unix_to_native_path/;
10 use File::Find 'find';
11 use File::Spec::Functions qw/abs2rel catfile/;
12 use Parse::CPAN::Meta;
13  
14 my $json_backend = Parse::CPAN::Meta->json_backend;
15 my $json = $json_backend->new->canonical->pretty->utf8;
16  
17 my @allowed_flags = qw/include_dirs defines library_dirs libraries extra_compiler_flags extra_linker_flags/;
18 my %allowed_flag = map { $_ => 1 } @allowed_flags;
19  
20 sub copy_header {
21 my ($planner, $module_dir, $filename, $target) = @_;
22  
23 my $output = catfile(qw/blib lib auto share module/, $module_dir, 'include', $target);
24 return $planner->copy_file(abs2rel($filename), $output);
25 }
26  
27 sub add_methods {
28 my ($self, $planner) = @_;
29  
30 $planner->add_delegate('export_headers', sub {
31 my ($self, %args) = @_;
32 my $module_name = $args{module} // $planner->main_module;
33 (my $module_dir = $module_name) =~ s/::/-/g;
34 croak 'No directory or file given to share' if not $args{dir} and not $args{file};
35  
36 my $inner = $planner->new_scope;
37 $inner->load_extension('Dist::Build::Core');
38  
39 $inner->create_subst(
40 on => $inner->create_pattern(dir => $args{dir}),
41 add_to => 'code',
42 subst => sub {
43 my ($source) = @_;
44 my $target = abs2rel($source, $args{dir});
45 return copy_header($inner, $module_dir, $source, $target);
46 },
47 ) if $args{dir};
48  
49 my @files = ref $args{file} ? @{ $args{file} } : defined $args{file} ? $args{file} : ();
50 for my $file (@files) {
51 $planner->create_phony('code', copy_header($inner, $module_dir, $file, $file));
52 }
53 });
54  
55 $planner->add_delegate('export_typemap', sub {
56 my ($self, %args) = @_;
57 my $module_name = $args{module} // $planner->main_module;
58 (my $module_dir = $module_name) =~ s/::/-/g;
59 my $filename = $args{filename} // 'typemap';
60 my $destination = catfile(qw/blib lib auto share module/, $module_dir, 'typemap');
61  
62 my $inner = $planner->new_scope;
63 $inner->load_extension('Dist::Build::Core');
64 $inner->copy_file($filename, $destination);
65  
66 $planner->create_phony('code', $destination);
67 });
68  
69 $planner->add_delegate('export_flags', sub {
70 my ($self, %args) = @_;
71 my %flags = map { $_ => $args{$_} } grep { $allowed_flag{$_} } keys %args;
72  
73 my $module_name = $args{module} // $planner->main_module;
74 (my $module_dir = $module_name) =~ s/::/-/g;
75 my $filename = catfile(qw/blib lib auto share module/, $module_dir, 'compile.json');
76  
77 my $inner = $planner->new_scope;
78 $inner->load_extension('Dist::Build::Core');
79 $inner->dump_json($filename, \%flags);
80  
81 return $planner->create_phony('code', $filename);
82 });
83 }
84  
85 1;
86  
87 # ABSTRACT: Dist::Build extension to export headers for other XS modules
88  
89 __END__