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