File Coverage

blib/lib/Inline/Module/LeanDist/DistDir.pm
Criterion Covered Total %
statement 9 47 19.1
branch 0 30 0.0
condition n/a
subroutine 3 11 27.2
pod 0 3 0.0
total 12 91 13.1


line stmt bran cond sub pod time code
1             package Inline::Module::LeanDist::DistDir;
2              
3 1     1   377 use strict;
  1         1  
  1         31  
4              
5 1     1   4 use File::Find;
  1         1  
  1         70  
6 1     1   519 use File::Copy;
  1         3735  
  1         685  
7              
8             require Inline::Module::LeanDist;
9              
10              
11             ## Inline::C makes it kind of hard to get access to this, so just copy/paste it in here for now (I know, I know, ick...)
12              
13             our $inline_h_file = <<'END_OF_INLINE_H';
14             #define Inline_Stack_Vars dXSARGS
15             #define Inline_Stack_Items items
16             #define Inline_Stack_Item(x) ST(x)
17             #define Inline_Stack_Reset sp = mark
18             #define Inline_Stack_Push(x) XPUSHs(x)
19             #define Inline_Stack_Done PUTBACK
20             #define Inline_Stack_Return(x) XSRETURN(x)
21             #define Inline_Stack_Void XSRETURN(0)
22              
23             #define INLINE_STACK_VARS Inline_Stack_Vars
24             #define INLINE_STACK_ITEMS Inline_Stack_Items
25             #define INLINE_STACK_ITEM(x) Inline_Stack_Item(x)
26             #define INLINE_STACK_RESET Inline_Stack_Reset
27             #define INLINE_STACK_PUSH(x) Inline_Stack_Push(x)
28             #define INLINE_STACK_DONE Inline_Stack_Done
29             #define INLINE_STACK_RETURN(x) Inline_Stack_Return(x)
30             #define INLINE_STACK_VOID Inline_Stack_Void
31              
32             #define inline_stack_vars Inline_Stack_Vars
33             #define inline_stack_items Inline_Stack_Items
34             #define inline_stack_item(x) Inline_Stack_Item(x)
35             #define inline_stack_reset Inline_Stack_Reset
36             #define inline_stack_push(x) Inline_Stack_Push(x)
37             #define inline_stack_done Inline_Stack_Done
38             #define inline_stack_return(x) Inline_Stack_Return(x)
39             #define inline_stack_void Inline_Stack_Void
40             END_OF_INLINE_H
41              
42              
43             sub run {
44 0     0 0   my ($distvname, $inline_file) = @ARGV;
45              
46             edit_file("$distvname/Makefile.PL", sub {
47 0 0   0     s/^(use Inline::Module::LeanDist.*?)$/# Commented out for distribution by Inline::Module::LeanDist\n#$1/mg
48             || die "unable to find 'use Inline::Module::LeanDist' directive in Makefile.PL";
49 0           });
50              
51             edit_file("$distvname/$inline_file", sub {
52 0     0     my $xsloader_snippet = qq{# XSLoader added for distribution by Inline::Module::LeanDist:\nrequire XSLoader; XSLoader::load(__PACKAGE__, \$VERSION);\n};
53 0 0         s/^(use Inline::Module::LeanDist.*?)$/# Commented out for distribution by Inline::Module::LeanDist\n#$1\n\n$xsloader_snippet/mg
54             || die "unable to find 'use Inline' directive in $inline_file";
55 0           });
56              
57 0 0         $inline_file =~ m{([^/]+)[.]pm$}
58             || die "couldn't extract base filename from filename $inline_file";
59              
60 0           my $base_filename = $1;
61              
62             edit_file("$distvname/Makefile.PL", sub {
63 0 0   0     s/^(\s*OBJECT\s*=>\s*')(.*?')/$1${base_filename}.o $2/mg
64             || die "couldn't find OBJECT => '' in Makefile.PL";
65 0           });
66              
67 0           my $xs_file;
68              
69             File::Find::find({
70             wanted => sub {
71 0 0   0     -f or return;
72              
73 0 0         if (m{/$base_filename[.]xs$}) {
74 0 0         die "found multiple $base_filename.xs files in the $Inline::Module::LeanDist::inline_build_path dir"
75             if defined $xs_file;
76              
77 0           $xs_file = $_;
78             }
79             },
80 0           no_chdir => 1,
81             }, $Inline::Module::LeanDist::inline_build_path);
82              
83 0 0         die "unable to find $base_filename.xs in $Inline::Module::LeanDist::inline_build_path"
84             if !defined $xs_file;
85              
86 0 0         copy($xs_file, "$distvname/$base_filename.xs") || die "copy of xs file failed: $!";
87              
88 0           add_to_manifest("$distvname/MANIFEST", "$base_filename.xs");
89              
90             edit_file("$distvname/$base_filename.xs", sub {
91 0 0   0     s/^#include "INLINE.h"\s*$/$inline_h_file/mg
92             || die "couldn't find INLINE.h include in $distvname/$base_filename.xs";
93 0           });
94             }
95              
96              
97              
98             sub edit_file {
99 0     0 0   my ($filename, $cb) = @_;
100              
101 0 0         open(my $fh, '<', $filename) || die "unable to open $filename for reading: $!";
102              
103 0           local $_;
104              
105             {
106 0           local $/;
  0            
107 0           $_ = <$fh>;
108             }
109              
110 0           undef $fh;
111              
112 0           $cb->();
113              
114             ## Write to $filename.edit because MakeMaker likes to use hard-links. You can always over-ride this
115             ## in the Makefile.PL with DIST_CP => 'cp', but this way it will work even if a user forgets to do this.
116              
117 0 0         open($fh, '>', "$filename.edit") || die "unable to open $filename.edit for writing: $!";
118              
119 0           print $fh $_;
120              
121 0           undef $fh;
122              
123 0 0         rename("$filename.edit", $filename) || die "couldn't rename $filename.edit to $filename: $!";
124             }
125              
126              
127             sub add_to_manifest {
128 0     0 0   my ($manifest_file, $line) = @_;
129              
130 0           copy($manifest_file, "$manifest_file.edit");
131              
132 0 0         rename("$manifest_file.edit", $manifest_file) || die "couldn't rename $manifest_file.edit to $manifest_file: $!";
133              
134 0 0         open(my $fh, '>>', $manifest_file) || die "couldn't open $manifest_file for append: $!";
135              
136 0           print $fh "$line\n";
137             }
138              
139              
140             1;