File Coverage

blib/lib/Module/Build/PPMMaker.pm
Criterion Covered Total %
statement 55 65 84.6
branch 8 20 40.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 72 98 73.4


line stmt bran cond sub pod time code
1             package Module::Build::PPMMaker;
2              
3 2     2   28 use strict;
  2         6  
  2         152  
4 2     2   18 use warnings;
  2         14  
  2         182  
5 2     2   18 use Config;
  2         20  
  2         2660  
6              
7             our $VERSION = '0.42_33';
8             $VERSION = eval $VERSION;
9              
10             # This code is mostly borrowed from ExtUtils::MM_Unix 6.10_03, with a
11             # few tweaks based on the PPD spec at
12             # http://www.xav.com/perl/site/lib/XML/PPD.html
13              
14             # The PPD spec is based on
15              
16             sub new {
17 5     5 0 16 my $package = shift;
18 5         52 return bless {@_}, $package;
19             }
20              
21             sub make_ppd {
22 5     5 0 53 my ($self, %args) = @_;
23 5         17 my $build = delete $args{build};
24              
25 5         11 my @codebase;
26 5 100       49 if (exists $args{codebase}) {
27 2 50       8 @codebase = ref $args{codebase} ? @{$args{codebase}} : ($args{codebase});
  0         0  
28             } else {
29 3         51 my $distfile = $build->ppm_name . '.tar.gz';
30 3         158 print "Using default codebase '$distfile'\n";
31 3         19 @codebase = ($distfile);
32             }
33              
34 5         12 my %dist;
35 5         16 foreach my $info (qw(name author abstract version)) {
36 20         68 my $method = "dist_$info";
37 20 50       169 $dist{$info} = $build->$method() or die "Can't determine distribution's $info\n";
38             }
39              
40 5         20 $self->_simple_xml_escape($_) foreach $dist{abstract}, @{$dist{author}};
  5         33  
41              
42             # TODO: could add tag if we knew what the URLs were for
43             # various licenses
44 5         30 my $ppd = <<"PPD";
45            
46             $dist{abstract}
47 5         15 @{[ join "\n", map " $_", @{$dist{author}} ]}
  5         66  
48            
49             PPD
50              
51             # We don't include recommended dependencies because PPD has no way
52             # to distinguish them from normal dependencies. We don't include
53             # build_requires dependencies because the PPM installer doesn't
54             # build or test before installing. And obviously we don't include
55             # conflicts either.
56              
57 5         25 foreach my $type (qw(requires)) {
58 5         112 my $prereq = $build->$type();
59 5         30 foreach my $modname (sort keys %$prereq) {
60 0 0       0 next if $modname eq 'perl';
61              
62 0         0 my $min_version = '0.0';
63 0         0 foreach my $c ($build->_parse_conditions($prereq->{$modname})) {
64 0         0 my ($op, $version) = $c =~ /^\s* (<=?|>=?|==|!=) \s* ([\w.]+) \s*$/x;
65              
66             # This is a nasty hack because it fails if there is no >= op
67 0 0       0 if ($op eq '>=') {
68 0         0 $min_version = $version;
69 0         0 last;
70             }
71             }
72              
73             # PPM4 spec requires a '::' for top level modules
74 0 0       0 $modname .= '::' unless $modname =~ /::/;
75              
76 0         0 $ppd .= qq! \n!;
77             }
78             }
79              
80             # We only include these tags if this module involves XS, on the
81             # assumption that pure Perl modules will work on any OS.
82 5 50       10 if (keys %{$build->find_xs_files}) {
  5         97  
83 5         73 my $perl_version = $self->_ppd_version($build->perl_version);
84 5         59 $ppd .= sprintf(<<'EOF', $self->_varchname($build->config) );
85            
86             EOF
87             }
88              
89 5         612 foreach my $codebase (@codebase) {
90 5         28 $self->_simple_xml_escape($codebase);
91 5         25 $ppd .= sprintf(<<'EOF', $codebase);
92            
93             EOF
94             }
95              
96 5         14 $ppd .= <<'EOF';
97            
98            
99             EOF
100              
101 5         23 my $ppd_file = "$dist{name}.ppd";
102 5 50       526 open(my $fh, '>', $ppd_file)
103             or die "Cannot write to $ppd_file: $!";
104              
105             binmode($fh, ":utf8")
106 5 50 33     199 if $] >= 5.008 && $Config{useperlio};
107 5         423 print $fh $ppd;
108 5         444 close $fh;
109              
110 5         155 return $ppd_file;
111             }
112              
113             sub _ppd_version {
114 7     7   25 my ($self, $version) = @_;
115              
116             # generates something like "0,18,0,0"
117 7         72 return join ',', (split(/\./, $version), (0)x4)[0..3];
118             }
119              
120             sub _varchname { # Copied from PPM.pm
121 7     7   44 my ($self, $config) = @_;
122 7         25 my $varchname = $config->{archname};
123             # Append "-5.8" to architecture name for Perl 5.8 and later
124 7 50       37 if ($] >= 5.008) {
125 7         72 my $vstring = sprintf "%vd", $^V;
126 7         78 $vstring =~ s/\.\d+$//;
127 7         29 $varchname .= "-$vstring";
128             }
129 7         35 return $varchname;
130             }
131              
132             {
133             my %escapes = (
134             "\n" => "\\n",
135             '"' => '"',
136             '&' => '&',
137             '>' => '>',
138             '<' => '<',
139             );
140             my $rx = join '|', keys %escapes;
141              
142             sub _simple_xml_escape {
143 15     15   132 $_[1] =~ s/($rx)/$escapes{$1}/go;
144             }
145             }
146              
147             1;
148             __END__