| 1 | package MYDan::Util::Command; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use MYDan; | ||
| 6 | |||
| 7 | sub new | ||
| 8 | { | ||
| 9 | my ( $class, $name, $cmd, $alias ) = @_; | ||
| 10 | bless +{ name => $name, cmd => $cmd, alias => $alias || +{} }, ref $class || $class; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub do | ||
| 14 | { | ||
| 15 | my ( $this, $cmd, @argv )= @_; | ||
| 16 | |||
| 17 | $ENV{MYDan_DEBUG} = 1 && $cmd = lc( $cmd ) if $cmd && $cmd =~ /^[A-Z][a-z]*$/; | ||
| 18 | |||
| 19 | if( $cmd && $cmd eq '--box' ) | ||
| 20 | { | ||
| 21 | system "touch '$MYDan::PATH/dan/.ignore'" if -d "$MYDan::PATH/dan"; | ||
| 22 | print "Switch to `box` first.\n"; | ||
| 23 | return; | ||
| 24 | } | ||
| 25 | elsif( $cmd && $cmd eq '--dan' ) | ||
| 26 | { | ||
| 27 | unlink "$MYDan::PATH/dan/.ignore" if -f "$MYDan::PATH/dan/.ignore"; | ||
| 28 | print "Switch to `dan` first.\n"; | ||
| 29 | return; | ||
| 30 | } | ||
| 31 | |||
| 32 | $cmd = $this->{alias}{$cmd} if $cmd && $this->{alias}{$cmd}; | ||
| 33 | |||
| 34 | my ( $c ) = grep{ $cmd && $_->[0] eq $cmd }@{$this->{cmd}}; | ||
| 35 | |||
| 36 | $this->help() and return unless $c; | ||
| 37 | |||
| 38 | my @x = splice @$c, 2; | ||
| 39 | @x = reverse @x if -f "$MYDan::PATH/dan/.ignore"; | ||
| 40 | map{ exec join( ' ', "$MYDan::PATH/$_", map{"'$_'"}@argv ) if -e "$MYDan::PATH/$_" }@x; | ||
| 41 | print "$cmd is not installed\n"; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub help | ||
| 45 | { | ||
| 46 | my $this = shift; | ||
| 47 | |||
| 48 | my ( $name, $cmd ) = @$this{qw( name cmd )}; | ||
| 49 | |||
| 50 | print "Usage: $name COMMAND [arg...]\n"; | ||
| 51 | print "Options:\n\t--dan\tSwitch to `dan` first.\n\t--box\tSwitch to `box` first.\n\n"; | ||
| 52 | print "\tHelp\tshow detail\n"; | ||
| 53 | print "Commands:\n"; | ||
| 54 | |||
| 55 | map{ | ||
| 56 | my @x = splice @$_, 2; | ||
| 57 | @x = reverse @x if -f "$MYDan::PATH/dan/.ignore"; | ||
| 58 | my ( $x ) = grep{ -e "$MYDan::PATH/$_" }@x; | ||
| 59 | if( $ENV{MYDan_DEBUG} ) | ||
| 60 | { | ||
| 61 | printf "\t%s $_->[0]\t$_->[1]\n", $x ? $x =~ /^dan/ ? 'dan' : 'box' : 'nil'; | ||
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | printf "\t%s $_->[0]\t$_->[1]\n", $x ? '*': ' '; | ||
| 66 | } | ||
| 67 | }@$cmd; | ||
| 68 | |||
| 69 | print "\nRun '$name COMMAND --help' for more information on a command.\n" | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | __END__ |