| 1 | package App::lcpan::Cmd::extract_script; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | require App::lcpan; | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Perinci::Object; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 12 | 1 | our $DATE = '2023-09-26'; # DATE | |||||||||
| 13 | 1 | our $DIST = 'App-lcpan'; # DIST | |||||||||
| 14 | 1 | our $VERSION = '1.074'; # VERSION | |||||||||
| 15 | |||||||||||
| 16 | 1 | our %SPEC; | |||||||||
| 17 | |||||||||||
| 18 | 1 | $SPEC{'handle_cmd'} = { | |||||||||
| 19 | v => 1.1, | ||||||||||
| 20 | summary => "Extract a script's latest release file to current directory", | ||||||||||
| 21 | args => { | ||||||||||
| 22 | %App::lcpan::common_args, | ||||||||||
| 23 | %App::lcpan::script_args, | ||||||||||
| 24 | %App::lcpan::all_args, | ||||||||||
| 25 | }, | ||||||||||
| 26 | tags => ['write-to-fs'], | ||||||||||
| 27 | }; | ||||||||||
| 28 | 0 | ▶ | sub handle_cmd { | ||||||||
|
Subroutine
handle_cmd: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 29 | 0 | require Archive::Extract; | |||||||||
| 30 | |||||||||||
| 31 | 0 | my %args = @_; | |||||||||
| 32 | |||||||||||
| 33 | 0 | my $state = App::lcpan::_init(\%args, 'ro'); | |||||||||
| 34 | 0 | my $dbh = $state->{dbh}; | |||||||||
| 35 | |||||||||||
| 36 | 0 | my $script = $args{script}; | |||||||||
| 37 | |||||||||||
| 38 | 0 | my $sth = $dbh->prepare("SELECT | |||||||||
| 39 | script.name script, | ||||||||||
| 40 | file.cpanid author, | ||||||||||
| 41 | file.name release | ||||||||||
| 42 | FROM script | ||||||||||
| 43 | LEFT JOIN file ON script.file_id=file.id | ||||||||||
| 44 | LEFT JOIN module ON file.id=module.file_id | ||||||||||
| 45 | WHERE script.name=? | ||||||||||
| 46 | GROUP BY file.id | ||||||||||
| 47 | ORDER BY module.version_numified DESC"); | ||||||||||
| 48 | |||||||||||
| 49 | 0 | $sth->execute($script); | |||||||||
| 50 | |||||||||||
| 51 | 0 | my @paths; | |||||||||
| 52 | my %mem; | ||||||||||
| 53 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | ▶ | unless ($args{all}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | ▶ | next if $mem{$row->{script}}++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | push @paths, App::lcpan::_fullpath( | ||||||||||
| 58 | 0 | $row->{release}, $state->{cpan}, $row->{author}); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | return [404, "No release for script '$script'"] unless @paths; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 0 | my $envres = envresmulti(); | |||||||||
| 64 | 0 | for my $i (0..$#paths) { | |||||||||
| 65 | 0 | my $path = $paths[$i]; | |||||||||
| 66 | 0 | ▶ | (-f $path) or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | $envres->add_result( | |||||||||
| 68 | 404, "File not found: $path", | ||||||||||
| 69 | {item_id => $path}, | ||||||||||
| 70 | ); | ||||||||||
| 71 | 0 | next; | |||||||||
| 72 | }; | ||||||||||
| 73 | 0 | my $ae = Archive::Extract->new(archive => $path); | |||||||||
| 74 | 0 | ▶ | $ae->extract or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | $envres->add_result( | |||||||||
| 76 | 500, "Can't extract: " . $ae->error, | ||||||||||
| 77 | {item_id => $path}, | ||||||||||
| 78 | ); | ||||||||||
| 79 | 0 | next; | |||||||||
| 80 | }; | ||||||||||
| 81 | 0 | $envres->add_result( | |||||||||
| 82 | 200, "OK", | ||||||||||
| 83 | {item_id => $path}, | ||||||||||
| 84 | ); | ||||||||||
| 85 | } | ||||||||||
| 86 | 0 | $envres->as_struct; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 1 | 1; | |||||||||
| 90 | # ABSTRACT: Extract a script's latest release file to current directory | ||||||||||
| 91 | |||||||||||
| 92 | __END__ | ||||||||||