File Coverage

blib/lib/PPM/Make/Install.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package PPM::Make::Install;
2 1     1   1582 use strict;
  1         2  
  1         25  
3 1     1   3 use warnings;
  1         2  
  1         22  
4 1     1   3 use base qw(PPM::Make);
  1         1  
  1         131  
5             use File::Path;
6             use PPM::Make::Util qw(:all);
7             use PPM::Make::Config qw(:all);
8             use PPM::Make::Meta;
9             use Config;
10             use Cwd;
11             require File::Spec;
12              
13             our $VERSION = '0.9904';
14              
15             sub new {
16             my ($class, %opts) = @_;
17              
18             die "\nInvalid option specification" unless check_opts_install(%opts);
19             die "Must have the ppm utility to install" unless HAS_PPM;
20             my $arch = $Config{archname};
21             my $os = $Config{osname};
22             if ($] >= 5.008) {
23             my $vstring = sprintf "%vd", $^V;
24             $vstring =~ s/\.\d+$//;
25             $arch .= "-$vstring";
26             }
27             my $has = what_have_you($opts{program}, $arch, $os);
28             my $self = {
29             opts => \%opts || {},
30             cwd => '',
31             has => $has,
32             args => {},
33             ppd => '',
34             archive => '',
35             prereq_pm => {},
36             file => '',
37             version => '',
38             use_mb => '',
39             ARCHITECTURE => $arch,
40             OS => $os,
41             };
42             bless $self, $class;
43             }
44              
45             sub check_opts_install {
46             my %opts = @_;
47             my %legal =
48             map {$_ => 1} qw(force ignore dist program upgrade remove skip);
49             foreach (keys %opts) {
50             next if $legal{$_};
51             warn "Unknown option '$_'\n";
52             return;
53             }
54              
55             if (defined $opts{program} and my $progs = $opts{program}) {
56             unless (ref($progs) eq 'HASH') {
57             warn "Please supply a HASH reference to 'program'";
58             return;
59             }
60             my %ok = map {$_ => 1} qw(zip unzip tar gzip make);
61             foreach (keys %{$progs}) {
62             next if $ok{$_};
63             warn "Unknown program option '$_'\n";
64             return;
65             }
66             }
67             return 1;
68             }
69              
70             sub make_ppm {
71             my $self = shift;
72             my $dist = $self->{opts}->{dist};
73             if ($dist) {
74             my $build_dir = File::Spec->tmpdir;
75             chdir $build_dir or die "Cannot chdir to $build_dir: $!";
76             print "Working directory: $build_dir\n";
77             unless ($dist = $self->fetch_file($dist)) {
78             die $self->{fetch_error};
79             }
80             # if ($dist =~ m!$protocol!
81             # or $dist =~ m!^\w/\w\w/! or $dist !~ m!$ext!);
82             print "Extracting files from $dist ....\n";
83             my $name = $self->extract_dist($dist, $build_dir);
84             chdir $name or die "Cannot chdir to $name: $!";
85             $self->{file} = $dist;
86             }
87             die "Need a Makefile.PL or Build.PL to build"
88             unless (-f 'Makefile.PL' or -f 'Build.PL');
89             $self->{cwd} = cwd;
90             my $mb = -e 'Build.PL';
91             $self->{mb} = $mb;
92             my $force = $self->{opts}->{force};
93             die "This distribution requires Module::Build to build"
94             if ($mb and not HAS_MB);
95             $self->build_dist()
96             unless (-d 'blib' and (-f 'Makefile' or ($mb and -f 'Build') )
97             and not $force);
98             my $meta = PPM::Make::Meta->new(dir => $self->{cwd});
99             die qq{Creating PPM::Make::Meta object failed}
100             unless ($meta and (ref($meta) eq 'PPM::Make::Meta'));
101             $meta->meta();
102             foreach my $key( keys %{$meta->{info}}) {
103             next unless defined $meta->{info}->{$key};
104             $self->{args}->{$key} ||= $meta->{info}->{$key};
105             }
106             for my $search_info(qw(dist_search mod_search)) {
107             next unless defined $meta->{$search_info};
108             $self->{$search_info} = $meta->{$search_info};
109             }
110             $self->{version} = (defined $self->{args}->{VERSION_FROM}) ?
111             parse_version($self->{args}->{VERSION_FROM}) :
112             ($self->{args}->{VERSION});
113             $self->make_html() unless (-d 'blib/html' and not $force);
114             $dist = $self->make_dist();
115             $self->make_ppd($dist);
116             return 1;
117             }
118              
119             sub ppm_install {
120             my $self = shift;
121             my $cwd = $self->{cwd};
122             (my $package = $self->{ppd}) =~ s!\.ppd$!!;
123             my $version = $self->{version};
124             print "Installing $package ...\n";
125             if (HAS_PPM >= 3) {
126             my @args = $self->{opts}->{upgrade} ?
127             ('ppm', 'upgrade', $self->{ppd}) :
128             ('ppm', 'install', $self->{ppd});
129             system(@args) == 0 or die "Cannot install/upgrade $self->{ppd}: $?";
130             }
131             else {
132             my @args = $self->{opts}->{upgrade} ?
133             ('ppm', 'verify', '--upgrade', $self->{ppd}) :
134             ('ppm', 'install', $self->{ppd});
135             system(@args) == 0 or die "Cannot install/upgrade $self->{ppd}: $?";
136             }
137             return unless $self->{opts}->{remove};
138             my $file = $self->{file};
139             unless ($file) {
140             warn "Cannot clean files unless a distribution is specified";
141             return;
142             }
143             if (-f "../$file") {
144             print "Removing $cwd/../$file ....\n";
145             unlink "$cwd/../$file" or warn "Cannot unlink $cwd/../$file: $!";
146             }
147             chdir('..') or die "Cannot move up one directory: $!";
148             if (-d $cwd) {
149             print "Removing $cwd ...\n";
150             rmtree($cwd) or warn "Cannot remove $cwd: $!";
151             }
152             return 1;
153             }
154              
155             1;
156              
157             __END__