| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::CPAN::Get; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 63426 | use strict; | 
|  | 4 |  |  |  |  | 30 |  | 
|  | 4 |  |  |  |  | 98 |  | 
| 4 | 4 |  |  | 4 |  | 16 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 120 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 1418 | use App::CPAN::Get::Utils qw(process_module_name_and_version); | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 74 |  | 
| 7 | 4 |  |  | 4 |  | 2069 | use Class::Utils qw(set_params); | 
|  | 4 |  |  |  |  | 86709 |  | 
|  | 4 |  |  |  |  | 71 |  | 
| 8 | 4 |  |  | 4 |  | 280 | use Error::Pure qw(err); | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 133 |  | 
| 9 | 4 |  |  | 4 |  | 6855 | use Getopt::Std; | 
|  | 4 |  |  |  |  | 222 |  | 
|  | 4 |  |  |  |  | 209 |  | 
| 10 | 4 |  |  | 4 |  | 1613 | use IO::Barf qw(barf); | 
|  | 4 |  |  |  |  | 2187 |  | 
|  | 4 |  |  |  |  | 50 |  | 
| 11 | 4 |  |  | 4 |  | 2576 | use LWP::UserAgent; | 
|  | 4 |  |  |  |  | 172459 |  | 
|  | 4 |  |  |  |  | 158 |  | 
| 12 | 4 |  |  | 4 |  | 1898 | use Menlo::Index::MetaCPAN; | 
|  | 4 |  |  |  |  | 76669 |  | 
|  | 4 |  |  |  |  | 155 |  | 
| 13 | 4 |  |  | 4 |  | 1706 | use URI::cpan; | 
|  | 4 |  |  |  |  | 33758 |  | 
|  | 4 |  |  |  |  | 1844 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $VERSION = 0.07; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Constructor. | 
| 18 |  |  |  |  |  |  | sub new { | 
| 19 | 4 |  |  | 4 | 1 | 5000 | my ($class, @params) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Create object. | 
| 22 | 4 |  |  |  |  | 11 | my $self = bless {}, $class; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # LWP::User agent object. | 
| 25 | 4 |  |  |  |  | 14 | $self->{'lwp_user_agent'} = undef; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # Process parameters. | 
| 28 | 4 |  |  |  |  | 15 | set_params($self, @params); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 4 | 100 |  |  |  | 44 | if (defined $self->{'lwp_user_agent'}) { | 
| 31 | 2 | 100 |  |  |  | 14 | if (! $self->{'lwp_user_agent'}->isa('LWP::UserAgent')) { | 
| 32 | 1 |  |  |  |  | 5 | err "Parameter 'lwp_user_agent' must be a ". | 
| 33 |  |  |  |  |  |  | 'LWP::UserAgent instance.'; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | } else { | 
| 36 | 2 |  |  |  |  | 17 | $self->{'lwp_user_agent'} = LWP::UserAgent->new; | 
| 37 | 2 |  |  |  |  | 5185 | $self->{'lwp_user_agent'}->agent(__PACKAGE__.'/'.$VERSION); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # Object. | 
| 41 | 3 |  |  |  |  | 142 | return $self; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # Run. | 
| 45 |  |  |  |  |  |  | sub run { | 
| 46 | 1 |  |  | 1 | 1 | 2 | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # Process arguments. | 
| 49 | 1 |  |  |  |  | 5 | $self->{'_opts'} = { | 
| 50 |  |  |  |  |  |  | 'h' => 0, | 
| 51 |  |  |  |  |  |  | }; | 
| 52 | 1 | 50 | 33 |  |  | 9 | if (! getopts('h', $self->{'_opts'}) || @ARGV < 1 | 
|  |  |  | 33 |  |  |  |  | 
| 53 |  |  |  |  |  |  | || $self->{'_opts'}->{'h'}) { | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 1 |  |  |  |  | 130 | print STDERR "Usage: $0 [-h] [--version] module_name[module_version]\n"; | 
| 56 | 1 |  |  |  |  | 15 | print STDERR "\t-h\t\tPrint help.\n"; | 
| 57 | 1 |  |  |  |  | 10 | print STDERR "\t--version\tPrint version.\n"; | 
| 58 | 1 |  |  |  |  | 10 | print STDERR "\tmodule_name\tModule name. e.g. ". | 
| 59 |  |  |  |  |  |  | "App::Pod::Example\n"; | 
| 60 | 1 |  |  |  |  | 11 | print STDERR "\tmodule_version\tModule version. e.g. \@1.23, ~1.23 etc.\n"; | 
| 61 | 1 |  |  |  |  | 6 | return 1; | 
| 62 |  |  |  |  |  |  | } | 
| 63 | 0 |  |  |  |  |  | $self->{'_module_name_and_version'} = shift @ARGV; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # Parse module name and version. | 
| 66 |  |  |  |  |  |  | ($self->{'_module_name'}, $self->{'_module_version_range'}) | 
| 67 | 0 |  |  |  |  |  | = process_module_name_and_version($self->{'_module_name_and_version'}); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | # Get meta information for module name. | 
| 70 |  |  |  |  |  |  | # XXX Why not small dist?. | 
| 71 |  |  |  |  |  |  | my $res = Menlo::Index::MetaCPAN->new->search_packages({ | 
| 72 |  |  |  |  |  |  | 'package' => $self->{'_module_name'}, | 
| 73 |  |  |  |  |  |  | exists $self->{'_module_version_range'} | 
| 74 | 0 | 0 |  |  |  |  | ? ('version_range' => $self->{'_module_version_range'}) | 
| 75 |  |  |  |  |  |  | : (), | 
| 76 |  |  |  |  |  |  | }); | 
| 77 | 0 | 0 |  |  |  |  | if (! defined $res) { | 
| 78 | 0 |  |  |  |  |  | err "Module '".$self->{'_module_name'}."' doesn't exist."; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | # Download dist. | 
| 82 | 0 | 0 |  |  |  |  | if (! $res->{'download_uri'}) { | 
| 83 | 0 |  |  |  |  |  | err "Value 'download_uri' doesn't exist."; | 
| 84 |  |  |  |  |  |  | } | 
| 85 | 0 |  |  |  |  |  | my $dist_res = $self->{'lwp_user_agent'}->get($res->{'download_uri'}); | 
| 86 | 0 | 0 |  |  |  |  | if (! $dist_res->is_success) { | 
| 87 | 0 |  |  |  |  |  | err "Cannot download '$res->{'download_uri'}'."; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # Save to file. | 
| 91 | 0 | 0 |  |  |  |  | if (! $res->{'uri'}) { | 
| 92 | 0 |  |  |  |  |  | err "Value 'uri' doesn't exist."; | 
| 93 |  |  |  |  |  |  | } | 
| 94 | 0 |  |  |  |  |  | my $u = URI->new($res->{'uri'}); | 
| 95 | 0 |  |  |  |  |  | my $dist_file = ($u->path_segments)[-1]; | 
| 96 | 0 |  |  |  |  |  | barf($dist_file, $dist_res->decoded_content); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | print "Package on '$res->{'download_uri'}' was downloaded.\n"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | return 0; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |