| 1 | package App::PPK::Deptool; | ||
| 2 | |||
| 3 | # Copyright (c) 2018, cPanel, L.L.C. | ||
| 4 | # All rights reserved. | ||
| 5 | # http://cpanel.net/ | ||
| 6 | # | ||
| 7 | # This is free software; you can redistribute it and/or modify it under the same | ||
| 8 | # terms as Perl itself. See the LICENSE file for further details. | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Getopt::Long ('GetOptionsFromArray'); | ||
| 14 | use Build::PPK::Deptool (); | ||
| 15 | |||
| 16 | my $DEFAULT_DIST_INDEX = 'DIST.INDEX'; | ||
| 17 | my $DEFAULT_PROG_INDEX = 'PROG.INDEX'; | ||
| 18 | |||
| 19 | my %ACTIONS = ( | ||
| 20 | 'fetch-dist' => { | ||
| 21 | 'usage' => [ | ||
| 22 | '[--force] <dist ...>', | ||
| 23 | '--deps-for <targets ...>' | ||
| 24 | ], | ||
| 25 | |||
| 26 | 'handler' => \&fetch_dist | ||
| 27 | }, | ||
| 28 | |||
| 29 | 'list-targets' => { | ||
| 30 | 'usage' => undef, | ||
| 31 | 'handler' => \&list_targets | ||
| 32 | }, | ||
| 33 | |||
| 34 | 'list-target-deps' => { | ||
| 35 | 'usage' => '<target ...>', | ||
| 36 | 'handler' => \&list_target_deps | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub usage { | ||
| 41 | my ($action) = @_; | ||
| 42 | my @actions = $action ? ($action) : sort keys %ACTIONS; | ||
| 43 | |||
| 44 | my $first = 1; | ||
| 45 | |||
| 46 | foreach my $action (@actions) { | ||
| 47 | my $usage = $ACTIONS{$action}->{'usage'}; | ||
| 48 | my @synopses = ref($usage) eq 'ARRAY' ? @{$usage} : ($usage); | ||
| 49 | |||
| 50 | if ($first) { | ||
| 51 | print STDERR "usage: "; | ||
| 52 | } | ||
| 53 | |||
| 54 | foreach my $synopsis (@synopses) { | ||
| 55 | unless ($first) { | ||
| 56 | print STDERR " "; | ||
| 57 | } | ||
| 58 | |||
| 59 | print STDERR "$0 $action"; | ||
| 60 | print STDERR " $synopsis" if $synopsis; | ||
| 61 | print STDERR "\n"; | ||
| 62 | |||
| 63 | $first = 0; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | exit 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub fetch_dist { | ||
| 71 | my ( $deptool, @args ) = @_; | ||
| 72 | my %opts; | ||
| 73 | my @dists; | ||
| 74 | |||
| 75 | GetOptionsFromArray( | ||
| 76 | \@args, | ||
| 77 | 'force' => \$opts{'force'}, | ||
| 78 | 'deps-for=s@{,}' => \$opts{'targets'} | ||
| 79 | ); | ||
| 80 | |||
| 81 | if ( $opts{'targets'} ) { | ||
| 82 | push @dists, $deptool->target_deps( 'targets' => $opts{'targets'} ); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | push @dists, @args; | ||
| 86 | } | ||
| 87 | |||
| 88 | usage('fetch-dist') unless @dists; | ||
| 89 | |||
| 90 | foreach my $dist (@dists) { | ||
| 91 | if ( -e $dist ) { | ||
| 92 | next unless $opts{'force'}; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $path = $ENV{'PPK_DISTDIR'} ? "$ENV{'PPK_DISTDIR'}/$dist" : $dist; | ||
| 96 | |||
| 97 | $deptool->fetch_dist( | ||
| 98 | 'dist' => $dist, | ||
| 99 | 'path' => $path, | ||
| 100 | 'callback' => sub { | ||
| 101 | my (%args) = @_; | ||
| 102 | |||
| 103 | print "fetch $args{'url'} => $args{'path'}\n"; | ||
| 104 | } | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | |||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub list_targets { | ||
| 112 | my ($deptool) = @_; | ||
| 113 | |||
| 114 | foreach my $target ( $deptool->targets ) { | ||
| 115 | print "$target\n"; | ||
| 116 | } | ||
| 117 | |||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub list_target_deps { | ||
| 122 | my ( $deptool, @targets ) = @_; | ||
| 123 | |||
| 124 | usage('list-target-deps') unless @targets; | ||
| 125 | |||
| 126 | my @deps = $deptool->target_deps( 'targets' => \@targets ); | ||
| 127 | |||
| 128 | foreach my $dep (@deps) { | ||
| 129 | my $path = $ENV{'PPK_DISTDIR'} ? "$ENV{'PPK_DISTDIR'}/$dep" : $dep; | ||
| 130 | |||
| 131 | print "$path\n"; | ||
| 132 | } | ||
| 133 | |||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub run { | ||
| 138 | my ( $class, @args ) = @_; | ||
| 139 | my $action = shift @args; | ||
| 140 | |||
| 141 | usage() unless $action && $ACTIONS{$action}; | ||
| 142 | |||
| 143 | my $deptool = Build::PPK::Deptool->new( | ||
| 144 | 'dist_index' => $DEFAULT_DIST_INDEX, | ||
| 145 | 'prog_index' => $DEFAULT_PROG_INDEX | ||
| 146 | ); | ||
| 147 | |||
| 148 | return $ACTIONS{$action}->{'handler'}->( $deptool, @args ); | ||
| 149 | } |